在 Azure WebJob 上读取自定义 ConfigurationSection 时出错

Error reading Custom ConfigurationSection on an Azure WebJob

我有一个控制台应用程序,它有一个 app.config 文件,其中包含多个 ConfigurationSections

一个示例配置部分是:

   public class ThrottlingConfigurationSection : ConfigurationSection, IThrottlingConfigurationSection
{
    private const string MillisecondsToThrottleProperty = "millisecondsToThrottle";

    /// <summary>
    /// Gets or sets the MillisecondsToThrottle.
    /// </summary>
    [ConfigurationProperty(MillisecondsToThrottleProperty, IsRequired = true)]
    public int MillisecondsToThrottle
    {
        get
        {
            return (int)this[MillisecondsToThrottleProperty];
        }

        set
        {
            this[MillisecondsToThrottleProperty] = value;
        }
    }
}

它在 app.config 文件中配置如下:

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <
        <section type="FULLNAMESPACE.ThrottlingConfigurationSection, FULLASSEMBLYNAME" name="fulltextRepositorySection" />
...
</configSections>
<fulltextRepositorySection millisecondsToThrottle="10" />
...
</configuration>

我能够将 webjob 发布到 Azure,并且在 webjob 日志中我看到以下错误:

[09/13/2016 12:13:06 > b4a151: ERR ] Unhandled Exception: System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for fulltextRepositorySection: Could not load file or assembly 'NAMESPACE.WEBJOBNAME' or one of its dependencies. The system cannot find the file specified. (D:\local\Temp\jobs\continuous\WEBJOBNAME\bcbcaozj.d0b\XXX.XXX.XXX.XXX.WEBJOBNAME.exe.config line 4) ---> System.IO.FileNotFoundException: Could not load file or assembly 'XXX.XXX.XXX.XXX.WEBJOBNAME' or one of its dependencies. The system cannot find the file specified.

[09/13/2016 12:13:06 > b4a151: ERR ] at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)

[09/13/2016 12:13:06 > b4a151: ERR ] at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)

[09/13/2016 12:13:06 > b4a151: ERR ] at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)

[09/13/2016 12:13:06 > b4a151: ERR ] at System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord)

[09/13/2016 12:13:06 > b4a151: ERR ] at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere) [09/13/2016 12:13:06 > b4a151: ERR ] --- End of inner exception stack trace ---

[09/13/2016 12:13:06 > b4a151: ERR ] at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)

[09/13/2016 12:13:06 > b4a151: ERR ] at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)

[09/13/2016 12:13:06 > b4a151: ERR ] at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)

[09/13/2016 12:13:06 > b4a151: ERR ] at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)

[09/13/2016 12:13:06 > b4a151: ERR ] at System.Configuration.ConfigurationManager.GetSection(String sectionName)

[09/13/2016 12:13:06 > b4a151: ERR ] at NAMESPACE.Common.Configuration.ConfigurationLoader.LoadConfigurationSection[TSection](String sectionName) in C:\Install\agent-03_work\s\Src\PROJECTNAME\Common\Configuration\ConfigurationLoader.cs:line 28

我似乎能够从网络作业中读取应用程序设置和连接字符串以外的任何内容。关于如何这样做的任何想法?有些东西可以从配置部分转换为应用程序设置(如示例),但也有像 nlog 这样的东西需要配置部分。这对 webjobs 来说是可能的还是我应该只使用 VM?

System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for fulltextRepositorySection: Could not load file or assembly 'NAMESPACE.WEBJOBNAME' or one of its dependencies. The system cannot find the file specified.

一般来说,这意味着 .NET 程序集加载程序无法找到您在配置文件的 <section type="FULLNAMESPACE.ThrottlingConfigurationSection, FULLASSEMBLYNAME" name="fulltextRepositorySection" /> 中定义的名为 "FULLASSEMBLYNAME" 的程序集。请确保 webjob 在您这边按预期工作。

(D:\local\Temp\jobs\continuous\WEBJOBNAME\bcbcaozj.d0b\XXX.XXX.XXX.XXX.WEBJOBNAME.exe.config line 4) ---> System.IO.FileNotFoundException: Could not load file or assembly 'XXX.XXX.XXX.XXX.WEBJOBNAME' or one of its dependencies. The system cannot find the file specified.

请尝试通过KUDU调试控制台检查错误消息中提到的路径中是否存在特定文件或程序集。

此外,我测试了自定义 ConfigurationSection,它在我这边和 Azure 上都成功地定义了 in/outside 与 WebJob 项目相同的程序集。这是代码示例,您可以参考它。

App.config

    <configSections>
      <!--ThrottlingConfigurationSection is defined in the same assembly within WebJob project which is called WebJobWithCustomConfig-->
      <section name="FulltextRepositorySection" type="WebJobWithCustomConfig.ThrottlingConfigurationSection,WebJobWithCustomConfig" />
      <!--ThrottlingConfigurationSection is defined in another assmebly outside of the WebJob project-->
      <section name="BruceConfigurationSection" type="Bruce.Configuration.ThrottlingConfigurationSection,Bruce.Configuration" />
    </configSections>
    <FulltextRepositorySection millisecondsToThrottle="10" />
    <BruceConfigurationSection millisecondsToThrottle="20" />

Functions.cs

[NoAutomaticTrigger]
public static void ManualTrigger(int value)
{
    ThrottlingConfigurationSection innerSection = (ThrottlingConfigurationSection)ConfigurationManager.GetSection("FulltextRepositorySection");
    Console.WriteLine("FulltextRepositorySection.MillisecondsToThrottle:{0}", innerSection.MillisecondsToThrottle);

    Bruce.Configuration.ThrottlingConfigurationSection outerSection= (Bruce.Configuration.ThrottlingConfigurationSection)ConfigurationManager.GetSection("BruceConfigurationSection");
    Console.WriteLine("BruceConfigurationSection.MillisecondsToThrottle:{0}", outerSection.MillisecondsToThrottle);
}