views:

122

answers:

1

Situation:

I have a bunch of business objects sitting behind a web service. All the objects are encapsulated in a BusObjects.DLL, which is strongly named and sits in the GAC on the server (because other apps on the server access it as well).

I have a click-once client app, which calls the said web service. The click-once app also ships with this BusObjects.DLL.

The way the web service returns data to the client is as follows: It serializes the business object down to a byte array and returns this byte array to the client. The client deserializes the received byte array back into a business object. This is possible because both the client and the server code have a reference to the same BusObjects.DLL. This all works great.

The problem for me is this. When I have the client solution (which includes the BusObjects project) in the VS2005 IDE, the code is unable to deserialize the byte array back into a business object because, it claims,

"Could not load file or assembly 'CC.BusObjects, Version=2.12.1.47, Culture=neutral, PublicKeyToken=af56fdb58c626305' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"

I've tried matching versions, but nothing seems to work if the BusObjects project is being referenced as a project, rather than an outside assembly. Unfortunately, I have to have the BusObjects inside the solution for debugging.

What can I do to fix this issue? I've heard of version redirection, but can't seem to make it work with a strong named assembly, but perhaps I am doing it wrong.

Here is the serialization and deserialization code:

    public static byte[] ToBinary(Object objToBinary)
    {
        MemoryStream memStream = new MemoryStream();
        BinaryFormatter formatter = new BinaryFormatter(null, 
                  new StreamingContext(StreamingContextStates.Clone));
        formatter.Serialize(memStream, objToBinary);
        memStream.Seek(0, SeekOrigin.Begin);
        return memStream.ToArray();
    }


    public static object BinaryTo(byte[] objFromBinary)
    {
        MemoryStream ms = new MemoryStream(objFromBinary);
        BinaryFormatter formatter = new BinaryFormatter();
        ms.Position = 0;
        object obj = formatter.Deserialize(ms);
        return obj;
    }

To serialize:

[WebMethod]
public byte [] GetContacts()
{
   return ToBinary(BusObjects.GetContacts());
}

To deserialize:

byte [] bts = ContactService.GetContacts();
List<Contact> lstContacts = (List<Contact>) BinaryTo(bts);
+2  A: 

What's almost certainly happening is you have the version number set to be auto-incrementing during the build process. This often results in one off version number scenarios that can cause load issues like you are seeing.

Try the following

  • Go to Solution explorer.
  • Expand the Properties Node
  • Open AssemblyInfo.cs
  • Change the AssemblyVersion attribute to have a hard coded version number
JaredPar