重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

wcf学习1:搭建服务-创新互联

一:创建一个解决方案wcf学习1:搭建服务

1:分别添加:控制台应用程序:Hosting(宿主)、控制台应用程序:Client(客户端)、类库:Service (服务)、类库:Contract(契约)

创新互联建站基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业服务器托管报价,主机托管价格性价比高,为金融证券行业四川绵阳服务器托管,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。

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() == 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(); } } } }

管理方式配置宿主:

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:修改配置文件:
          binding="wsHttpBinding"
          contract="WCFContract.ICalculator"
          name="defaultEndpoint">
3:开始调用 using (ChannelFactory channe = new ChannelFactory("defaultEndpoint")) {           WCFContract.ICalculator ca= channe.CreateChannel();           Console.WriteLine(ca.Add(1, 2)); };

文章题目:wcf学习1:搭建服务-创新互联
链接URL:http://cqcxhl.cn/article/gecgs.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP