Fun with Subversion on Windows 
Hi there,

I recently had to deal with a Subversion repository that produced some strange errors. Here's what happened.

We run Subversion using mod_dav_svn on Apache on OpenSSL. The end result is that we connect through an encrypted environment and our data is safe. However, although our environment worked fine when I set it up, users started to tell me that they weren't able to commit any files. The error they received was this:

Error: MKACTIVITY of '/sourcecode/!svn/act/23ae8076-d94a-1d43-9824-5cfe874b60a5': Could not read status line: An existing connection was forcibly closed by the remote host.

What did this mean? I searched for MKACTIVITY and I got a bunch of different hints. Some hints said that the problem was related to case sensitivity (I always use lowercase - case sensitivity is a bug carefully preserved by Unix for historical reasons). Another hint said that there might be a proxy in between Subversion and the HTTPS server. Another hint said that there might be an authentication problem.

The real answer? I had left the Subversion 1.4.3 binaries in place and installed 1.4.5 elsewhere. When I removed the 1.4.3 binaries Apache refused to load. For some reason, Apache was linking to the wrong version!

Now that I understood the problem, I just moved the 1.4.5 binaries into the location where the old ones used to live, and everything works fine.

Let that be a lesson to you - don't upgrade carelessly!
  |  permalink  |  related link  |   ( 4.1 / 498 )
Add Microsoft Word Interop to Your Application 
So I build a lot of data import/export tools. I discovered that it's not as easy as I'd think to import text from a Microsoft Word document into my .NET application. I wasted a few hours hunting around for how to add the reference to the component in my app! Frankly, it's pretty frustrating! However, here are the steps to get it going.

1) Download the appropriate version of Microsoft Office "Primary Interop Assemblies".

Here's the links you need.

Office 2003 PIA

Office 2007 PIA

2) Add the reference to your application.

In Visual Studio:
Select Add Reference from the Project menu.
Select the COM tab of the Add Reference window and double-click the appropriate type library file listed.
For example, for Microsoft Word 2007, select "Microsoft Word 12.0 Object Library".

Select OK to end and add the reference.

That's it!
  |  permalink  |  related link  |   ( 4.1 / 522 )
Add a Javascript Include to a page for a custom control 
I wrote a custom control - non AJAX - recently. This control depends upon a script file. I want this custom control to automatically add the script to each webpage where I use the custom control.

Now, Microsoft's AJAX patterns use an implementation of IScriptControl to add javascript references to a page, but I didn't want to do that. So instead I wrote this tiny block of code in the control:

protected override void OnPreRender(EventArgs e)
{
ScriptManager sm = ScriptManager.GetCurrent(Page);
ScriptReference sr = new ScriptReference("myscript.js");
sm.Scripts.Add(sr);
}


This adds the reference dynamically at the correct point in the page's load process. Enjoy!
  |  permalink  |  related link  |   ( 3 / 265 )
Serialize an untyped object 
Whoah! So what happens if you have an "Object", and you don't know exactly what it is, and you want to serialize it and make sure it retains its exact shape when it comes out the other end of the serialization process?

Simple - you store the "Type" of the object as well as the object itself in your serialization stream:

public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("ValueType", Value.GetType(), typeof(Type));
info.AddValue("Value", Value, Value.GetType());
}


To retrieve it later, follow this shorthand:


public MySerializedObjectConstructor(SerializationInfo info, StreamingContext ctxt)
{
Type t = (Type)info.GetValue("ValueType", typeof(Type));
Object Value = info.GetValue("Value", t);
}

  |  permalink  |  related link  |   ( 2.9 / 230 )
Serialization to a Base 64 String 
Let's say you've got an object and you want to save it somewhere. Everybody knows you can serialize this object to a file, but file storage isn't always the optimum solution. What if you want to just keep this object in memory somewhere? I can think of a few different types of storage:

* Memory
* String
* Cache
* Email
* ... and so on.

So, you want to take this object and turn it into a string and save it somewhere. Strings are such fundamentally useful objects that they fit into pretty much anything. However, when Microsoft designed serialization, they specifically decided that strings weren't going to be a valid destination.

Instead, we need multiple steps in our code. First, we convert our object into an in-memory stream. This results in a whole bunch of binary garbage that won't be a valid string in and of itself. So, to turn this into valid text, we use an encoding method called Base64. This method takes all the unprintable binary data and converts it into something we can store in an ASCII string.

Base64 works by figuring that it can encode 64 different values safely into a single byte (which can store up to 256 different values). If you're familiar with binary logic, you'll notice that we're changing an 8-bit value into a 6-bit one. This means that for every three bytes, we require four ASCII characters to store the resulting value. This means that we need to preserve the exact original bytecount of the memory stream, otherwise we risk padding an extra zero or two onto the end of our data.

So, the code ends up doing all the work with a few short lines of code:

public static string SerializeBase64(object o)
{
// Serialize to a base 64 string
byte[] bytes;
long length = 0;
MemoryStream ws = new MemoryStream();
BinaryFormatter sf = new BinaryFormatter();
sf.Serialize(ws, o);
length = ws.Length;
bytes = ws.GetBuffer();
string encodedData = bytes.Length + ":" + Convert.ToBase64String(bytes, 0, bytes.Length, Base64FormattingOptions.None);
return encodedData;
}


public static object DeserializeBase64(string s)
{
// We need to know the exact length of the string - Base64 can sometimes pad us by a byte or two
int p = s.IndexOf(':');
int length = Convert.ToInt32(s.Substring(0, p));

// Extract data from the base 64 string!
byte[] memorydata = Convert.FromBase64String(s.Substring(p + 1));
MemoryStream rs = new MemoryStream(memorydata, 0, length);
BinaryFormatter sf = new BinaryFormatter();
object o = sf.Deserialize(rs);
return o;
}


  |  permalink  |  related link  |   ( 3 / 205 )

Back Next