標籤:exchange lan tar 連接埠 客戶 目錄 編譯 point content
繼續沿用上一篇隨筆中WCF服務類庫 Wettery.WcfContract.Services
WCF綁定netTcpBinding寄宿到控制台應用程式
服務端
添加WCF服務應用程式 Wettery.WcfIISHost.Services,其中添加兩個WCF服務,GameService.svc PlayerService.svc,刪掉契約介面和 .cs內嵌檔案,只留下.svc檔案
我們通過Autofac注入契約/服務的依賴關係,Nuget引用
Install-Package Autofac.Wcf
修改兩個svc檔案的ServiceHost聲明
<%@ ServiceHost Language="C#" Debug="true" Service="Wettery.WcfContract.Services.IGameService, Wettery.WcfContract.Services" Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf" %>
<%@ ServiceHost Language="C#" Debug="true" Service="Wettery.WcfContract.Services.IPlayerService, Wettery.WcfContract.Services" Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf" %>
添加 Global.asax,修改 Application_Start 方法,程式啟動時就注入依賴關係
protected void Application_Start(object sender, EventArgs e){ var builder = new ContainerBuilder(); //註冊其它依賴關係 //.... //註冊WCF服務依賴關係 var wcfAssembly = typeof(GameService).Assembly; builder.RegisterAssemblyTypes(wcfAssembly).AsImplementedInterfaces().AsSelf(); var container = builder.Build(); AutofacHostFactory.Container = container;}
服務端配置
修改Web.config中system.serviceModel節
<system.serviceModel> <services> <service name="Wettery.WcfContract.Services.GameService" behaviorConfiguration="mex"> <endpoint address="" binding="netTcpBinding" contract="Wettery.WcfContract.Services.IGameService" bindingConfiguration="netTcpBindingConfig"></endpoint> <!--為net.tcp綁定提供中繼資料--> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> </service> <service name="Wettery.WcfContract.Services.PlayerService" behaviorConfiguration="mex"> <endpoint address="" binding="netTcpBinding" contract="Wettery.WcfContract.Services.IPlayerService" bindingConfiguration="netTcpBindingConfig"></endpoint> <!--為net.tcp綁定提供中繼資料--> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> </service> </services> <bindings> <netTcpBinding> <binding name="netTcpBindingConfig" transferMode="Buffered" portSharingEnabled="true"> <readerQuotas maxStringContentLength="6553600" /> <security mode="None" /> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="mex"> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
IIS配置
因為IIS預設不支援非HTTP協議,所以要開啟非HTTP功能,“開啟或關閉Windows功能”中選中HTTP和非HTTP啟用
編譯WCF應用程式,在IIS中添加一個網站,指向WCF目錄,綁定兩個協議,HTTP用21335連接埠,NET.TCP用21336連接埠
網站,進階設定中啟用net.tcp協議
用戶端測試
還是用wcftestclient工具測試,WCF服務中繼資料地址:
http://localhost:21335/GameService.svc
http://localhost:21335/PlayerService.svc
如果Invoke發生錯誤
通常是沒開啟這兩個服務,開啟即可
Net.Tcp Listener Adapter
Net.Tcp Port Sharing Service
WCF綁定netTcpBinding寄宿到IIS