简体   繁体   English

使用自定义部分时,为什么必须在我的app.config中指定程序集而不是为web.config指定程序集

[英]Why do I have to specify the assembly in my app.config but not for my web.config when using custom sections

I have custom sections in my project. 我的项目中有自定义部分。 The following line works for my Web API project from the web.config: 以下行适用于来自web.config的Web API项目:

...
  <sectionGroup name="Project.Models">
    <section name="product" type="Project.Models.Configuration.ProductSettings" />
  </sectionGroup>
</configSections>  
<Project.Models>
   <product id="1" />    
</Project.Models>

When I run my unit tests, I get the following error: 运行单元测试时,出现以下错误:

System.Configuration.ConfigurationErrorsException : An error occurred creating the configuration section handler for Project.Models/product: Could not load type 'Project.Models.Configuration.ProductSettings' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. System.Configuration.ConfigurationErrorsException:在为Project.Models / product创建配置节处理程序时发生错误:无法从程序集“ System.Configuration,版本= 4.0.0.0,文化=中性”中加载类型“ Project.Models.Configuration.ProductSettings” ,PublicKeyToken = b03f5f7f11d50a3a'。 Could not load type 'Project.Models.Configuration.ProductSettings' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. 无法从程序集“ System.Configuration,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a”中加载类型“ Project.Models.Configuration.ProductSettings”。

Why do I have to specify the assembly name when referencing this from my unit tests app.config? 从单元测试app.config引用此名称时,为什么必须指定程序集名称? This resolved the issue, but not sure why it's needed. 这解决了问题,但不确定为什么需要它。

<section name="product" type="Project.Models.Configuration.ProductSettings, Project.Models" />

It depends on the host that executes your code. 这取决于执行代码的主机。

Without extra plumbing you'll find that in the inner workings of the Configuration namespace the type attribute is fed into the static method Type.GetType(string typeName) . 无需额外检查,您会发现在Configuration命名空间的内部工作中,将type属性输入到静态方法Type.GetType(string typeName)

For the typeName parameter you'll find in its description: 对于typeName参数,您可以在其说明中找到:

If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace. 如果类型在当前正在执行的程序集中或在Mscorlib.dll中,则只需提供其名称空间限定的类型名称即可。

The key part is currently executing assembly . 关键部分是当前正在执行的汇编 That seems to be never the case for normal appdomains, and thus for the application that runs your unit-test (which I assume is VS). 对于普通的应用程序域,对于运行单元测试(我认为是VS)的应用程序,似乎从来都不是这样。

The ASP.NET web hosting on the other hand provides an internal HttpConfigurationSystem class that re-implements the calls to GetSection . 另一方面,ASP.NET Web托管提供了一个内部HttpConfigurationSystem类,该类重新实现了对GetSection的调用。 It is a little hard to follow but it looks like an internal class BuildManager loads all assemblies and iterate over all types, to find one that matches. 很难理解,但看起来内部类BuildManager会加载所有程序集并遍历所有类型,以找到匹配的类型。

This explains the difference in behavior. 这解释了行为上的差异。 It is adviced to always specifiy the assemblyname. 建议始终指定程序集名称。 In the asp.net scenario, if the assemblyname is present in the type parameter it short-circuits to the Type.GetType call which prevents the loading and inspection of all dll's in the bin folder of your webapp. 在asp.net方案中,如果assemblyname存在于type参数中,则它会短路到Type.GetType调用,从而阻止加载和检查Webapp的bin文件夹中的所有dll。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM