這篇文章主要講解了“WCF自托管宿主是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“WCF自托管宿主是什么”吧!
創(chuàng)新互聯(lián)公司是一家專業(yè)提供東乃企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、成都做網(wǎng)站、html5、小程序制作等業(yè)務(wù)。10年已為東乃眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
利用WCF提供的ServiceHost<T>提供的Open()和Close()方法,可以便于開發(fā)者在控制臺(tái)應(yīng)用程序,Windows應(yīng)用程序乃至于ASP.NET應(yīng)用程序中托管服務(wù)。不管自宿主的環(huán)境是何種應(yīng)用程序,實(shí)質(zhì)上托管服務(wù)的方式都是一致的。例如在控制臺(tái)應(yīng)用程序中:
using (ServiceHost host = new ServiceHost(typeof(DocumentsExplorerService))) { host.Open(); Console.WriteLine("The Service had been launched."); Console.Read(); }
ServiceHost實(shí)例是被創(chuàng)建在應(yīng)用程序域中,因此我們必須保證宿主進(jìn)程在調(diào)用服務(wù)期間不會(huì)被關(guān)閉,因此我們利用Console.Read() 來阻塞進(jìn)程,以使得控制臺(tái)應(yīng)用程序能夠一直運(yùn)行,直到認(rèn)為地關(guān)閉應(yīng)用程序。如果是Windows應(yīng)用程序,則可以將創(chuàng)建ServiceHost實(shí)例的代碼放在主窗體的相關(guān)代碼中,保證服務(wù)WCF自托管宿主不會(huì)被關(guān)閉。相應(yīng)地,我們需要配置應(yīng)用程序的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>
注意,配置文件中的服務(wù)名必須包含服務(wù)契約以及服務(wù)類的命名空間。此外,在配置文件中我通過<baseAddresses>標(biāo)簽為服務(wù)添加了基地址,因此在endpoint中,address為""。
感謝各位的閱讀,以上就是“WCF自托管宿主是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)WCF自托管宿主是什么這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)站題目:WCF自托管宿主是什么
網(wǎng)站地址:http://www.sd-ha.com/article46/geghhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、電子商務(wù)、定制開發(fā)、App開發(fā)、軟件開發(fā)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)