Loading renamed C# assembly throws FileNotFoundException
I have an C# assembly that is referenced by a C# application.
Because of our coding standards, there is a rule where debug DLLs are postfixed by a "d" (e.g. ProjectA.dll
becomes ProjectAd.dll
). When I add a reference to the renamed DLL to the application, it builds successfully, but throws a FileNotFoundException
upon execution.
The error thrown is as follows:
System.IO.FileLoadException: Could not load file or assembly 'ProjectA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=49df7f988e86ed92' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'ProjectA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=49df7f988e86ed92'
The assembly manager also chimes in with a warning and error:
WRN: Comparing the assembly name resulted in the mismatch: PUBLIC KEY TOKEN
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing 开发者_开发知识库terminated.
From the error message, it looks like it is looking for an assembly without the d
postfix.
BTW, there is a C++/CLI assembly that is referenced by the same application. It has a d
appended to the DLL, but viewing the properties of the reference in VS2005 shows that the security identity has the d
correctly appended. The C# reference does not have the d
appended in the properties window.
What do I have to do to get this working properly for the debug C# assemblies? I've tried modifying the entries in AssemblyInfo.cs to no avail. Is this something that a manifest file would resolve?
Unfotunatelly you cannot achieve this by only renaming the assembly.
The name of an assembly is written into its meta data by its compilation. When you later change its file name you do not actually change the name in its meta data.
Then by the second compilation the name of the referenced assembly will be read from its meta data and written to the newly built assembly.
In runtime the CLR will search for the referenced assembly base on the name int the meta data of the referencing assembly. However, it will not find it in any of the probe paths and thus will throw an exception FileNotFound.
You can tackle this problem by editing the project file of the referenced assembly. You do that by right clicking the project properties in the solution explorer and selecting unload project. Then right click the unloaded project and select edit project. Paste this right before the first tag ItemGroup
...
<PropertyGroup>
<AssemblyName Condition="'$(Configuration)' == 'Debug'">$(AssemblyName)d</AssemblyName>
</PropertyGroup>
<ItemGroup>
...
This is a conditional AssemblyName property of msbuild script. It will be applied only when you have your configuration set to Debug value and will take the above defined AssemblyName and add 'd' to it.
Now you will have the name with 'd' in both the file name and the meta data. When you change back to the Release configuration the property will be omitted.
精彩评论