[英]Where can I set a try-catch block when debugging a `WCF Service Library` (i.e. where is the entry point / Main()-method)
這是我的情況:我在 Win 10 64 位機器上使用帶有 .NET Framework 4.6.1 的 Visual Studio 2015 來構建 WCF 服務。
在我的解決方案中,我有幾種不同的項目類型,主要是普通的 C# Class Library
(類庫)、一些C++
-dll 引用,當然還有WCF Service Library
本身。
在我繼續之前,我想說這是我第一次與 WCF 約會......
這個 SO 問題正在解決一個類似的問題, WCF 服務的啟動方法在哪里? ,但是由於我在幾年后提出原始問題時正在處理WCF Service Library
,並且原始問題未使用相同的項目類型,因此我相信(?)答案不存在適合我的問題。 至少在調試時不是?
在我的解決方案中,我已將WCF Service Library
項目設置為啟動項目。 當按下 F5 鍵(用於開始)時,這就是我得到的:
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.Assembly.GetTypes()
at Microsoft.Tools.SvcHost.ServiceHostHelper.LoadServiceAssembly(String svcAssemblyPath)
好的,通常,在普通的 C# Console Application
,我會在Main()
方法中設置一個 try-catch-block 以檢查我的問題。 但是我的WCF Service Library
似乎沒有這樣的。
我的問題:如何找出LoaderException
屬性?
編輯 1:我嘗試了@user497745 的答案(兩個建議),第一個幫助了我:
WCF Class Library
添加部分來嘗試使用診斷跟蹤的第二個建議。App.config
一部分:<system.serviceModel>\n <診斷>\n <消息記錄\n logEntireMessage="true"\n logMalformedMessages="false"\n logMessagesAtServiceLevel="true"\n logMessagesAtTransportLevel="false"\n maxMessagesToLog="3000"\n maxSizeOfMessageToLog="2000"/>\n </診斷>\n <行為>\n <服務行為>\n <行為名稱="HemsamaritenServiceBehavior">\n <serviceMetadata httpGetEnabled="true"/>\n <serviceDebug includeExceptionDetailInFaults="true"/>\n </行為>\n </serviceBehaviors>\n </行為>\n <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />\n <服務>\n <!-- 本節為新配置模型可選\n 在 .NET Framework 4 中引入。 -->\n <service name="WCFServiceLibrary.HemsamaritenDuplexService"\n behaviorConfiguration="HemsamaritenServiceBehavior">\n <主機>\n <基地址>\n <add baseAddress="http://localhost:8000/Hemsamariten/service"/>\n </基地址>\n </host>\n <!-- 這個端點暴露在主機提供的基地址:http://localhost:8000/Hemsamariten/service -->\n <端點地址=""\n 綁定=“wsDualHttpBinding”\n contract="WCFServiceLibrary.Interfaces.IHemsamaritenDuplexService" />\n <!-- mex 端點暴露在 http://localhost:8000/Hemsamariten/service/mex -->\n <端點地址="mex"\n 綁定=“mexHttpBinding”\n 合約="IMetadataExchange" />\n </服務>\n </服務>\n </system.serviceModel>\n\n <系統診斷>\n <來源>\n <source name="System.ServiceModel" switchValue="All"\n 傳播活動=“真”>\n <聽眾>\n <添加名稱="xml" />\n </聽眾>\n </translation>\n <source name="System.ServiceModel.MessageLogging" switchValue="All">\n <聽眾>\n <添加名稱="xml" />\n </聽眾>\n </translation>\n </來源>\n <共享監聽器>\n <add initializeData="C:\\Users\\haunsTM\\Desktop\\WinService\\debuglog.svclog" type="System.Diagnostics.XmlWriterTraceListener"\n 名稱="xml" />\n </sharedListeners>\n <trace autoflush="true" />\n </system.diagnostics>
C:\\Users\\haunsTM\\Desktop\\WinService\\debuglog.svclog
在運行后是空的。因此,如果您試圖中斷調試器以在錯誤發生時捕獲錯誤,我會推薦一種不同的方法。
任何一個:
轉到 DEBUG > Exceptions 並選擇 Common Language Runtime Exceptions > System.Reflection 並檢查您獲得的確切異常類型。 我不確定您是否還需要在調試 > 選項中取消選中“僅啟用我的代碼”。 也許檢查“啟用 .NET Framework 源步進”。 然后當異常發生時,Visual Studio 應該中斷並允許您像查看任何異常一樣查看異常詳細信息。
通過將以下內容添加到 WCF 類庫的 App.config 文件來添加診斷跟蹤。 然后嘗試啟動庫,在出現錯誤后,停止調試器並在 SvcTraceViewer 中打開生成的跟蹤文件(位於您在下面選擇的任何路徑和文件名中)。 您可以打開 Visual Studio 開發人員命令提示符並從那里啟動它。
<system.serviceModel> <diagnostics> <messageLogging logEntireMessage="true" logMalformedMessages="false" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="false" maxMessagesToLog="3000" maxSizeOfMessageToLog="2000"/> </diagnostics> </system.serviceModel> <system.diagnostics> <sources> <source name="System.ServiceModel" switchValue="All" propagateActivity="true"> <listeners> <add name="xml" /> </listeners> </source> <source name="System.ServiceModel.MessageLogging" switchValue="All"> <listeners> <add name="xml" /> </listeners> </source> </sources> <sharedListeners> <add initializeData="[SOME_PATH]\\[SOME_FILENAME].svclog" type="System.Diagnostics.XmlWriterTraceListener" name="xml" /> </sharedListeners> <trace autoflush="true" /> </system.diagnostics>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.