当我尝试设置
时,我得到一个未处理的TypeInitializationException
异常ServicePointManager.ServerCertificateValidationCallback += (a, b, c, d) => true;
当我在我的app.config中获得以下内容(取自MSDN)时,会发生异常。与下面的配置和MSDN上的配置唯一的区别是,我已经将System.Net
, System.Net.Cache
, System.Net.Sockets
和System.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:UsersEoinDocumentsVisual Studio 2013ProjectstesttestProgram.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
类型的最终内部异常。信息是"监听系统"。Net"在sharedListeners部分中不存在",堆栈跟踪为
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()
System.Net.Http的监听器名称中有一个空格。
<source name="System.Net.Http">
<listeners>
<add name="System.Net "/>
</listeners>
</source>
这将尝试找到一个名为"System.Net "
的侦听器,但当然您只有一个名为"System.Net"
的侦听器。只要去掉空格,你就完美了。