WCFEndpoint作用体现

WCF是由微软公司开发的一种功能强大的开发插件,主要应用于.NET Framework 3.5,可以帮助我们实现许多特定的功能需求。在这里我们竟会为大家详细介绍一下有关WCF Endpoint的相关应用方法。#t#

网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了信丰免费建站欢迎大家使用!

每一个 WCF 服务都会关系到地址(Address)、绑定(Binding)和契约(Contract),而 WCF 则通过 Endpoint 将 ABC 三个方面联系在一起。每一个 Endpoint 都必须包括 ABC 三个方面,缺一不可,而 host 进程会提供WCF Endpoint供客户端调用。每个 Endpoint 都对应一个唯一地址,但是多个 Endpoint 可以共享相同的绑定和契约,每个服务又可以提供多个 Endpoint 供客户端掉用。

使用配置文件

再次体现 Microsoft 的傻瓜式编程。唯一值得注意的地方是在 service 节点中添加了 behaviorConfiguration 属性。

 
 
 
  1. < ?xml version="1.0"?> 
  2. < configuration xmlns="http://schemas.microsoft.com/.
    NetConfiguration/v2.0"> 
  3. < system.serviceModel> 
  4. < services> 
  5. < !--< service name="MyService" 
    behaviorConfiguration="returnFaults"> 
  6. < endpoint contract="IMyService" binding="wsHttpBinding"/> 
  7. < /service>--> 
  8. < service name="Anrs.Service.AnrsService" 
    behaviorConfiguration="returnFaults"> 
  9. < endpoint contract = "Anrs.Service.IAnrsServiceContract1" 
  10. binding = "wsHttpBinding" 
  11. address = "http://localhost:4021/AnrsServiceByIIS/AnrsService/" /> 
  12. < /service> 
  13. < /services> 
  14. < behaviors> 
  15. < serviceBehaviors> 
  16. < behavior name="returnFaults" > 
  17. < serviceMetadata httpGetEnabled="true">< /serviceMetadata> 
  18. < serviceDebug includeExceptionDetailInFaults="true" /> 
  19. < /behavior> 
  20. < /serviceBehaviors> 
  21. < /behaviors> 
  22. < /system.serviceModel> 
  23. < system.web> 
  24. < compilation debug="true"/> 
  25. < /system.web> 
  26. < /configuration> 

 

使用配置文件的好处自不待言,无论是修改了服务的地址、绑定还是契约,都不需要重新编译甚至部署。配置完成后,就能在浏览器中看到如下的画面了。

编程控制WCF Endpoint

相对于配置文件的简单,编程控制 Endpoint 也不会多几行代码。下面的代码就相当于上面的配置文档。

 
 
 
  1. using System;  
  2. using System.ServiceModel;  
  3. using System.ServiceModel.Channels;  
  4. namespace Anrs.Service  
  5. {  
  6. class Program  
  7. {  
  8. static void Main(string[] args)  
  9. {  
  10. ServiceHost sh = new ServiceHost(typeof(AnrsService));  
  11. Binding wsHttpBinding = new WSHttpBinding();  
  12. sh.AddServiceEndpoint(typeof(IAnrsServiceContract1),  
  13. wsHttpBinding,  
  14. new Uri("http://localhost:8086/AnrsService/"));  
  15. sh.Open();  
  16. Console.Write("Press any key to exit");  
  17. Console.ReadLine();  
  18. sh.Close();  
  19. }  
  20. }  

WCF Endpoint的相关内容就为大家介绍到这里。

当前文章:WCFEndpoint作用体现
分享路径:http://www.shufengxianlan.com/qtweb/news27/261277.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联