【问题标题】:TypeInitializationException when setting ServicePointManager.ServerCertificateValidationCallback with tracing off设置 ServicePointManager.ServerCertificateValidationCallback 并关闭跟踪时出现 TypeInitializationException
【发布时间】:2015-05-20 11:11:13
【问题描述】:

我在尝试设置时遇到未处理的 TypeInitializationException 异常

ServicePointManager.ServerCertificateValidationCallback += (a, b, c, d) => true;

当我的 app.config 中有以下内容(取自 MSDN)时,就会发生异常。与下面的配置和 MSDN 上的配置的唯一区别是我将 System.NetSystem.Net.CacheSystem.Net.SocketsSystem.Net.WebSockets 的日志记录设置为 Off。我只有在所有四个都关闭时才看到问题。

这个配置有问题还是我遗漏了什么?

<system.diagnostics>
  <sources>
    <source name="System.Net" tracemode="includehex" maxdatasize="1024">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.Cache">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.Http">
      <listeners>
        <add name="System.Net "/>
      </listeners>
    </source>
    <source name="System.Net.Sockets">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.WebSockets">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
  </sources>
  <switches>
    <add name="System.Net" value="Off"/>
    <add name="System.Net.Cache" value="Off"/>
    <add name="System.Net.Http" value="Verbose"/>
    <add name="System.Net.Sockets" value="Off"/>
    <add name="System.Net.WebSockets" value="Off"/>
  </switches>
  <sharedListeners>
    <add name="System.Net"
         type="System.Diagnostics.TextWriterTraceListener"
         initializeData="network.log"
    />
  </sharedListeners>
  <trace autoflush="true"/>
</system.diagnostics>

这是异常的堆栈跟踪

at System.Net.ServicePointManager.get_ServerCertificateValidationCallback()
at test.Program.Main(String[] args) in c:\Users\Eoin\Documents\Visual Studio 2013\Projects\test\test\Program.cs:line 12
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

有一个内部异常,也是 TypeInitializationException 类型,调用堆栈仅包含 at System.Net.ServicePointManager..cctor(),消息是“'System.Net.ComNetOS' 的类型初始化程序引发了异常。”。

这有一个ConfigurationErrorsException 类型的最终内部异常。消息是“在 sharedListeners 部分中不存在侦听器‘System.Net’。”并且堆栈跟踪是

at System.Diagnostics.ListenerElement.GetRuntimeObject()
at System.Diagnostics.ListenerElementsCollection.GetRuntimeObject()
at System.Diagnostics.TraceSource.Initialize()
at System.Net.Logging.InitializeLogging()
at System.Net.Logging.get_On()
at System.Net.ComNetOS..cctor()

【问题讨论】:

  • 你能发布异常堆栈跟踪吗?

标签: c# .net system.net system.net.sockets system.net.websockets


【解决方案1】:

System.Net.Http 的侦听器名称中有一个空格。

<source name="System.Net.Http">
  <listeners>
    <add name="System.Net "/>
  </listeners>
</source>

这将尝试查找名为"System.Net " 的侦听器,但当然您只有一个名为"System.Net" 的侦听器。只需删除空格,您就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-14
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多