1

As consequence of my previous question ( XML serialization of interfaces ) I obtained another problem...

I have an application that export data from a database. The export procedure is implemented by different concrete classes that implement a common interface used for invocation.

The concrete implementations are loaded as plug-ins (DLLs) so I don't reference them in my code directly.

I need to serialize instances of these concrete classes as byte arrays into my database, but now when I try to deserialize them from a byte array I obtain a SerializationException: Unable to find assembly …

I suppose it appens because I load at runtime the dll with the concrete implementation of my interface...

How can I solve it?

NOTE I'm using this code to deserialize objects:

    public static object DeSerialize(byte[] arrayToDeSerialize)
    {
        object serializedObject;
        using (MemoryStream stream = new MemoryStream(arrayToDeSerialize))
        {
            //Creating binary formatter to De-Serialize string.
            BinaryFormatter formatter = new BinaryFormatter();

            //De-Serializing.
            serializedObject = formatter.Deserialize(stream);
        }
        return serializedObject;
    }
Community
  • 1
  • 1
davioooh
  • 23,742
  • 39
  • 159
  • 250

1 Answers1

5

You could hook the AppDomain.AssemblyResolve event to load the assemblies as they are needed. the event is raised each time that the runtime needs an assembly that it cannot resolve. It gives you one last chance to provide the assembly before the "Unable to find assembly" exception is thrown. Examples are on the page that I linked.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • Thank you for this suggestion, I think it can solve my problem, but I cannot understand how to apply it to my deserializing method... Sorry but I'm new to .NET serialization... – davioooh Feb 06 '12 at 15:50
  • I'm working on a WinForm application, where I have to declare my `AppDomain` and the event handler? in my `Program` class? – davioooh Feb 06 '12 at 16:11
  • Just use AppDomain.CurrentDomain: http://msdn.microsoft.com/en-us/library/system.appdomain.currentdomain.aspx. Your Main is probably a good place to hook that event. – Chris Shain Feb 06 '12 at 16:12
  • Ok, putting the handler in my `Main` the event is fired, but there's something wrong... In my `MyResolveEventHandler` I've tried to put: `Type resType = Type.GetType(args.Name); return resType.Assembly;` but resType is always `null`. Am I wrong? – davioooh Feb 06 '12 at 16:43
  • You have to `return` the resolved type. Weird semantics for an event handler. See the example in the original link for AssemblyResolve, at the bottom: `return typeof(MyType).Assembly;` – Chris Shain Feb 06 '12 at 16:45
  • Ahh- you need to dynamically load the assembly from some other path, using `Assembly.Load`, `Assembly.LoadFrom`, or `Assembly.LoadFile`. If `Type.GetType` would have worked, then this whole exercise would have been unneccessary. Type.GetType uses the default Assembly resolution scheme. See http://stackoverflow.com/questions/5260404/resolve-assembly-references-from-another-folder also – Chris Shain Feb 06 '12 at 17:00
  • `AssemblyResolve` is for **CurrentDomain**, not valid for another domain `AppDomain.CreateDomain` – Kiquenet Dec 01 '15 at 13:27