【问题标题】:Error reading Custom ConfigurationSection on an Azure WebJob读取 Azure WebJob 上的自定义配置部分时出错
【发布时间】:2023-03-27 13:00:01
【问题描述】:

我有一个控制台应用程序,它有一个 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] 未处理异常:System.Configuration.ConfigurationErrorsException:为 fulltextRepositorySection 创建配置节处理程序时出错:无法加载文件或程序集“NAMESPACE.WEBJOBNAME”或其依赖项之一。该系统找不到指定的文件。 (D:\local\Temp\jobs\continuous\WEBJOBNAME\bcbcaozj.d0b\XXX.XXX.XXX.XXX.WEBJOBNAME.exe.config 第 4 行)---> System.IO.FileNotFoundException:无法加载文件或程序集'XXX.XXX.XXX.XXX.WEBJOBNAME' 或其依赖项之一。系统找不到指定的文件。

[09/13/2016 12:13:06 > b4a151: ERR ] 在 System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost 主机,字符串 typeString,布尔 throwOnError)

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

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

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

[09/13/2016 12:13:06 > b4a151: ERR ] 在 System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere) [09/13/2016 12:13:06 > b4a151: ERR ] --- 内部异常堆栈跟踪结束 ---

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

[2016 年 9 月 13 日 12:13:06 > b4a151:ERR ] 在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串 configKey,布尔 getLkg,布尔 checkPermission,布尔 getRuntimeObject,布尔 requestIsHere,对象和结果,对象和 resultRuntimeObject)

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

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

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

[09/13/2016 12:13:06 > b4a151: ERR ] 在 C:\Install\agent-03_work\10\s\ 中的 NAMESPACE.Common.Configuration.ConfigurationLoader.LoadConfigurationSection[TSection](String sectionName) Src\PROJECTNAME\Common\Configuration\ConfigurationLoader.cs:第 28 行

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

【问题讨论】:

  • 它抱怨的程序集是否无法加载您使用 exe 部署的程序集?
  • 另外,请尝试就地运行您的 WebJob 以查看是否有所作为。见this page

标签: .net azure azure-webjobs configurationsection


【解决方案1】:

System.Configuration.ConfigurationErrorsException:为 fulltextRepositorySection 创建配置节处理程序时出错:无法加载文件或程序集“NAMESPACE.WEBJOBNAME”或其依赖项之一。该系统找不到指定的文件。

通常,这意味着 .NET 程序集加载器找不到您在配置文件的 &lt;section type="FULLNAMESPACE.ThrottlingConfigurationSection, FULLASSEMBLYNAME" name="fulltextRepositorySection" /&gt; 中定义的名为“FULLASSEMBLYNAME”的程序集。请确保网络作业在您身边按预期工作。

(D:\local\Temp\jobs\continuous\WEBJOBNAME\bcbcaozj.d0b\XXX.XXX.XXX.XXX.WEBJOBNAME.exe.config 第 4 行) ---> System.IO.FileNotFoundException: 无法加载文件或程序集“XXX.XXX.XXX.XXX.WEBJOBNAME”或其依赖项之一。系统找不到指定的文件。

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

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

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多