使用已签名的程序集会导致 TargetInitationException

发布于 2024-12-05 16:44:00 字数 2853 浏览 1 评论 0原文

为了让 SQL CLR 程序集在 UNSAFE 模式下工作,我必须使用密钥对该程序集进行签名。我的解决方案中的另一个项目引用该程序集以使用其某些功能。在运行时期间,当调用已签名程序集中的函数时,会引发 TargetInitationException 并显示以下消息:

无法加载文件或程序集“MyAssembly,Version=12.2.1.3,Culture=neutral,PublicKeyToken=null”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (HRESULT 异常:0x80131040)

有没有办法让 .NET 加载正确的程序集/绕过异常? 编辑: 在主程序集上运行 fusion 后,它似乎每次都会尝试使用不同的 publickeytoken 加载 MyAssembly 两次。第一次加载成功,但第二次失败。并且错误消息引用失败加载的 publickeytoken。为什么要尝试做两次? 这是错误的文本:

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.

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2024-12-12 16:44:00

PublicKeyToken=null

它正在寻找没有强名称的程序集。您需要更新程序集引用(删除并再次添加)并重建项目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文