ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 一: 服务是端点的集合   当你在开发wcf的时候,你或许已经注意到了一个service可以公布多个endpoint,确实是这样,在wcf中有一句很经典的话,叫做“服务是端点的集合",就 比如说一个普普通通的服务,它就公布了一个服务端点,一个元数据端点,对吧。。。 ![](https://box.kancloud.cn/2015-08-04_55c0b2d2bb760.png) 仔细一想,这个问题就好玩了,既然一个service可以公布多个endpoint,而且我还知道wcf中有很多的binding,这些binding对应着很多的传输方式,那是不是 说我一个service可以用多种协议方法对外公布,比如说同时以nettcp,basic,msmqbinding,udp等方式公布,对吧,那这样的话是不是超级好玩,如果对方 是非.net程序,那就可以调用我的basic,如果对方是.net程序,那是不是可以调用我的nettcp,对不对。。。当然啦,wcf无所不能,这是一个史上无比强大的牛 逼框架,牛逼的要死,已经逼得程序员只需随便改几个配置就能达到完全不一样的效果。。。下面我同时用nettcp和basic的方式来同时公布服务,好了,现在我 们就来见证奇迹吧。。。 Service: ~~~ using System; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Channels; using System.Threading; namespace MyService { public class HomeService : IHomeService { public Student Update(Student message) { return new Student() { Name = "一线码农" }; } } [DataContract] public class Student { [DataMember] public string Name { get; set; } [DataMember] public int Age { get; set; } } } ~~~ Host : ~~~ class Program1 { static void Main(string[] args) { ServiceHost host = new ServiceHost(typeof(HomeService), new Uri("http://192.168.1.105:1920")); host.AddServiceEndpoint(typeof(IHomeService), new BasicHttpBinding(), "HomeServie"); host.AddServiceEndpoint(typeof(IHomeService), new NetTcpBinding(), "net.tcp://192.168.1.105:1921/HomeServieTcp"); host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true }); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); host.Open(); Console.Read(); } } ~~~ Client端: ~~~ class Program { static void Main(string[] args) { //basic 方式 ChannelFactory<IHomeService> factory = new ChannelFactory<IHomeService>(new BasicHttpBinding(), new EndpointAddress("http://192.168.1.105:1920/HomeServie")); var client = factory.CreateChannel(); var result = client.Update(new Student() { }); //nettcp方式 factory = new ChannelFactory<IHomeService>(new NetTcpBinding(), new EndpointAddress("net.tcp://192.168.1.105:1921/HomeServieTcp")); client = factory.CreateChannel(); result = client.Update(new Student() { }); } } ~~~ 通过上面的代码,是不是已经发现,我在client端,既可以用basic的方式调用,又可以用nettcp的方式调用,这个技巧是不是感觉wcf无比强大呢??? ## 二:Host寄宿多个Service   我们知道wcf的寄宿方式有很多种,有iis,有windowservice,还有简单方便的console方式,而默认情况下,我们最通常的方法都是一个service,一个寄宿, 而其实呢??? 其实一个寄宿host可以承载多个service,看起来是不是很好玩,如果说你有10个servcie,现在你只需要用一个console host就能寄宿起来,废 话不多说,我演示一下给你看就好了。 Service: ~~~ namespace MyService { [ServiceContract] public interface IHomeService { [OperationContract] Student Update(Student message); } [ServiceContract] public interface IFlyService { [OperationContract] Student Fly(Student stu); } } ~~~ Host: ~~~ class Program1 { static void Main(string[] args) { //第一个: 这是Home服务 ServiceHost host = new ServiceHost(typeof(HomeService), new Uri("http://192.168.1.105:1920")); host.AddServiceEndpoint(typeof(IHomeService), new BasicHttpBinding(), "HomeServie"); host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true }); host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); host.Open(); Console.WriteLine("Home服务开启。。。。"); //第一个: 这是Fly服务 var host2 = new ServiceHost(typeof(FlyService), new Uri("http://192.168.1.105:1930")); host2.AddServiceEndpoint(typeof(IFlyService), new BasicHttpBinding(), "FlyServie"); host2.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true }); host2.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); host2.Open(); Console.WriteLine("Fly服务开启。。。。"); Console.Read(); } } ~~~ ![](https://box.kancloud.cn/2015-08-04_55c0b2d9d667a.png) 有没有看到,现在两个服务都开启了,这种方式看起来是不是很爽呀,否则的话,你需要开启两个Host,这样的话,我的手续就精简了。。。对吧。。 ## 三: Tcp中的端口共享    这玩意听起来大家都懂,端口共享嘛,不就是两个程序共享一个端口,对吧,在通常情况下,我们肯定会认为这无法做到,其实呢?在Wcf中我们还是可以玩 的,也就是一个PortSharingEnabled的事!!!如果说端口可以共享的话,那我们的service是不是就可以少开辟几个端口呢?同样这也方便我们进行service的管 理,下面我给大家继续演示一下。。。很好玩的,么么哒 ![](https://box.kancloud.cn/2015-08-04_55c0b2da31db3.png) 可以看到,我的两个host都是用1920的端口,并且现在我真的开启起来啦。。。。好了,三种技巧都说到了,我想你在现实的wcf开发中,或多或少的都能接 触的到,希望对你有用~~~~