WCF綁定netTcpBinding寄宿到IIS

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.