一:创建一个解决方案
1:分别添加:控制台应用程序:Hosting(宿主)、控制台应用程序:Client(客户端)、类库:Service (服务)、类库:Contract(契约)
2:添加契约:
右击contract项目,添加接口 ICalcultor
[ServiceContract(Name = "CalcultorService", Namespace = "http://www.artech.com")] 注意:http://www.artech.com 不能写成 http://www.artech.com/ public interface ICalculator { [OperationContract] double Add(double x, double y); [OperationContract] double Subtract(double x, double y); [OperationContract] double Multiply(double x, double y); [OperationContract] double Divide(double x, double y); }
3:添加服务
右击Service项目,添加类:Calcutor
public class CalcultorService : ICalculator { public double Add(double x, double y) { return x + y; } public double Subtract(double x, double y) { return x - y; } public double Multiply(double x, double y) { return x * y; } public double Divide(double x, double y) { return x / y; } }
4:配置Hosting(宿主)
编程方式配置:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Description; using WCFContract; using WCFService; namespace Host3_26 { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(CalcultorService))) { host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/CalcultorService"); if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) { ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetEnabled = true; behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/CalcultorService/metadata"); host.Description.Behaviors.Add(behavior); } host.Opened += delegate { Console.WriteLine("CalcultorService已经启动,按任意键终止服务!!"); }; host.Open(); Console.Read(); } } } }
管理方式配置宿主:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:9999/CalcultorService/metadata"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="WCFService.CalcultorService" behaviorConfiguration="metadataBehavior"> <endpoint address="http://127.0.0.1:9999/CalcultorService" binding="wsHttpBinding" contract="WCFContract.ICalculator"/> </service> </services> </system.serviceModel> </configuration>
5:发布宿主
6:客户端添加服务
在宿主已经运行的情况下,右击客户端添加服务,路径:
http://127.0.0.1:9999/CalcultorService/metadata
cs代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Client3_26.ServiceReference1; namespace Client3_26 { class Program { static void Main(string[] args) { using (CalcultorServiceClient pro = new CalcultorServiceClient()) { Console.WriteLine(pro.Add(1, 1)); Console.Read(); }
} } }
//客户端调用服务方法2: 1:引用契约 using WCFContract; 2:修改配置文件: <endpoint address="http://127.0.0.1:9999/CalcultorService"
binding="wsHttpBinding"
contract="WCFContract.ICalculator"
name="defaultEndpoint"></endpoint> 3:开始调用 using (ChannelFactory<WCFContract.ICalculator> channe = new ChannelFactory<WCFContract.ICalculator>("defaultEndpoint")) { WCFContract.ICalculator ca = channe.CreateChannel(); Console.WriteLine(ca.Add(1, 2)); };