Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 应用程序池回收后,自定义配置节为空_C#_Asp.net_.net_Iis - Fatal编程技术网

C# 应用程序池回收后,自定义配置节为空

C# 应用程序池回收后,自定义配置节为空,c#,asp.net,.net,iis,C#,Asp.net,.net,Iis,最近,在读取配置文件时,我们在生产中遇到了以下问题 对象引用未设置为对象的实例。 (C:\applications\SampleWebsite\web.config第105行)位于 System.Configuration.BaseConfigurationRecord.EvaluateOne(字符串[]) 键,分区输入,布尔值不受信任,FactoryRecord factoryRecord、SectionRecord、Object parentResult)位于 System.Configur

最近,在读取配置文件时,我们在生产中遇到了以下问题

对象引用未设置为对象的实例。 (C:\applications\SampleWebsite\web.config第105行)位于 System.Configuration.BaseConfigurationRecord.EvaluateOne(字符串[]) 键,分区输入,布尔值不受信任,FactoryRecord factoryRecord、SectionRecord、Object parentResult)位于 System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord、SectionRecord、Object parentResult、, 布尔getLkg,布尔getRuntimeObject,对象和结果,对象& resultRuntimeObject)位于 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串 configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,布尔请求服务器,对象和结果,对象& resultRuntimeObject)位于 System.Configuration.BaseConfigurationRecord.GetSection(字符串 配置密钥)在 System.Configuration.ConfigurationManager.GetSection(字符串 部门名称)在 SampleWebsite.Configuration.CustomConfigurationSection.get_Current() 在里面 c:\Builds\1\SampleWebsite\Sources\Source\SampleWebsite\Configuration\CustomConfigurationSection.cs:line 69在 SampleWebsite.Security.AuthorizationManager.CheckAccess(AuthorizationContext (上下文)在 c:\Builds\1\SampleWebsite\Sources\Source\SampleWebsite\Security\AuthorizationManager.cs:line 161 at System.IdentityModel.Services.ClaimsPrincipalPermission.Demand()位于 System.Security.PermissionSet.DemandNonCAS()位于 中的SampleWebsite.CustomController.Test() c:\Builds\1\SampleWebsite\Sources\Source\SampleWebsite\Controllers\CustomController.cs:line 125 at lambda_方法(闭包、对象、对象[])at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c_DisplayClass13.b_c(对象 实例,对象[]方法参数)位于 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象 实例,对象[]参数),位于 System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](函数'1 func,CancellationToken CancellationToken)

我们正在Windows Server 2012的IIS 8上使用.NET 4.5。该问题有时(并非总是!)发生在回收应用程序池之后。如果出现问题,我们必须停止应用程序池并启动应用程序池。回收应用程序池没有帮助,重新启动网站也没有帮助

配置文件中没有任何更改。我们注意到的唯一真正的区别是,对网站本身的请求更多

由于我们最初认为这可能是一种竞争条件,因此我们已将应用程序池安排在确定没有向网站发送请求的特定时间进行回收

循环时间是在向此站点发送请求的应用程序关闭时以及再次启动之前选择的

应用程序池设置了以下配置选项:

  • 自动启动:true
  • 启动模式:始终运行
  • 立即启动应用程序池:true
该网站设置了以下选项:

  • 已启用预加载:true
  • 自动启动:true
以前有人遇到过这个问题吗

任何帮助都将不胜感激

编辑:

这基本上就是自定义配置部分代码中的内容:

public sealed class CustomConfigurationSection : ConfigurationSection
{
    private static CustomConfigurationSection _current;

    public static CustomConfigurationSection Current
    {
        get
        {
            return _current
                ?? (_current = ConfigurationManager.GetSection(CustomSectionName) as CustomConfigurationSection);
        }
    }
}

在回收应用程序池且当前字段(静态)为空且需要从配置文件中再次读取后,ConfigurationManager.GetSection()中会出现nullref。您正在使用的null合并运算符。将锁定变量添加到getter:

private static CustomConfigurationSection _current;
private static readonly object _lock = new object();

public static CustomConfigurationSection Current
{
    get
    {
        lock(_lock) {
            return _current
                ?? (_current = ConfigurationManager.GetSection(CustomSectionName) as CustomConfigurationSection);
        }
    }
}
也可能是
ConfigurationManager
在回收应用程序池后不久没有返回预期值(我看不出
CustomSectionName
是在哪里定义的)。可能值得在这里添加一个检查,以确保您实际返回了预期的部分

在CustomConfigurationSection的构造函数中设置静态属性也可能值得:

public sealed class CustomConfigurationSection : ConfigurationSection
{
    public CustomConfigurationSection() {
        Current = CurrentConfiguration.GetSection(CustomSectionName);
    }

    public static CustomConfigurationSection Current { get; private set; }
}

CustomConfigurationSection.cs
中有什么?@Soner OP知道nullref是什么。我们只是不知道为什么自定义配置部分在随机时间为空。99%的情况下,该部分被正确读取。有1%的时间它会无缘无故地呕吐,而且配置中根本没有任何更改file@RGraham,请参见问题中的编辑。这信息够了吗?+1用于_锁。这已经在dev分支中,但还没有投入生产。我们将重写getter,使其更加详细,并在强制转换之前检查并记录自定义部分的内容。谢谢!我们将运行几天,看看会有什么结果。这很难复制,因此需要一段时间来确认这是否是解决方案。实际上,确保我们得到正确部分的检查可能是无关紧要的。异常发生在System.Configuration.BaseConfigurationRecord.EvaluateOne(字符串[]键,SectionInput输入,Boolean isTrusted,FactoryRecord FactoryRecord,SectionRecord SectionRecord,Object parentResult)中,因此在Configuration manager逻辑的某个更深的部分中读取节。@StephaneT这是真的,在这种情况下,您可能需要将属性更改为basic
Current{get;set;}
,并在构造函数中设置静态值。这样,您就可以访问
CurrentConfiguration
而不是静态
ConfigurationManager
实例。我不知道您对
CurrentConfiguration
的意思是什么。@RGraham使用
CurrentConfiguration
ConfigurationManager
更安全吗?