Microsoft BizTalk Server 會大量使用 Web 服務,以搭配 SOAP 配接器使用,並在將協調流程發佈為 Web 服務時使用。 本主題提供一些您可以遵循的步驟來針對 Web 服務進行疑難解答,以及一些常見的 Web 服務問題,以及如何解決這些問題。
使用 .NET Framework 追蹤來擷取和記錄 Web 服務中的錯誤
.NET Framework System.Diagnostics.Trace 類別可用來擷取和寫入文字文件的錯誤。
若要使用 System.Diagnostics.Trace 類別來擷取錯誤,並將錯誤寫入文本檔
更新 Web 服務的 web.config 檔案,將 TRACE 編譯程式指示詞設定為 true ,並新增 TraceSwitch 值:
<?xml version="1.0"?> <configuration> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" compilerOptions="/d:TRACE" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="1" /> </compilers> </system.codedom> <system.diagnostics> <switches> <add name="WebSvcTraceSwitch" value="2" /> <!-- Set to 0, 1, 2, 3, or 4, which corresponds to TraceLevel.Off, TraceLevel.Error, TraceLevel.Warning TraceLevel.Info, and TraceLevel.Verbose. --> </switches> </system.diagnostics> </configuration>
備註
如果 TRACE 編譯程式指示詞未設定為 true ,則不會產生任何追蹤輸出。 TraceSwitch 值也可以在呼叫類別中設定,但為了方便起見,web.config 檔案在這裡設定。 將 TraceSwitch 值設定為適當的開發層級,並在開發完成後變更值,以減少或抑制追蹤輸出。
建立TraceSwitch和TextWriterTraceListener類別的實例,並在 Web 服務的 [WebMethod] 呼叫中使用 try...catch 區塊來捕捉錯誤,並將這些錯誤寫入追蹤輸出檔案。 例如,下列程式代碼會擷取嘗試將整數變數 s2 設定為物件變數 o2 的 Null 實例時所產生的錯誤:
using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Diagnostics; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { TraceSwitch WebSvcTraceSwitch = new TraceSwitch("WebSvcTraceSwitch", "Web Service Trace"); TextWriterTraceListener TestTracer = new TextWriterTraceListener("C:\\traceout.txt"); // Note that by default the local ASPNET account(IIS 5.x) or the // local NETWORK SERVICE account(IIS 6.0) needs write access to // this directory so that the instance of the // TextWriterTraceListener can write to the trace output file. ); public Service () { } [WebMethod] public string HelloWorld() { string h2 = "Hello World"; //object o2 = 1; object o2 = null; try { int s2 = (int)o2; //Error if o2 set to null return h2; } catch(Exception e) { Trace.Listeners.Add(TestTracer); Trace.WriteLineIf(WebSvcTraceSwitch.Level = TraceLevel.Warning, "Exception caught: " + e.Message); //Writes to the trace file if specified TraceLevel switch value (in web.config) >= 2 TestTracer.Dispose(); return "An error occurred in the Web service, please contact the web server administrator."; } } }
備註
此程式代碼是您在 Visual Studio Microsoft 中建立新 ASP.Net Web 服務 專案時所產生的預設 HelloWorld Web 服務修改版本。
備註
針對 Windows Vista,可能需要系統管理員許可權,才能將追蹤輸出檔案寫入根資料夾。
重建 Web 服務專案。 現在,如果在 Try 語句中發生錯誤,則會在 Catch 語句中處理例外狀況,並將錯誤寫入追蹤輸出檔。
已知問題
Web 服務會傳回 HTTP 404 (找不到檔案) 錯誤
問題
嘗試呼叫 Web 服務傳回 HTTP 404(找不到檔案)錯誤。
原因
如果未在裝載 Web 服務的 IIS 伺服器上安裝及/或啟用 ASP.NET,就會發生此錯誤。
解決辦法
請確定已安裝並啟用 ASP.NET。 如果未安裝 .NET Framework,請安裝 /或執行位於 IIS 伺服器的 %WinDir%\Microsoft.NET\Framework\vXXX.XXX\ 資料夾中的 aspnet_regiis.exe程式。
呼叫 Web 服務時,會發生「System.IO.FileNotFoundException」錯誤
問題
當您在 Microsoft ASP.NET Web 應用程式中呼叫 Web 服務時,您可能會收到下列錯誤:
System.IO.FileNotFoundException
原因
如果下列任一條件成立,就可能發生此錯誤:
背景工作進程沒有讀取至進程 Temp 目錄的許可權,而且背景工作進程沒有寫入進程暫存目錄的許可權。
XmlSerializer 產生的程式代碼中有編譯錯誤。
解決辦法
此錯誤記載於 PRB:當用戶端應用程式呼叫 Web 服務時,您會收到「System.IO.FileNotFoundException」錯誤。