共用方式為


針對 Web 服務進行疑難排解

Microsoft BizTalk Server會大量使用 Web 服務,以便與 SOAP 配接器搭配使用,以及在將協調流程發佈為 Web 服務時使用。 本主題提供可供您進行 Web 服務疑難排解的一些步驟,並說明某些常見的 Web 服務問題以及解決這些問題的方式。

使用 .NET Framework 追蹤來擷取和記錄 Web 服務中的錯誤

.NET Framework System.Diagnostics.Trace類別可用來擷取錯誤並將其寫入文字檔。

使用 System.Diagnostics.Trace 類別來擷取錯誤並將錯誤寫入文字檔中

  1. 更新 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 值設定為適當的開發層級,並在開發完成後變更值,以減少或禁止追蹤輸出。

  2. 建立 TraceSwitchTextWriterTraceListener 類別的實例,並使用 try... Web 服務 [WebMethod] 呼叫 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.";
            }
        }
    }
    

    注意

    此程式碼是您在 Microsoft Visual Studio 中建立新 ASP.Net Web 服務 專案時所產生的預設 HelloWorld Web 服務修改版本。

    注意

    在 Windows Vista 中,您可能必須擁有系統管理員權限,才能將追蹤輸出檔寫入至根資料夾。

  3. 重新建立 Web 服務專案。 現在,如果在 Try 語句中發生錯誤,則會在 Catch 語句中處理例外狀況,並將錯誤寫入追蹤輸出檔。

已知問題

Web 服務傳回 HTTP 404 (找不到檔案) 錯誤

問題

嘗試呼叫 Web 服務時傳回 HTTP 404 (找不到檔案) 錯誤。

原因

如果裝載 Web 服務的 IIS 伺服器上沒有安裝及/或啟用 ASP.NET,就會發生這個錯誤。

解決方案

確認已安裝及啟用安裝 ASP.NET。 如果未安裝且/或執行位於 IIS 伺服器的 %WinDir%\Microsoft.NET\Framework\vXXX.XXX\ 資料夾的aspnet_regiis.exe程式,請安裝.NET Framework

呼叫 Web 服務時發生 "System.IO.FileNotFoundException" 錯誤

問題

在 Microsoft ASP.NET Web 應用程式中呼叫 Web 服務時,您可能會收到下列錯誤:

System.IO.FileNotFoundException

原因

如果下列其中一項條件成立,就會發生這個錯誤:

  • 背景工作處理序沒有權限可以讀取處理序暫存目錄,也沒有權限可以寫入處理序暫存目錄。

  • XmlSerializer 產生的程式碼中有編譯錯誤。

解決方案

此錯誤記載于 PRB:當用戶端應用程式呼叫 Web 服務時,您會收到「System.IO.FileNotFoundException」錯誤

另請參閱

解決 IIS 許可權問題的指導方針解決 Web 服務許可權問題的指導方針