在此範例中,會建立用戶端控制台應用程式以使用計算機服務,並在組態中以宣告方式指定該客戶端的系結。 用戶端會存取 CalculatorService,該CalculatorService實作了 ICalculator 介面,並且服務和用戶端都會使用 BasicHttpBinding 類別。
概述的程式假設計算機服務正在執行。 如需如何建置服務的資訊,請參閱 如何:在組態中指定服務系結。 它也會使用 Windows Communication Foundation (WCF) 所提供的 ServiceModel 元數據公用程式工具 (Svcutil.exe ),自動產生用戶端元件。 此工具會產生用來存取服務的用戶端程式代碼和組態。
用戶端內建兩個部分。 Svcutil.exe 生成實作 ICalculator 介面的 ClientCalculator。 接著,通過建立 ClientCalculator 的實例來構建用戶端應用程式。
在組態中以宣告方式指定系結和地址資訊,而不是在程式代碼中以命令方式指定,通常是最佳做法。 在程式代碼中定義端點通常並不實用,因為已部署服務的系結和位址通常與開發服務時所使用的端點不同。 更一般而言,將系結和尋址資訊從程序代碼中分離,可以讓這些資訊變更,而不需要重新編譯或重新部署應用程式。
您可以使用 組態編輯器工具(SvcConfigEditor.exe)以執行以下所有設定步驟。
如需此範例的來源複本,請參閱 BasicBinding 範例。
在組態中指定客戶端系結
從命令行使用 Svcutil.exe,從服務元數據產生程序代碼。
Svcutil.exe <service's Metadata Exchange (MEX) address or HTTP GET address>產生的用戶端包含
ICalculator介面,定義用戶端實作必須滿足的服務合約。[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] [System.ServiceModel.ServiceContractAttribute(Namespace="http://Microsoft.ServiceModel.Samples", ConfigurationName="Microsoft.ServiceModel.Samples.ICalculator")] public interface ICalculator { [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Add", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")] double Add(double n1, double n2); [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")] double Subtract(double n1, double n2); [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")] double Multiply(double n1, double n2); [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Divide", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")] double Divide(double n1, double n2); }[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); }產生的用戶端也包含
ClientCalculator的實作。[System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public partial 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, System.ServiceModel.EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public CalculatorClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.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 CalculatorService : ICalculator { public double Add(double n1, double n2) { return n1 + n2; } public double Subtract(double n1, double n2) { return n1 - n2; } public double Multiply(double n1, double n2) { return n1 * n2; } public double Divide(double n1, double n2) { return n1 / n2; } }Svcutil.exe 也會為使用 BasicHttpBinding 類別的用戶端產生組態。 使用 Visual Studio 時,請將此檔案命名為 App.config。請注意,位址和系結資訊不會在服務實作中的任何位置指定。 此外,不需要撰寫程式代碼,即可從組態檔擷取該資訊。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint name="" address="http://localhost/servicemodelsamples/service.svc" binding="basicHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" /> </client> <bindings> <basicHttpBinding/> </bindings> </system.serviceModel> </configuration>在應用程式中建立
ClientCalculator的實例,然後呼叫服務作業。using System; using System.ServiceModel; namespace Microsoft.ServiceModel.Samples { //Client implementation code. class Client { static void Main() { // Create a client with given client endpoint configuration CalculatorClient client = new CalculatorClient(); // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine($"Add({value1},{value2}) = {result}"); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine($"Subtract({value1},{value2}) = {result}"); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine($"Multiply({value1},{value2}) = {result}"); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine($"Divide({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(); } } }編譯並執行用戶端。