1

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

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

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

这个配置有问题还是我错过了其他东西?

<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' 的类型初始化程序引发了异常。”。

这有一个 type 的最终内部异常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()
4

1 回答 1

1

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

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

这将尝试找到一个名为的侦听器"System.Net ",但当然您只有一个名为的侦听器"System.Net"。只需删除空间,你就是金色的。

于 2015-12-25T21:44:41.617 回答