In order to get a SQL CLR assembly to work in UNSAFE mode, I had to sign the assembly with a key. Another project in my solution references that assembly to use some of its functionality. During a runtime, when a call to a function inside a signed assembly is made, a TargetInvocationException is thrown with the following message:
Could not load file or assembly 'MyAssembly, Version=12.2.1.3, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Is there a way to have .NET load the right assembly/by pass the exception? Edit: After running fusion on the main assembly, it appears that it tries to load MyAssembly twice each time with a different publickeytoken. The first load succeeds, but the second fails. And the error message references the publickeytoken of the failed load. Why is trying to do it twice? Here is the text of the error:
The operation failed.
Bind result: hr = 0x80131040. No description available.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
Running under executable C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\WebDev.WebServer20.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = MyAssembly, Version=12.3.2.148, Culture=neutral, PublicKeyToken=5ad1afbaab228075
(Fully-specified)
LOG: Appbase = file:///C:/inetpub/wwwroot/MySolution converted to 2010/MyApplication/
LOG: Initial PrivatePath = C:\inetpub\wwwroot\MySolution converted to 2010\MyApplication\bin
LOG: Dynamic Base = C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\8365e84e
LOG: Cache Base = C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\8365e84e
LOG: AppName = d8cf427a
Calling assembly : (Unknown).
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\inetpub\wwwroot\MySolution converted to 2010\MyApplication\web.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Post-policy reference: MyAssembly, Version=12.3.2.148, Culture=neutral, PublicKeyToken=5ad1afbaab228075
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/root/8365e84e/d8cf427a/MyAssembly.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/root/8365e84e/d8cf427a/MyAssembly/MyAssembly.DLL.
LOG: Attempting download of new URL file:///C:/inetpub/wwwroot/MySolution converted to 2010/MyApplication/bin/MyAssembly.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\inetpub\wwwroot\MySolution converted to 2010\MyApplication\bin\MyAssembly.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: MyAssembly, Version=12.3.2.148, Culture=neutral, PublicKeyToken=7a45b8ac095ea0f9
WRN: Comparing the assembly name resulted in the mismatch: PUBLIC KEY TOKEN
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
Thanks!
PublicKeyToken=null
It is looking for an assembly without a strong name. You need to update the assembly reference (remove and add it again) and rebuild the project.