System.Runtime.Serialization.SerializationException: Unable to find assembly MyAssembly

后端 未结 9 1800
-上瘾入骨i
-上瘾入骨i 2020-12-19 04:00

So I\'ve found a bunch of threads on this topic but I don\'t think I\'ve found one that applies yet.

Basically my .exe loads a .dll (MyAssembly) file which does the

相关标签:
9条回答
  • 2020-12-19 04:28

    I had a problem with this exception. I am writing a WPF plugin for a third party application. The application loads my assembly which should deserialize a type from another dll, MyType for example, which is in a private assembly added as a reference to my plugin and in the same directory as the plugin dll.

    What seems strange to me was that I can instantiate MyType in the plugin, but it throws this exception when deserializing in the same class.

    The solution for me was suggested by RoadBump and is very straightforward, but I don't understand why the surrounding code can find the assembly (if it can't this method will not work) but the deserialization call in the same code, can't.

    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyTypeResolveEventHandler);
    
    private Assembly MyTypeResolveEventHandler(object sender, ResolveEventArgs args)
    {
        return typeof(MyType).Assembly;
    }
    
    0 讨论(0)
  • 2020-12-19 04:29

    Is the DLL in the same folder like the EXE?
    I see you serialize/deserialize a object that lives in the DLL ("MyAssembly"). When deserialize, the formatter determines the name of the type from serialized data, and tries to find this type in the assembly at the main executable folder, that is- EXE folder.
    Solution- move the DLL to EXE folder. There is a way to cause the formatter to search in another assembly, capture the event AppDomain.AssemblyResolve and return your DLL. See MSDN.

    0 讨论(0)
  • 2020-12-19 04:35

    I would check if Marc Gravell's answer here applies... in short "One option would be to move the type to a library dll that both the other projects reference - then it is only defined once, and it will be happy"

    In my case I had temporarily created a copy of the class that I intended to serialize (in the assembly doing the serializing) early in development, but forgot about it when deserializing! So the deserializer never did have access to the same class that did the serialising!

    It is also worth checking out the properties of the project containing the class being serialized - Check for typos in its assembly name. You might want to try flattening out your namespace hierachy too to see if that helps.

    0 讨论(0)
提交回复
热议问题