一个WCF宿住如何发布两个接口

您所在的位置:网站首页 e-service2 一个WCF宿住如何发布两个接口

一个WCF宿住如何发布两个接口

2023-09-21 11:06| 来源: 网络整理| 查看: 265

服务端

建立第一个接口ISrevice1:

[ServiceContract] public interface IService1 { [OperationContract] string GetIService1(); } 实现IService1:

public class Service1 : IService1 { public string GetIService1() { return string.Format("You Called: IService1"); } } 建立第二个接口IService2:

[ServiceContract] public interface IService2 { [OperationContract] string GetIService2(); 实现IService2:

public class Service2 : IService2 { public string GetIService2() { return string.Format("You Called: IService2"); } } 新建一个WinForm项目,作为宿住,并引用WCF服务,并打开两个接口

private ServiceHost host1 = null; private ServiceHost host2 = null; private void button1_Click(object sender, EventArgs e) { host1 = new ServiceHost(typeof(WcfServiceLibrary1.Service1)); host2 = new ServiceHost(typeof(WcfServiceLibrary1.Service2)); host1.Open(); host2.Open(); label1.Text = "服务已启动"; } 主要是配置App.config文件:

注意,配置文件中插入了两个service节点,分别对应两个接口。

以上服务端搭建完成。

客户端:

建立WinForm项目,分别引用两个服务,并命名为Service1、Service2:

private void button1_Click(object sender, EventArgs e) { Service1.IService1 service1 = new Service1.Service1Client(); MessageBox.Show(service1.GetIService1()); service1 = null; } private void button2_Click(object sender, EventArgs e) { Service2.IService2 service2 = new Service2.Service2Client(); MessageBox.Show(service2.GetIService2()); service2 = null; } 客户端配置文件在引用后自动生成

运行服务端及客户端,实现客户端调用两个接口。

此示例只是简单的实现两个接口的过程,实际上两个接口可以使用不同的协议通讯。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3