在webrole的模型文件夹中定义辅助角色的app.config部分处理程序

时间:2014-08-18 17:16:31

标签: c# app-config azure-web-roles azure-worker-roles

这是我第一次为worker角色的app.config创建自定义配置节类。当我在WorkerRole1项目中定义自定义部分类时,我能够成功地为worker角色成功,并且最初是app.config部分:

<configSections>
  <sectionGroup name="environmentInfoGroup">
    <section name="environmentInfo" type="WorkerRole1.EnvironmentInfoSection, WorkerRole1" allowLocation="true" allowDefinition="Everywhere" />
  </sectionGroup>
</configSections>

但是,现在我将EnvironmentInfoSection类移动到MvcWebRole1.Models并将app.config更改为:

<configSections>
  <sectionGroup name="environmentInfoGroup">
    <section name="environmentInfo" type="MvcWebRole1.Models.EnvironmentInfoSection, MvcWebRole1.Models" allowLocation="true" allowDefinition="Everywhere" />
  </sectionGroup>
</configSections>

ConfigurationManager.GetSection("environmentInfoGroup/environmentInfo") as EnvironmentInfoSection;

我有一个例外

InnerException  {"An error occurred creating the configuration section handler for environmentInfoGroup/environmentInfo: Could not load file or assembly 'MvcWebRole1.Models' or one of its dependencies. The system cannot find the file specified. 

由于目前我的worker角色正在使用webRole中同一文件夹中的其他类,这是否意味着我无法在webRole和workerRole之间共享类以进行配置?或者有什么我错过了吗?

1 个答案:

答案 0 :(得分:0)

我找到了一些不能直接用作问题答案的东西。 但是,我所做的是在工作站和Web角色项目中定义了两个相同的EnvironmentInfoSection,在app.config和Web.config中定义了两个相同的配置部分。

我注意到在app.config中:

<configSections>
  <sectionGroup name="environmentInfoGroup">
    <section name="environmentInfo" type="WorkerRole1.EnvironmentInfoSection, WorkerRole1" allowLocation="true" allowDefinition="Everywhere" />
  </sectionGroup>
</configSections>

是正确的,但在Web.config(WebRole)

<configSections>
  <sectionGroup name="environmentInfoGroup">
    <section name="environmentInfo" type="WebRole1.Models.EnvironmentInfoSection" allowLocation="true" allowDefinition="Everywhere" />
  </sectionGroup>
</configSections>

是正确的方法。注意区别在于,在Web.config中,没有&#34;逗号&#34;在类型定义之后是命名空间。