了解產生的用戶端程式碼

ServiceModel Metadata Utility Tool (Svcutil.exe) 會產生用戶端程式碼和用戶端應用程式組態檔,用於建置用戶端應用程式。 本主題將提供產生之程式碼範例的導覽,用於標準服務合約情節。 如需使用所產生程式碼來建置用戶端應用程式的詳細資訊,請參閱 WCF 用戶端概觀

概觀

若您使用 Visual Studio 為您的專案產生 Windows Communication Foundation (WCF) 用戶端類型,通常不需要檢查產生的用戶端程式碼。 如果您不是使用為您執行相同服務的開發環境,可以使用如 Svcutil.exe 等工具來產生用戶端程式碼,然後使用該程式碼來開發您的用戶端應用程式。

由於 Svcutil.exe 有許多選項可修改產生的型別資訊,因此本主題不會討論所有的案例。 然而,下列標準工作包含找出產生的程式碼:

  • 識別服務合約介面。

  • 識別 WCF 用戶端類別。

  • 識別資料型別。

  • 識別雙工服務的回呼合約。

  • 識別協助程式服務合約通道介面。

尋找服務合約介面

如果要找出建立服務合約模型的介面,請搜尋以 System.ServiceModel.ServiceContractAttribute 屬性標示的介面。 由於其他屬性的存在,且這個屬性本身有明確的內容設定,因此常常很難快速找出這個屬性。 請記住,服務合約介面和用戶端合約介面是兩種不同的型別。 下列程式碼範例會顯示原始的服務合約。

[ServiceContractAttribute(
  Namespace = "http://microsoft.wcf.documentation"
)]
public interface ISampleService
{
  [OperationContractAttribute]
  [FaultContractAttribute(typeof(microsoft.wcf.documentation.SampleFault))]
  string SampleMethod(string msg);
}

下列程式碼範例會顯示和 Svcutil.exe 所產生之相同的服務合約。

[System.ServiceModel.ServiceContractAttribute(
  Namespace = "http://microsoft.wcf.documentation"
)]
public interface ISampleService
{
    [System.ServiceModel.OperationContractAttribute(
      Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethod",
      ReplyAction = "http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse"
    )]
    [System.ServiceModel.FaultContractAttribute(
      typeof(microsoft.wcf.documentation.SampleFault),
      Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethodSampleFaultFault"
    )]
    string SampleMethod(string msg);
}

您可以使用產生的服務合約介面以及 System.ServiceModel.ChannelFactory 類別,來建立叫用服務作業的 WCF 通道物件。 如需詳細資訊,請參閱操作說明:使用 ChannelFactory

尋找 WCF 用戶端類別

若要找出實作您所要使用服務合約的 WCF 用戶端類別,請搜尋 System.ServiceModel.ClientBase<TChannel> 的延伸模組,其中型別參數是您之前找出並擴充該介面的服務合約介面。 下列程式碼範例會顯示型別 ClientBase<TChannel>ISampleService類別。

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class SampleServiceClient : System.ServiceModel.ClientBase<ISampleService>, ISampleService
{

    public SampleServiceClient()
    {
    }

    public SampleServiceClient(string endpointConfigurationName)
        :
            base(endpointConfigurationName)
    {
    }

    public SampleServiceClient(string endpointConfigurationName, string remoteAddress)
        :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public SampleServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress)
        :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public SampleServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress)
        :
            base(binding, remoteAddress)
    {
    }
    public string SampleMethod(string msg)
    {
        return base.Channel.SampleMethod(msg);
    }
}

您可以使用此 WCF 用戶端類別,方法是建立其新執行個體並呼叫其實作的方法。 這些方法會叫用用於設計及設定互動的服務作業。 如需詳細資訊,請參閱 WCF 用戶端概觀

注意

當 SvcUtil.exe 產生 WCF 用戶端類別時,會將 DebuggerStepThroughAttribute 加入至用戶端類別,防止偵錯工具逐步執行 WCF 用戶端類別。

尋找資料型別

如果要在產生的程式碼中找出資料型別,最基本的機制就是識別合約中指定的型別名稱,然後在程式碼中搜尋該型別宣告。 例如,下列合約指定 SampleMethod 可以傳回型別 microsoft.wcf.documentation.SampleFault的 SOAP 錯誤。

[System.ServiceModel.OperationContractAttribute(
  Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethod",
  ReplyAction = "http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse"
)]
[System.ServiceModel.FaultContractAttribute(
  typeof(microsoft.wcf.documentation.SampleFault),
  Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethodSampleFaultFault"
)]
string SampleMethod(string msg);

搜尋 SampleFault 會找出下列型別宣告。

[assembly: System.Runtime.Serialization.ContractNamespaceAttribute(
  "http://microsoft.wcf.documentation",
  ClrNamespace = "microsoft.wcf.documentation"
)]
namespace microsoft.wcf.documentation
{
    using System.Runtime.Serialization;

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute()]
    public partial class SampleFault : object, System.Runtime.Serialization.IExtensibleDataObject
    {
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
        private string FaultMessageField;

        public System.Runtime.Serialization.ExtensionDataObject ExtensionData
        {
            get
            {
                return this.extensionDataField;
            }
            set
            {
                this.extensionDataField = value;
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public string FaultMessage
        {
            get
            {
                return this.FaultMessageField;
            }
            set
            {
                this.FaultMessageField = value;
            }
        }
    }
}

在此情況下,資料型別是由用戶端上特定例外狀況擲回的詳細型別,其中詳細型別參數為 FaultException<TDetail>microsoft.wcf.documentation.SampleFault。 如需資料類型的詳細資訊,請參閱在服務合約中指定資料傳輸。 如需處理用戶端中例外狀況的詳細資訊,請參閱 傳送及接收錯誤

尋找雙工服務的回呼合約

如果您找出合約介面為其指定 ServiceContractAttribute.CallbackContract 屬性值的服務合約,該合約就會指定雙工合約。 雙工合約需要用戶端應用程式建立回呼類別,實作回呼合約並將該類別的執行個體傳遞至用來與服務通訊的 System.ServiceModel.DuplexClientBase<TChannel>System.ServiceModel.DuplexChannelFactory<TChannel> 。 如需雙工用戶端的詳細資訊,請參閱操作說明:使用雙工合約存取服務

下列合約會指定型別 SampleDuplexHelloCallback的回呼合約。

[System.ServiceModel.ServiceContractAttribute(
  Namespace="http://microsoft.wcf.documentation",
  ConfigurationName="SampleDuplexHello",
  CallbackContract=typeof(SampleDuplexHelloCallback),
  SessionMode=System.ServiceModel.SessionMode.Required
)]
public interface SampleDuplexHello
{
  [System.ServiceModel.OperationContractAttribute(
      IsOneWay=true,
      Action="http://microsoft.wcf.documentation/SampleDuplexHello/Hello"
    )]
    void Hello(string greeting);
  }
    <System.ServiceModel.OperationContractAttribute(IsOneWay:=True, _
        Action:="http://microsoft.wcf.documentation/SampleDuplexHello/Hello")> _
    Sub Hello(ByVal greeting As String)
End Interface 'SampleDuplexHello

搜尋該回呼合約會找出下列用戶端應用程式必須實作的介面。

  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface SampleDuplexHelloCallback
{
  [System.ServiceModel.OperationContractAttribute(
      IsOneWay=true,
      Action="http://microsoft.wcf.documentation/SampleDuplexHello/Reply"
    )]
    void Reply(string responseToGreeting);
  }
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")>  _
Public Interface SampleDuplexHelloCallback
    <System.ServiceModel.OperationContractAttribute( _
        IsOneWay:=True, _
        Action:="http://microsoft.wcf.documentation/SampleDuplexHello/Reply")> _
    Sub Reply(ByVal responseToGreeting As String)
End Interface 'SampleDuplexHelloCallback

尋找服務合約通道介面

當使用含有服務合約介面的 ChannelFactory 類別時,您必須轉換為 System.ServiceModel.IClientChannel 介面,以明確開啟、關閉或中止通道。 為了讓這個類別更容易使用,Svcutil.exe 工具會另外產生可以實作服務合約介面和 IClientChannel 兩者的協助程式介面,讓您可以不用轉型就與用戶端通道基礎結構進行互動。 下列程式碼會示範實作前述服務合約之協助程式用戶端通道的定義。

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface ISampleServiceChannel : ISampleService, System.ServiceModel.IClientChannel
{
}

另請參閱