作法:在組態中指定用戶端繫結
在此範例中,建立了一個使用計算機服務的用戶端主控台應用程式,並在組態中以宣告方式指定用戶端的繫結。 用戶端會存取 CalculatorService
(該服務會實作 ICalculator
介面),而服務和用戶端都會使用 BasicHttpBinding 類別。
所述的程序假設計算機服務正在執行中。 如需如何建置服務的資訊,請參閱如何:在組態中指定服務繫結。 它也使用 Windows Communication Foundation (WCF) 提供的 ServiceModel 中繼資料公用程式工具 (Svcutil.exe) 來自動產生用戶端元件。 此工具會產生用戶端程式碼,以及存取服務的組態。
用戶端會建置成兩個部分。 Svcutil.exe 會產生 ClientCalculator
,而該元件會實作 ICalculator
介面。 接著會藉由建構 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({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(); } } }
請編譯並執行用戶端。