Search code examples
wcfapp-confignettcpbinding

Just another "An existing connection was forcibly closed by the remote host" issue


I'm trying to access a WCF service on a company network. I'm using the proxy and config file generated by svcutil. The code looks like this:

using (Client client = new Client())
{
    client.Open();
    client.DoStuff();
}

Client is the client class in the generated proxy code, meaning it's derived from System.ServiceModel.ClientBase and implements the Contract interface.

And the config file looks like this:

<configuration>
    <system.serviceModel>
        <client>
            <endpoint address="net.tcp://Address"
                      binding="netTcpBinding"
                      bindingConfiguration="Config"
                      contract="Namespace.Contract"
                      name="name">
                <identity>
                    <servicePrincipalName value="Host" />
                </identity>
            </endpoint>
        </client>
        <bindings>
            <netTcpBinding>
                <binding name="Config"
                         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="None">
                        <transport clientCredentialType="None" />
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
    </system.serviceModel>
</configuration>

I get the exception "An existing connection was forcibly closed by the remote host" every time the code reaches client.Open(). What really bothers is that when I try it from another computer, there's no exception. And on top of that, the config file used to have Transport security which was changed to None, and the computer where it works still has the old config file with

<security mode="Transport">
    <transport clientCredentialType="Windows"
               protectionLevel="EncryptAndSign" />
    <message clientCredentialType="Windows" />
</security>

whereas on the first machine and the server it's

<security mode="None">
    <transport clientCredentialType="None" />
</security>

What can cause this? I've read numerous other questions here regarding this exception, but none seemed to resemble my issue.

SOLVED! I have set the security options in the wrong place. The service was self hosted with ServiceHost, and the Security property wasn't set, so it defaulted to something else than None.


Solution

  • Check your Security settings for this project on your IIS (you are using IIS right?). I think it still says authentication type Windows.