WCF中的NetTcp和HTTP绑定

时间:2015-01-21 10:17:34

标签: c# asp.net wcf http tcp

我现在已经摆弄了两天,试图让我的TCP端点在我的Wcf应用程序中运行。

我已经在IIS8(非IIS Express)中托管了我的Wcf应用程序,并将网站配置为在端口808上启用net.tcp协议侦听。

但无论我做什么,我都无法到达TCP端点。 HTTP端点正在按预期工作。

这是我的Web.config:

    <configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="DefaultHttpBinding" />
      </basicHttpBinding>
      <netTcpBinding>
        <binding name="DefaultTCPBinding" portSharingEnabled="true">
          <security mode="None"/>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="SimplBehavior" name="SimplWCF.WcfService">
        <endpoint address="mextcp" binding="mexTcpBinding" contract="IMetadataExchange"/>
        <endpoint address="local" binding="netTcpBinding" contract="SimplWCF.IWcfService" bindingConfiguration="DefaultTCPBinding" name="localTcpBinding"/>
        <!-- Used for connecting the service to CMS-->
        <endpoint address="mexhttp" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint address="public" binding="basicHttpBinding" contract="SimplWCF.IWcfService" bindingConfiguration="DefaultHttpBinding" name="publicHttpBinding"/>
        <!-- Used for connecting the webpages to the service-->
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:50356/SimplWCF/" />
            <add baseAddress="net.tcp://localhost:808/SimplWCF/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="SimplBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
      <add binding="basicHttpBinding" scheme="http"/>
      <add binding="netTcpBinding" scheme="net.tcp"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

我知道HTTP端点无法与httpGetEnabled="false"一起正常工作,但是现在我只需要我的TCP端点就可以工作。

当我尝试在Visual Studio中的另一个项目中添加服务引用时(在命名空间中作为我的WCF),我写这样的URL:

  

的net.tcp://本地主机:808 / SimplWCF / WcfService.svc

  • 但我并非100%确定这是正确的做法。
希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

我终于明白了! 我删除了TCP端点中的相对地址,因此我的配置现在看起来像这样:`

<services>
      <service behaviorConfiguration="SimplBehavior" name="SimplWCF.WcfService">
        <endpoint address="mextcp" binding="mexTcpBinding" contract="IMetadataExchange"/>
        <endpoint address="" binding="netTcpBinding" contract="SimplWCF.IWcfService" bindingConfiguration="DefaultTCPBinding" name="localTcpBinding"/>
        <!-- Used for connecting the service to CMS-->
        <endpoint address="mexhttp" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint address="public" binding="basicHttpBinding" contract="SimplWCF.IWcfService" bindingConfiguration="DefaultHttpBinding" name="publicHttpBinding"/>
        <!-- Used for connecting the webpages to the service-->
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:50356/SimplWCF/" />
            <add baseAddress="net.tcp://localhost:808/SimplWCF/"/>
          </baseAddresses>
        </host>
      </service>
    </services>

感谢您的时间!

相关问题