FileLoadException with new version of dll

只愿长相守 提交于 2019-12-11 16:38:08

问题


I have an application that needs to work with two versions of a dll. I figured I would just compile the app once, copy the exe to two directories, and then copy the two versions of the dlls to each of these directories. FYI, I'm not doing anything with the GAC. However, I'm getting a FileLoadException on the version with the dll it was not compiled with.

In the project, I have the "Specific Version" set to false for the dll reference. But, maybe that is the wrong setting. How do I get this to work with multiple versions?


回答1:


If the assembly you are referencing is strongly signed the CLR will not load it if it has a different version than the one it was compiled against. You could in this case use the config file to specify the version you would like to use:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="YouAssemblyNameWithoutDotDll"
                              publicKeyToken="your-assembly-token"
                              culture="neutral" />
            <bindingRedirect oldVersion="1.0.0.0"
                             newVersion="2.0.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

Of course this assumes that the new version of the assembly has the same method signatures or you will get a MissingMethodException at runtime.




回答2:


This is because every time you recompile dll, adresses of functions are changed. And since you are binding your app on the compile time with your dll, those adresses are set to be fixed in your app. Basically you must recompile your program everytime with new version of dll (or to be more precise, when you mess with datastructures) or use dynamic loading and GetProcAddress.




回答3:


Make sure that the two versions of the assembly have the same AssemblyVersion specified in your AssemblyInfo.cs file.

Ex:

[assembly: AssemblyVersion("1.0.0.0")]

I believe the default generated for new projects before VS 2008 is:

[assembly: AssemblyVersion("1.0.0.*")]

which will automatically increment the assembly version every time it is built.

This is particularly important for assemblies that are signed since an assembly's strong name includes its version.




回答4:


I ruled against changing the config file, simply because it's another thing I would need to do. In regards to the address changing for different versions of the dll, I don't think I have that problem since I am actually using an interface to call my methods.

To give a little more detail, I simply need to create an instance of one type in multiple dlls. From there, I treat it as an interface object. So, here is my solution:

Assembly asm = Assembly.LoadFrom("XXX.YYY.ZZZ.Bin.dll");
Type type = asm.GetType("MyType");
IMyType obj = Activator.CreateInstance(type) as IMyType;

This seems to work.



来源:https://stackoverflow.com/questions/1722905/fileloadexception-with-new-version-of-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!