WCF Service Base Address Http and netTcp

2020-06-03 04:02发布

问题:

I have two base addresses defined in my WCF Service config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>      
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
        propagateActivity="true">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="C:\WCF Service Logs\app_tracelog.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelTraceListener" traceOutputOptions="DateTime, Timestamp">
        <filter type="" />
      </add>
    </sharedListeners>
  </system.diagnostics>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="netTcp" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000">
          <readerQuotas maxDepth="500" maxStringContentLength="50000000" maxArrayLength="50000000" maxBytesPerRead="50000000" maxNameTableCharCount="50000000" />
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="ReportingComponentLibrary.TemplateServiceBehavior"
        name="ReportingComponentLibrary.TemplateReportService">
        <endpoint address="TemplateService" binding="netTcpBinding" bindingConfiguration="netTcp"
          contract="ReportingComponentLibrary.ITemplateService"></endpoint>
        <endpoint address="ReportService" binding="netTcpBinding" bindingConfiguration="netTcp"
          contract="ReportingComponentLibrary.IReportService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8001/TemplateReportService" />
            <add baseAddress="http://localhost:8181/TemplateReportService"  />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ReportingComponentLibrary.TemplateServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

And although I have set endpoint binding as netTcpBinding,
I am only able to access my WCF service with base address:

http://localhost:8181/TemplateReportService

and not with

net.tcp://localhost:8001/TemplateReportService

How can I make my service access with netTcp address?

回答1:

You defined a Net.TCP base address to be:

net.tcp://localhost:8001/TemplateReportService

Your endpoints with Net TCP are:

<endpoint address="TemplateService" 

and

<endpoint address="ReportService" 

So their complete service address will be "netTcp base address" + "relative address as defined on the <endpoint> element" - this gives:

net.tcp://localhost:8001/TemplateReportService/TemplateService

and

net.tcp://localhost:8001/TemplateReportService/ReportService

respectfully.

Can you use them at these addresses??

Also - you defined a "mex" (metadata exchange) endpoint for the HTTP protocol - that's why you can see something when you navigate to the HTTP address. But you did not specify a MEX endpoint for netTcp.



回答2:

Due to not having answers clear enough , I decided to find out by my self the best solution. Here is what you have to do to configure a WCF NET TCP service endpoint in IIS 7.0.

To achieve this, we need two steps:

  1. Configure a config file for WCF net tcp endpoint
  2. Configure IIS for net tcp

1. Configure a config file for WCF net tcp endpoint

Below is a typical config file, notice that the value for "address", in service's endpoint, is empty.

Also notice that inside the node "host" Im adding 2 base addresses, an http and a net tcp protocols respectly. Since the service is hosted in IIS you don't have to worry about the absolute address in the endpoint, IIS figures this out based on the base address we have defined inside the node "host".

    <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviorConfig">
          <!--your custom behavior here-->
        </behavior>
      </serviceBehaviors>
    </behaviors>
     <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBindingConfig"/>
      </basicHttpBinding>
      <netTcpBinding>
        <!--our netTcpBinding binding-->
        <binding name="netTcpBindingConfig"/>   
      </netTcpBinding>
    </bindings>
    <services>
      <service name="MyNamespace.ServiceLayer.MyService" behaviorConfiguration="behaviorConfig">
        <!--Endpoint for basicHttpBinding-->
        <endpoint address="" binding="basicHttpBinding" contract="MyNamespace.ServiceLayer.IMyService" bindingConfiguration="basicHttpBindingConfig"/>
        <!--Endpoint for netTcpBindingConfig-->
        <endpoint address="" binding="netTcpBinding" contract="MyNamespace.ServiceLayer.IMyService" bindingConfiguration="netTcpBindingConfig">
        <identity>
            <dns value="localhost" />
        </identity>
    </endpoint>
    <!--Make sure you add a mexTcpBinding, without it you will receive an error when trying to resolve service's reference-->
    <endpoint address="mex" 
              binding="mexTcpBinding" 
              bindingConfiguration=""
              name="MyServiceMexTcpBidingEndpoint" 
              contract="IMetadataExchange" />
        <host>
            <!--This part is important for IIS to determine the base addresses for your endpoints-->
            <baseAddresses>            
                 <!--Notice that baseAddress for net tcp is preceded by net.tcp -->
                 <!--then ://localhost:{port} -->
                 <!--Also the same for http, so you can figure out how to define a baseAddress for https-->
               <add baseAddress="net.tcp://localhost:8090"/>
               <add baseAddress="http://localhost:8080"/>
            </baseAddresses>
        </host>
      </service>
    </services>    
  </system.serviceModel>

2. Configure IIS for net tcp

In IIS, (after updated your service with new configuration) enable the net.tcp protocol. These are the steps:

  • Open IIS.
  • In sites list, find your "site" where your wcf is hosted, then right-click on it
  • then, select "Edit Bindings",
  • a list of bindings appears (be sure that net.tcp is no there), then add your net.tcp configuration,
  • click "Add",
  • Type net.tcp (or select net.tcp from a dropdown list if it is available)
  • in binding Information type: 8090:* (make sure it's the same port as in host > base address you'd added)
  • then close this window and restart your service.

After doing this, there is also another configuration:

  • Open IIS
  • In sites list, find your "site" where your wcf is hosted, then right-click on it
  • Click "Advanced Settings"
  • Select Enabled Protocols
  • Set the value to http,net.tcp (be sure that there are not blanks between http,net.tcp)
  • Restart your service

Also, if you still cannot access service, you might need to start your Net.tcp listener adapter in your server. To achieve this, please follow next steps:

  • Go to ServerManager -> Configuration -> Services, stop the Net.Tcp Listener Adapter and the Net.Tcp Port Sharing Service. Then start them again.

That's all you have to do to have a nettcp WCF endpoint enabled in IIS.

Hope this helps.

Regards



标签: .net net.tcp wcf