本文主要是介绍WCF多个协定实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Description;namespace WcfDemo1
{/// <summary> /// 实现服务协定接口的服务类 /// </summary> class MyService : IService1, IService2, IService3{/// <summary>/// 服务器端/// </summary>/// <param name="args"></param>static void Main(string[] args){using (ServiceHost host = new ServiceHost(typeof(MyService), new Uri("http://localhost:8001/service"))){WSHttpBinding binding = new WSHttpBinding();binding.Name = "MyBinding";binding.Security.Mode = SecurityMode.None;host.AddServiceEndpoint(typeof(IService1), binding, "mysv1");host.AddServiceEndpoint(typeof(IService2), binding, "mysv2");host.AddServiceEndpoint(typeof(IService3), binding, "mysv3");ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();behavior.HttpGetEnabled = true;host.Description.Behaviors.Add(behavior);host.Opened += (s, a) => Console.WriteLine("服务已启动。");try{host.Open();}catch (Exception ex){Console.WriteLine(ex.Message);}Console.ReadKey();host.Close();}}string IService1.SayHelloA(){return "你好,这是第一个服务协定。";}string IService2.SayHelloB(){return "你好,这是第二个服务协定。";}string IService3.SayHelloC(){return "你好,这是第三个服务协定。";} }[ServiceContract]public interface IService1{[OperationContract]string SayHelloA();}[ServiceContract]public interface IService2{[OperationContract]string SayHelloB();}[ServiceContract]public interface IService3{[OperationContract]string SayHelloC();}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Client
{class Program{/// <summary>/// 客户端 添加服务引用/// </summary>/// <param name="args"></param>static void Main(string[] args){WS.Service1Client client1 = new WS.Service1Client();Console.WriteLine(client1.SayHelloA());WS.Service2Client client2 = new WS.Service2Client();Console.WriteLine(client2.SayHelloB());WS.Service3Client client3 = new WS.Service3Client();Console.WriteLine(client3.SayHelloC());Console.ReadKey(); }}
}
这篇关于WCF多个协定实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!