共用方式為


作法:在程式碼中指定用戶端繫結

在這個範例中,建立了一個使用計算機服務的用戶端,並於程式碼中以命令方式指定該用戶端的繫結。 用戶端會存取 CalculatorService (該服務會實作 ICalculator 介面),而服務和用戶端都會使用 BasicHttpBinding 類別。

此程序假設計算機服務正在執行中。 如需如何建置服務的資訊,請參閱如何:在組態中指定服務繫結。 它也使用 Windows Communication Foundation (WCF) 提供的 ServiceModel 中繼資料公用程式工具 (Svcutil.exe) 來自動產生用戶端元件。 此工具會產生存取服務所需的用戶端程式碼。

用戶端會建置成兩個部分。 Svcutil.exe 會產生 ClientCalculator,而該元件會實作 ICalculator 介面。 接著,會建構 ClientCalculator 的執行個體,並在程式碼中指定此服務的繫結與位址,藉此建構此用戶端應用程式。

如需此範例的來源複本,請參閱 BasicBinding 範例。

若要在程式碼中指定自訂繫結

  1. 從命令列使用 Svcutil.exe 產生取自服務中繼資料的程式碼。

    Svcutil.exe <service's Metadata Exchange (MEX) address or HTTP GET address>
    
  2. 所產生的用戶端會包含 ICalculator 介面,其中定義了用戶端實作所必須滿足的服務合約。

    [ServiceContract]
    public interface ICalculator
    {
       [OperationContract]
       double Add(double n1, double n2);
       [OperationContract]
       double Subtract(double n1, double n2);
       [OperationContract]
       double Multiply(double n1, double n2);
       [OperationContract]
       double Divide(double n1, double n2);
    }
    
    
    <ServiceContract()> _
    Public Interface ICalculator
    
        <OperationContract()> _
        Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
    
    End Interface
    
    
  3. 產生的用戶端也會包含 ClientCalculator 的實作。

    
    public class CalculatorClient : System.ServiceModel.ClientBase<Microsoft.ServiceModel.Samples.ICalculator>, Microsoft.ServiceModel.Samples.ICalculator
    {
    
        public CalculatorClient()
        {
        }
    
        public CalculatorClient(string endpointConfigurationName) :
                base(endpointConfigurationName)
        {
        }
    
        public CalculatorClient(string endpointConfigurationName, string remoteAddress) :
                base(endpointConfigurationName, remoteAddress)
        {
        }
    
        public CalculatorClient(string endpointConfigurationName, EndpointAddress remoteAddress) :
                base(endpointConfigurationName, remoteAddress)
        {
        }
    
        public CalculatorClient(Binding binding, EndpointAddress remoteAddress) :
                base(binding, remoteAddress)
        {
        }
    
        public double Add(double n1, double n2)
        {
            return base.Channel.Add(n1, n2);
        }
    
        public double Subtract(double n1, double n2)
        {
            return base.Channel.Subtract(n1, n2);
        }
    
        public double Multiply(double n1, double n2)
        {
            return base.Channel.Multiply(n1, n2);
        }
    
        public double Divide(double n1, double n2)
        {
            return base.Channel.Divide(n1, n2);
        }
    }
    
    
    Public Class CalculatorClient
        Inherits System.ServiceModel.ClientBase(Of Microsoft.ServiceModel.Samples.ICalculator)
        Implements Microsoft.ServiceModel.Samples.ICalculator
    
        Public Sub New()
        End Sub
    
        Public Sub New(ByVal endpointConfigurationName As String)
            MyBase.New(endpointConfigurationName)
        End Sub
    
        Public Sub New(ByVal endpointConfigurationName As String, _
                       ByVal remoteAddress As String)
            MyBase.New(endpointConfigurationName, remoteAddress)
        End Sub
    
        Public Sub New(ByVal endpointConfigurationName As String, _
                       ByVal remoteAddress As EndpointAddress)
            MyBase.New(endpointConfigurationName, remoteAddress)
        End Sub
    
        Public Sub New(ByVal binding As Binding, _
                       ByVal remoteAddress As EndpointAddress)
            MyBase.New(binding, remoteAddress)
        End Sub
    
        Public Function Add(ByVal n1 As Double, _
                            ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Add
            Return MyBase.Channel.Add(n1, n2)
        End Function
    
        Public Function Subtract(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Subtract
            Return MyBase.Channel.Subtract(n1, n2)
        End Function
    
        Public Function Multiply(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Multiply
            Return MyBase.Channel.Multiply(n1, n2)
        End Function
    
        Public Function Divide(ByVal n1 As Double, _
                               ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Divide
            Return MyBase.Channel.Divide(n1, n2)
        End Function
    
    End Class
    
    
  4. 建立 ClientCalculator 的執行個體 (此執行個體會在用戶端應用程式中使用 BasicHttpBinding 類別),然後在指定的位址上呼叫服務作業。

    
        //Client implementation code.
        class Client
        {
            static void Main()
            {
    
                //Specify the binding to be used for the client.
                BasicHttpBinding binding = new BasicHttpBinding();
    
                //Specify the address to be used for the client.
                EndpointAddress address =
                   new EndpointAddress("http://localhost/servicemodelsamples/service.svc");
    
                // Create a client that is configured with this address and binding.
                CalculatorClient client = new CalculatorClient(binding, address);
    
                // Call the Add service operation.
                double value1 = 100.00D;
                double value2 = 15.99D;
                double result = client.Add(value1, value2);
                Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
    
                // Call the Subtract service operation.
                value1 = 145.00D;
                value2 = 76.54D;
                result = client.Subtract(value1, value2);
                Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
    
                // Call the Multiply service operation.
                value1 = 9.00D;
                value2 = 81.25D;
                result = client.Multiply(value1, value2);
                Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
    
                // Call the Divide service operation.
                value1 = 22.00D;
                value2 = 7.00D;
                result = client.Divide(value1, value2);
                Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
    
                //Closing the client gracefully closes the connection and cleans up resources
                client.Close();
    
                Console.WriteLine();
                Console.WriteLine("Press <ENTER> to terminate client.");
                Console.ReadLine();
            }
        }
    }
    
    
    
        'Client implementation code.
        Friend Class Client
            Shared Sub Main()
    
                'Specify the binding to be used for the client.
                Dim binding As New BasicHttpBinding()
    
                'Specify the address to be used for the client.
                Dim address As New EndpointAddress("http://localhost/servicemodelsamples/service.svc")
    
    
                ' Create a client that is configured with this address and binding.
                Dim client As New CalculatorClient(binding, address)
    
                ' Call the Add service operation.
                Dim value1 = 100.0R
                Dim value2 = 15.99R
                Dim result = client.Add(value1, value2)
                Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result)
    
                ' Call the Subtract service operation.
                value1 = 145.0R
                value2 = 76.54R
                result = client.Subtract(value1, value2)
                Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result)
    
                ' Call the Multiply service operation.
                value1 = 9.0R
                value2 = 81.25R
                result = client.Multiply(value1, value2)
                Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result)
    
                ' Call the Divide service operation.
                value1 = 22.0R
                value2 = 7.0R
                result = client.Divide(value1, value2)
                Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result)
    
                'Closing the client gracefully closes the connection and cleans up resources
                client.Close()
    
                Console.WriteLine()
                Console.WriteLine("Press <ENTER> to terminate client.")
                Console.ReadLine()
            End Sub
        End Class
    End Namespace
    
    
  5. 請編譯並執行用戶端。

另請參閱