1

我得到一個未處理的異常TypeInitializationException當我嘗試設置TypeInitializationException設置ServicePointManager.ServerCertificateValidationCallback當跟蹤功能

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

當我有以下的例外發生在我的應用程序(從MSDN拍攝)的.config。從下面的配置和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引發了一個例外。「

這有最終的內部例外類型ConfigurationErrorsException。該消息是「Listener'System.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() 
+0

你能發佈異常堆棧跟蹤嗎? –

回答

1

有在監聽System.Net.Http的名稱空間。

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

這將嘗試找到一個名爲"System.Net "當然你只能有一個名爲"System.Net"監聽器。只要刪除空間,你是金。