开发者

Implementing WCF using netTCP protocol

开发者 https://www.devze.com 2023-02-12 19:33 出处:网络
Having successfully got a WCF service and client to talk to one another using Http, I\'m now trying to swtich it to netTcp so that I can encrypt the communication. However I\'m having trouble getting

Having successfully got a WCF service and client to talk to one another using Http, I'm now trying to swtich it to netTcp so that I can encrypt the communication. However I'm having trouble getting the connection to establish under this protocol.

Having changed my App.config file on the service and deleted the service reference in the client project, I'm trying to re-add the service reference. Clicking on "discover" finds the service but on http with the address http://localhost:31284/MyService.svc which isn't an address I've specified anywhere: I assume it must be a default. If I try and add this service I get the error:

The HTML document does not contain Web service discovery information. Metadata contains a reference that cannot be resolved: 'http://localhost:31284/MyService.svc'. Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:31284/MyService.svc. The client and service bindings may be mismatched. The remote server returned an error: (415) Unsupported Media Type. If the service is defined in the current solution, try building the solution and adding the service reference again.

In my app config I've specified the endpoint address as net.tcp://localhost:8732/Design_Time_Addresses/NameSpace.ApiServices.Server/MyService. If I put this address into the service reference box and try to connect to it or try to connect using WCF test client it also fails. Both kick up very similar errors:

Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8732/Design_Time_Addresses/NameSpace.ApiServices.Server/MyService'. Could not connect to net.tcp://localhost:8732/Design_Time_Addresses/NameSpace.ApiServices.Server/MyService. The connection attempt lasted for a time span of 00:00:02.0142014. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8732. No connection could be made because the target machine actively refused it 127.0.0.1:8732 If the service is defined in the current solution, try building the solution and adding the service reference again.

Not sure what's going on. Both are running on the same machine and my firewall is turned off. I checked netstat -a and couldn't see TCP listening on port 8732 but I have got the port sharing service turned on and enabled in my config. Which looks like this:

<bindings>
  <netTcpBinding>
    <binding portSharingEnabled="True" name="tcpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security>
        <transport>
          <extendedProtectionPolicy policyEnforcement="Never" />
        </transport>
      </security>
    </binding>
  </netTcpBinding>
</bindings>

<behaviors>
  <serviceBehaviors>
    <behavior name="standard">
      <serviceDebug includeExceptionDetailInFaults="false" />
      <serviceMetadata />
    </behavior>
  </serviceBehaviors>
</behaviors>

<services>
  <service name="NameSpace.ApiServices.Server.MyService" behaviorConfiguration="standard">
    <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpBinding" contract="NameSpace.ApiServices.Server.IMyService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8732/Design_Time_Addresses/NameSpace.ApiServices.Server/MyService/" />
      </baseAddresses>
    </host>
  </service>
</services>

Can anyone see what I'm doing wrong?

EDIT: I was asked for my client config:

    <bindings>
      <netTcpBinding>
        <binding name="tcpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCha开发者_如何学运维rCount="2147483647" />
          <security>
            <transport>
              <extendedProtectionPolicy policyEnforcement="Never" />
            </transport>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
        <endpoint address=""
            binding="netTcpBinding" bindingConfiguration="tcpBinding"
            contract="NameSpace.Server.IUpdateCustomers" name="tcpBinding" />
    </client>
</system.serviceModel>


OK, finally got to the bottom of it, if it helps anyone else. The serviceMetdata tag needs to have the httpGetEnabled attribute set to false:

 <serviceMetadata httpGetEnabled="false" />

Otherwise, I assume, it's trying to spin up the endpoint on Http as well as Tcp and failing.


After you created the tcp endpoint in the server, you have to make sure that you update the reference at the client end also. The first thing I would check is if the endpoints are compatible on the server and client. Check in app.config on the client side, and check that the tcp endpoint is set up properly here as well, and not just the old http endpoint.

Also, I believe that the mex endpoint for TCP cannot run on the same port as the service, so try exposing the mex endpoint on another tcp port than the main service endpoint.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号