为了账号安全,请及时绑定邮箱和手机立即绑定

你需要了解的三个小技巧

标签:
前端工具

 一: 服务是端点的集合

  当你在开发wcf的时候,你或许已经注意到了一个service可以公布多个endpoint,确实是这样,在wcf中有一句很经典的话,叫做“服务是端点的集合",就

比如说一个普普通通的服务,它就公布了一个服务端点,一个元数据端点,对吧。。。

仔细一想,这个问题就好玩了,既然一个service可以公布多个endpoint,而且我还知道wcf中有很多的binding,这些binding对应着很多的传输方式,那是不是

说我一个service可以用多种协议方法对外公布,比如说同时以nettcp,basic,msmqbinding,udp等方式公布,对吧,那这样的话是不是超级好玩,如果对方

是非.net程序,那就可以调用我的basic,如果对方是.net程序,那是不是可以调用我的nettcp,对不对。。。当然啦,wcf无所不能,这是一个史上无比强大的牛

逼框架,牛逼的要死,已经逼得程序员只需随便改几个配置就能达到完全不一样的效果。。。下面我同时用nettcp和basic的方式来同时公布服务,好了,现在我

们就来见证奇迹吧。。。

Service:

复制代码

 1 using System; 2 using System.Runtime.Serialization; 3 using System.ServiceModel; 4 using System.ServiceModel.Channels; 5 using System.Threading; 6  7 namespace MyService 8 { 9     public class HomeService : IHomeService10     {11         public Student Update(Student message)12         {13             return new Student() { Name = "一线码农" };14         }15     }16 17     [DataContract]18     public class Student19     {20         [DataMember]21         public string Name { get; set; }22 23         [DataMember]24         public int Age { get; set; }25     }26 }

复制代码

Host :

复制代码

 1     class Program1 2     { 3         static void Main(string[] args) 4         { 5             ServiceHost host = new ServiceHost(typeof(HomeService), new Uri("http://192.168.1.105:1920")); 6  7             host.AddServiceEndpoint(typeof(IHomeService), new BasicHttpBinding(), "HomeServie"); 8  9             host.AddServiceEndpoint(typeof(IHomeService), new NetTcpBinding(), "net.tcp://192.168.1.105:1921/HomeServieTcp");10 11             host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });12 13             host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");14 15             host.Open();16 17             Console.Read();18         }19     }

复制代码

Client端:

复制代码

 1     class Program 2     { 3         static void Main(string[] args) 4         { 5             //basic 方式 6             ChannelFactory<IHomeService> factory = new ChannelFactory<IHomeService>(new BasicHttpBinding(), 7                                                                                     new EndpointAddress("http://192.168.1.105:1920/HomeServie")); 8  9             var client = factory.CreateChannel();10 11             var result = client.Update(new Student() { });12 13 14             //nettcp方式15             factory = new ChannelFactory<IHomeService>(new NetTcpBinding(),16                                                                        new EndpointAddress("net.tcp://192.168.1.105:1921/HomeServieTcp"));17 18             client = factory.CreateChannel();19 20             result = client.Update(new Student() { });21         }22     }

复制代码

 

通过上面的代码,是不是已经发现,我在client端,既可以用basic的方式调用,又可以用nettcp的方式调用,这个技巧是不是感觉wcf无比强大呢???

 

二:Host寄宿多个Service

  我们知道wcf的寄宿方式有很多种,有iis,有windowservice,还有简单方便的console方式,而默认情况下,我们最通常的方法都是一个service,一个寄宿,

而其实呢??? 其实一个寄宿host可以承载多个service,看起来是不是很好玩,如果说你有10个servcie,现在你只需要用一个console host就能寄宿起来,废

话不多说,我演示一下给你看就好了。

Service:

复制代码

 1     namespace MyService 2     { 3         [ServiceContract] 4         public interface IHomeService 5         { 6             [OperationContract] 7             Student Update(Student message); 8         } 9 10         [ServiceContract]11         public interface IFlyService12         {13             [OperationContract]14             Student Fly(Student stu);15         }16     }

复制代码

Host:

复制代码

 1     class Program1 2     { 3         static void Main(string[] args) 4         { 5             //第一个: 这是Home服务 6             ServiceHost host = new ServiceHost(typeof(HomeService), new Uri("http://192.168.1.105:1920")); 7             host.AddServiceEndpoint(typeof(IHomeService), new BasicHttpBinding(), "HomeServie"); 8             host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true }); 9             host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");10             host.Open();11 12             Console.WriteLine("Home服务开启。。。。");13 14             //第一个: 这是Fly服务15             var host2 = new ServiceHost(typeof(FlyService), new Uri("http://192.168.1.105:1930"));16             host2.AddServiceEndpoint(typeof(IFlyService), new BasicHttpBinding(), "FlyServie");17             host2.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });18             host2.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");19             host2.Open();20 21             Console.WriteLine("Fly服务开启。。。。");22 23             Console.Read();24         }25     }

复制代码

有没有看到,现在两个服务都开启了,这种方式看起来是不是很爽呀,否则的话,你需要开启两个Host,这样的话,我的手续就精简了。。。对吧。。

 

三: Tcp中的端口共享

   这玩意听起来大家都懂,端口共享嘛,不就是两个程序共享一个端口,对吧,在通常情况下,我们肯定会认为这无法做到,其实呢?在Wcf中我们还是可以玩

的,也就是一个PortSharingEnabled的事!!!如果说端口可以共享的话,那我们的service是不是就可以少开辟几个端口呢?同样这也方便我们进行service的管

理,下面我给大家继续演示一下。。。很好玩的,么么哒

 

可以看到,我的两个host都是用1920的端口,并且现在我真的开启起来啦。。。。好了,三种技巧都说到了,我想你在现实的wcf开发中,或多或少的都能接

触的到,希望对你有用~~~~

 

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消