.net - 读取 Azure WebJob 上的自定义配置部分时出错

标签 .net azure azure-webjobs configurationsection

我有一个控制台应用程序,它的 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\10\s\Src\PROJECTNAME\Common\Configuration\ConfigurationLoader.cs:line 28

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

最佳答案

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”的程序集。你的配置文件。请确保网络作业在您这边按预期工作。

(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,该部分在同一程序集内部/外部与 WebJob 项目在我这边和 Azure 上都成功定义。这是代码示例,您可以引用。

应用程序配置

    <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" />

函数.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);
}

关于.net - 读取 Azure WebJob 上的自定义配置部分时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39484468/

相关文章:

.net - 为什么子 AppDomain 中的 StackOverflowException 会终止父 AppDomain?

c# - 使用 .Net 调用电话和留下语音消息?

c# - 我们可以继续在服务器端按需创建 Azure 共享访问签名吗?

azure - Azure WebJobs SDK 3.0 中 'Settings.job' 和 'TimerTrigger' 之间有什么区别

azure - Azure WebJobs sdk 是否可以按需/调度程序读取预先存在的 blob?

c# - 代码中的访问控制属性

c# - 代码分析规则集的层次结构?

java - 从 Azure Blob 存储下载 Blob 列表

c# - 如何从 Azure 应用程序配置获取 .crt 和 .key 文件

azure - Azure WebJobs 中的物理路径