温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

WCF自托管宿主是什么

发布时间:2021-12-15 09:15:16 来源:亿速云 阅读:108 作者:iii 栏目:编程语言

这篇文章主要讲解了“WCF自托管宿主是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“WCF自托管宿主是什么”吧!

利用WCF提供的ServiceHost<T>提供的Open()和Close()方法,可以便于开发者在控制台应用程序,Windows应用程序乃至于ASP.NET应用程序中托管服务。不管自宿主的环境是何种应用程序,实质上托管服务的方式都是一致的。例如在控制台应用程序中:

using (ServiceHost host = new ServiceHost(typeof(DocumentsExplorerService)))   {   host.Open();    Console.WriteLine("The Service had been launched.");   Console.Read();   }

ServiceHost实例是被创建在应用程序域中,因此我们必须保证宿主进程在调用服务期间不会被关闭,因此我们利用Console.Read() 来阻塞进程,以使得控制台应用程序能够一直运行,直到认为地关闭应用程序。如果是Windows应用程序,则可以将创建ServiceHost实例的代码放在主窗体的相关代码中,保证服务WCF自托管宿主不会被关闭。相应地,我们需要配置应用程序的app.config配置文件:

<configuration>   <system.serviceModel>   <services>   <service name="BruceZhang.WCF.DocumentsExplorerServiceImplementation.DocumentsExplorerService" behaviorConfiguration="DocumentExplorerServiceBehavior">   <host>   <baseAddresses>   <add baseAddress="http://localhost:8008/DocumentExplorerService"/>   </baseAddresses>   </host>   <endpoint   address=""   binding="basicHttpBinding"   bindingConfiguration="DocumentExplorerServiceBinding"   contract="BruceZhang.WCF.DocumentsExplorerServiceContract.IDocumentsExplorerService"/>   <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>   </service>   </services>   <bindings>   <basicHttpBinding>   <binding name="DocumentExplorerServiceBinding" sendTimeout="00:10:00" transferMode="Streamed"   messageEncoding="Text" textEncoding="utf-8" maxReceivedMessageSize="9223372036854775807">   </binding>   </basicHttpBinding>   </bindings>   <behaviors>   <serviceBehaviors>   <behavior name="DocumentExplorerServiceBehavior">   <serviceMetadata httpGetEnabled="true"/>   </behavior>   </serviceBehaviors>   </behaviors>   </system.serviceModel>   </configuration>

注意,配置文件中的服务名必须包含服务契约以及服务类的命名空间。此外,在配置文件中我通过<baseAddresses>标签为服务添加了基地址,因此在endpoint中,address为""。

感谢各位的阅读,以上就是“WCF自托管宿主是什么”的内容了,经过本文的学习后,相信大家对WCF自托管宿主是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

wcf
AI