共用方式為


預設訊息合約

預設訊息合約範例展示了一項服務,在這項服務中,自定義使用者訊息會來回傳送至服務操作。 此範例是以入門指南為基礎,實作計算機介面來作為具型別的服務。 這個範例會傳遞一個自定義訊息,其中包含運算數和運算子,並傳回算術計算的結果,而不是使用 Getting Started 中用於加法、減法、乘法和除法的個別服務操作。

用戶端是控制台程式(.exe),服務連結庫(.dll)由 Internet Information Services (IIS) 裝載。 主控台視窗中會顯示客戶端活動。

備註

此範例的安裝程式和建置指示位於本主題結尾。

在服務中,會定義單一服務作業,以接受並傳回 類型的 MyMessage自定義訊息。 雖然在此範例中,要求和回應訊息的類型相同,但如有必要,它們當然可能是不同的訊息合約。

[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract(Action="http://test/MyMessage_action",
                  ReplyAction="http://test/MyMessage_action")]
    MyMessage Calculate(MyMessage request);
}

自訂訊息 MyMessage 定義於以 MessageContractAttributeMessageHeaderAttributeMessageBodyMemberAttribute 屬性標註的類別中。 此範例中只會使用第三個建構函式。 使用訊息合約可讓您完全控制SOAP訊息。 在此範例中,使用MessageHeaderAttribute屬性將Operation放入SOAP標頭中。 運算元 N1N2 以及 Result 出現在 SOAP 主體中,因為它們已應用 MessageBodyMemberAttribute 屬性。

[MessageContract]
public class MyMessage
{
    private string operation;
    private double n1;
    private double n2;
    private double result;

    //Constructor - create an empty message.

    public MyMessage() {}

    //Constructor - create a message and populate its members.

    public MyMessage(double n1, double n2, string operation,
                     double result)
    {
        this.n1 = n1;
        this.n2 = n2;
        this.operation = operation;
        this.result = result;
    }

    //Constructor - create a message from another message.

    public MyMessage(MyMessage message)
    {
        this.n1 = message.n1;
        this.n2 = message.n2;
        this.operation = message.operation;
        this.result = message.result;
    }

    [MessageHeader]
    public string Operation
    {
        get { return operation; }
        set { operation = value; }
    }

    [MessageBodyMember]
    public double N1
    {
        get { return n1; }
        set { n1 = value; }
    }

    [MessageBodyMember]
    public double N2
    {
        get { return n2; }
        set { n2 = value; }
    }

    [MessageBodyMember]
    public double Result
    {
        get { return result; }
        set { result = value; }
    }
}

實作類別包含服務作業的程序 Calculate 代碼。 類別 CalculateService 會從要求訊息取得作數和運算符,並建立包含所要求計算結果的回應消息,如下列範例程式代碼所示。

// Service class which implements the service contract.
public class CalculatorService : ICalculator
{
    // Perform a calculation.

    public MyMessage Calculate(MyMessage request)
    {
        MyMessage response = new MyMessage(request);
        switch (request.Operation)
        {
            case "+":
                response.Result = request.N1 + request.N2;
                break;
            case "-":
                response.Result = request.N1 - request.N2;
                break;
            case "*":
                response.Result = request.N1 * request.N2;
                break;
            case "/":
                response.Result = request.N1 / request.N2;
                break;
            default:
                response.Result = 0.0D;
                break;
        }
        return response;
    }
}

產生的用戶端程式代碼是使用 ServiceModel 元數據公用程式工具 (Svcutil.exe) 工具所建立。 必要時,此工具會自動在產生的用戶端程式代碼中建立訊息合約類型。 /messageContract可以指定命令選項來強制產生訊息合約。

svcutil.exe /n:"http://Microsoft.ServiceModel.Samples,Microsoft.ServiceModel.Samples" /o:client\generatedClient.cs http://localhost/servicemodelsamples/service.svc/mex

下列範例程式代碼示範用戶端使用 MyMessage 訊息。

// Create a client with given client endpoint configuration
CalculatorClient client = new CalculatorClient();

// Perform addition using a typed message.

MyMessage request = new MyMessage()
                    {
                        N1 = 100D,
                        N2 = 15.99D,
                        Operation = "+"
                    };
MyMessage response = ((ICalculator)client).Calculate(request);
Console.WriteLine("Add({0},{1}) = {2}", request.N1, request.N2, response.Result);

當您執行範例時,計算會顯示在用戶端控制台視窗中。 在客戶端視窗中按 ENTER 鍵以關閉用戶端。

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

此時,自定義使用者定義訊息已在用戶端與服務作業之間傳遞。 訊息合約定義作數和結果位於訊息本文中,且運算符位於訊息標頭中。 訊息記錄可以設定為觀察此訊息結構。

要設定、建置和執行範例,請執行以下步驟:

  1. 請確定您已針對 Windows Communication Foundation 範例 執行One-Time 安裝程式。

  2. 若要建置解決方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例 中的指示。

  3. 若要在單一或跨計算機組態中執行範例,請遵循執行 Windows Communication Foundation 範例 中的指示。