machine.config 中忽略 .NET assemblyBinding 配置
我遇到的情况是,我需要能够根据程序集的部分名称在 GAC 中加载程序集。 为了做到这一点,我已将以下内容添加到我的 app.config 文件中:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="MyAssembly"
fullName= "MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef"/>
</assemblyBinding>
</runtime>
这完全按照我想要的方式工作。 但是,如果我将相同的元素放入 machine.config 文件中,它似乎会被忽略,并且在尝试加载 MyAssembly 时会收到 FileNotFoundExceptions。
以下是当元素在我的app.config中时,绑定成功的程序集绑定日志:
LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Documents and Settings\jon_scheiding\My Documents\Source\Testing\Test Projects 1\Cmd\bin\Debug\Testers.Cmd.vshost.exe.config LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config. LOG: Partial reference qualified from config file. New reference: MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef. LOG: Post-policy reference: MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef LOG: Found assembly by looking in the GAC. LOG: Binding succeeds. Returns assembly from C:\WINDOWS\assembly\GAC_MSIL\MyAssembly\1.0.0.0__b20f4683c1030dbd\MyAssembly.dll. LOG: Assembly is loaded in default load context.
与我的配置在machine.config中时,绑定失败时的日志对比:
LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Documents and Settings\jon_scheiding\My Documents\Source\Testing\Test Projects 1\Cmd\bin\Debug\Testers.Cmd.vshost.exe.config LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config. LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind). LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly.DLL. LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly/MyAssembly.DLL. LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly.EXE. LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly/MyAssembly.EXE. LOG: All probing URLs attempted and failed.
问题似乎出在第四行, “目前政策尚未应用于参考。” 但是,我找不到关于此消息的含义或如何解决它的文档。
我怎样才能让框架识别我的
提前致谢!
I have a situation where I need to be able to load assemblies in the GAC based on their partial names. In order to do this I have added the following to my app.config file:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="MyAssembly"
fullName= "MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef"/>
</assemblyBinding>
</runtime>
This works exactly the way I want it to. However, if I place the same element in my machine.config file, it seems to be ignored, and I get FileNotFoundExceptions when trying to load MyAssembly.
The following is the assembly binding log when the element is in my app.config, and the bind succeeds:
LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Documents and Settings\jon_scheiding\My Documents\Source\Testing\Test Projects 1\Cmd\bin\Debug\Testers.Cmd.vshost.exe.config LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config. LOG: Partial reference qualified from config file. New reference: MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef. LOG: Post-policy reference: MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef LOG: Found assembly by looking in the GAC. LOG: Binding succeeds. Returns assembly from C:\WINDOWS\assembly\GAC_MSIL\MyAssembly\1.0.0.0__b20f4683c1030dbd\MyAssembly.dll. LOG: Assembly is loaded in default load context.
Contrast that with the log when my configuration is in machine.config, and the bind fails:
LOG: This bind starts in default load context. LOG: Using application configuration file: C:\Documents and Settings\jon_scheiding\My Documents\Source\Testing\Test Projects 1\Cmd\bin\Debug\Testers.Cmd.vshost.exe.config LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config. LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind). LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly.DLL. LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly/MyAssembly.DLL. LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly.EXE. LOG: Attempting download of new URL file:///C:/Documents and Settings/jon_scheiding/My Documents/Source/Testing/Test Projects 1/Cmd/bin/Debug/MyAssembly/MyAssembly.EXE. LOG: All probing URLs attempted and failed.
The problem seems to be the fourth line, "Policy not being applied to reference at this time." However, I can find very little documentation on what this message means, or how to address it.
How can I get the framework to recognize my <runtime> element?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该框架不会从 machine.config 读取 QualifyAssebmly 配置,它仅从应用程序配置文件中读取它。
该框架确实可以识别您的 runtim 元素,但它无法识别 QualifyAssembly 元素。
The framework won't read qualifyAssebmly configuration from machine.config, it only reads it from your application configuration file.
The framework does recognize your runtim element, however it does not recognize the qualifyAssembly element.
很久以后我才回到这个需求,因为我们在 GAC 中有一些程序集,并且需要像 NHibernate 这样的东西来解决它们。 下面的代码允许我们将它们添加到 machine.config 中,而不是将所有这些 QualifyAssembly 元素添加到每个 app.config 中。
在大多数情况下,我们通过像这样使用它来限制它的范围:
它也可以在应用程序启动代码或 global.asax 中使用,并在应用程序的生命周期中持续存在,如下所示:
I came back to this need much later, as we have some assemblies in the GAC and things like NHibernate needed to resolve them. Rather than adding all these qualifyAssembly elements to every app.config, the following code allows us to add them to our machine.config.
In most cases, we limit the scope of it, by using it like this:
It can also be used in app startup code or global.asax, and persist for the lifetime of the application, like so: