wcf将一个服务同时绑定到http和tcp的写法

服务器端:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="DCourtDbConStr" connectionString="IRyiHYdvR0dghJ+3ltmD04Hpzc1LZjqPu1H49+L4iGRsL93jzDrmhZ2Jt2KGX2vzZJRuL8SwNYhHcBUPrcVhareMc5FHq6oR1GKyzOTuYJCUB1wid/hFyZi8S+QVi/NsNcQwKzeddPoaZSCCWYQ4P8t5arJE2WgY" providerName="DbOle" />
  </connectionStrings>
  <system.serviceModel>
    <services>
      <service name="DCourt.Service.DcDataService">
<!-- 这里是用来绑定http访问方式的 -->
        <endpoint address="" binding="basicHttpBinding" name="webIn"
          contract="DCourt.Service.IDcDataService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
<!-- 这里是用来绑定tcp访问方式的 -->
        <endpoint address=""
        binding="netTcpBinding"
        contract="DCourt.Service.IDcDataService"/>
        <endpoint address="mex" binding="mexHttpBinding" name="mexIn"
          contract="IMetadataExchange" />
       
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8001/DcService"/>
            <add baseAddress="http://localhost:8000/DcService/" />
           
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- 要接收故障异常详细信息以进行调试,
          请将以下值设置为 true。在部署前设置为 false
          以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

客户端(通过8000端口或者8001端口都可以用,上面的服务器端配置同时绑定了HTTP 8000端口和TCP8001端口)


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="DCourtDbConStr" connectionString="IRyiHYdvR0dghJ+3ltmD04Hpzc1LZjqPu1H49+L4iGRsL93jzDrmhZ2Jt2KGX2vzZJRuL8SwNYhHcBUPrcVhareMc5FHq6oR1GKyzOTuYJCUB1wid/hFyZi8S+QVi/NsNcQwKzeddPoaZSCCWYQ4P8t5arJE2WgY" providerName="DbOle" />
  </connectionStrings>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IDcDataService">
          <security mode="None">
            <transport clientCredentialType="None" />
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
      <netTcpBinding>
        <binding name="NetTcpBinding_IDcDataService" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
            hostNameComparisonMode="StrongWildcard" listenBacklog="10"
            maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
            maxReceivedMessageSize="65536">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
              enabled="false" />
          <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
            <message clientCredentialType="Windows" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8000/DcService/" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IDcDataService" contract="DcDataSer.IDcDataService"
        name="BasicHttpBinding_IDcDataService" />
      <endpoint address="net.tcp://localhost:8001/DcService" binding="netTcpBinding"
        bindingConfiguration="NetTcpBinding_IDcDataService" contract="DcDataSer.IDcDataService"
        name="NetTcpBinding_IDcDataService" />
    </client>
  </system.serviceModel>
</configuration>


客户端代码
//using (DcDataServiceClient client = new DcDataServiceClient())//这个时候要注释掉其中一个endpoint
            //using (DcDataServiceClient client = new DcDataServiceClient("NetTcpBinding_IDcDataService"))
            using (DcDataServiceClient client = new DcDataServiceClient("NetTcpBinding_IDcDataService"))
            {
                DCArea[] lstAreas = client.GetAllDCAreas();
                foreach (DCArea area in lstAreas)
                {
                    ShowDbgInfo(area.DcName);
                }
            }

原文地址:https://www.cnblogs.com/lykbk/p/fgjhghfghfgh4564565465.html