如何:在配置中指定客户端绑定
在此示例中,创建了一个使用计算器服务的客户端控制台应用程序,并在配置中以声明方式为该客户端指定了绑定。 该客户端访问实现了 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(); } } }
编译并运行客户端。