基本資料合約

這個範例會示範如何實作資料合約。資料合約可以讓您在服務間來回傳遞結構化資料。這個範例是以使用者入門範例為基礎,但是它會使用複數,而不是基本數字型別。

在這個範例中,服務是由網際網路資訊服務 (IIS) 所裝載,而用戶端是主控台應用程式 (.exe)。

ms752104.note(zh-tw,VS.90).gif注意:
此範例的安裝程序與建置指示位於本主題的結尾。

這個服務的服務合約會使用複數,如下列範例程式碼所示。

// Define a service contract.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract]
    ComplexNumber Add(ComplexNumber n1, ComplexNumber n2);
    [OperationContract]
    ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2);
    [OperationContract]
    ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2);
    [OperationContract]
    ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2);
}

DataContractAttributeDataMemberAttribute 屬性已套用至 ComplexNumber 類別的定義,以便指出類別的哪些欄位可以經由網路在用戶端與服務之間傳遞,如下列範例程式碼所示。

[DataContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public class ComplexNumber
{
    [DataMember]
    public double Real = 0.0D;
    [DataMember]
    public double Imaginary = 0.0D;

    public ComplexNumber(double real, double imaginary)
    {
        this.Real = real;
        this.Imaginary = imaginary;
    }
}

服務實作會計算並傳回適當的結果,即接受並傳回 ComplexNumber 型別的數字。

// This is the service class that implements the service contract.
public class CalculatorService : ICalculator
{
    public ComplexNumber Add(ComplexNumber n1, ComplexNumber n2)
    {
        return new ComplexNumber(n1.Real + n2.Real, n1.Imaginary +
                                                      n2.Imaginary);
    }

    public ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2)
    {
        return new ComplexNumber(n1.Real - n2.Real, n1.Imaginary - 
                                                     n2.Imaginary);
    }
    public ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2)
    {
        double real1 = n1.Real * n2.Real;
        double imaginary1 = n1.Real * n2.Imaginary;
        double imaginary2 = n2.Real * n1.Imaginary;
        double real2 = n1.Imaginary * n2.Imaginary * -1;
        return new ComplexNumber(real1 + real2, imaginary1 + 
                                                 imaginary2);
    }

    public ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2)
    {
         ComplexNumber conjugate = 
              new ComplexNumber(n2.Real, -1*n2.Imaginary);
         ComplexNumber numerator = Multiply(n1, conjugate);
         ComplexNumber denominator = Multiply(n2, conjugate);
         return new ComplexNumber(numerator.Real / denominator.Real,
                                               numerator.Imaginary);
    }
}

用戶端實作也會使用複數。服務合約與資料合約都會定義於 generatedClient.cs 原始程式檔中,而這個檔案是Service Metadata Utility Tool (Svcutil.exe) 從服務中繼資料所產生的。

// Create a client.
DataContractCalculatorClient client = new DataContractCalculatorClient();
// Call the Add service operation.
ComplexNumber value1 = new ComplexNumber(); 
value1.Real = 1; 
value1.Imaginary = 2;
ComplexNumber value2 = new ComplexNumber(); 
value2.Real = 3;
value2.Imaginary = 4;
ComplexNumber result = proxy.Add(value1, value2);
Console.WriteLine("Add({0} + {1}i, {2} + {3}i) = {4} + {5}i",
      value1.Real, value1.Imaginary, value2.Real, value2.Imaginary, 
      result.Real, result.Imaginary); 
    …
}

當您執行範例時,作業的要求和回應會顯示在用戶端主控台視窗中。在用戶端視窗中按下 ENTER 鍵,即可關閉用戶端。

    Add(1 + 2i, 3 + 4i) = 4 + 6i
    Subtract(1 + 2i, 3 + 4i) = -2 + -2i
    Multiply(2 + 3i, 4 + 7i) = -13 + 26i
    Divide(3 + 7i, 5 + -2i) = 0.0344827586206897 + 41i

    Press <ENTER> to terminate client.

若要設定、建置及執行範例

  1. 請確定您已執行 Windows Communication Foundation 範例的單次安裝程序

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

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

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.