How to configure net.tcp binding for client in WCF

Dhaval Panchal picture Dhaval Panchal · Dec 25, 2013 · Viewed 60.4k times · Source

I'm new to WCF services and I have created a same application using WCF. Please see below the basic code:

IService1.cs:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    [OperationContract]
    String WelComeMessage(String name);
}

Service1.svc.cs:

public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }

        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }

        return composite;
    }

    public String WelComeMessage(String name)
    {
        return String.Format("{0}, Welcome to http://www.csharptutorial.in", name);
    }
}

and web.config:

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

I want to make this service net.tcp binding ready, can anyone help me to understand where to declare the net.tcp binding and how to consume this service using net.tcp binding?

Edit 1:

I have updated the Server configuration file (in WCF service configuration file) below:

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="DemoWCF.Service1"
               behaviorConfiguration="MyServiceBehavior">
        <endpoint name="NetTCPBinding_IService1Client"
                  address="net.tcp://localhost:10109/Service1.svc"
                  binding="netTcpBinding"
                  bindingConfiguration="EndPointConfiguration"
                  contract="DemoWCF.IService1" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:10109/Service1" />
          </baseAddresses>
        </host>
      </service>
    </services>

    <bindings>
      <netTcpBinding>
        <binding name="NetTCPBinding_IService1"  sendTimeout="00:01:00">
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <dataContractSerializer maxItemsInObjectGraph="65536" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

And below is the client side code:

static void Main(string[] args)
        {
            try
            {
                Service1Client service1Client = new Service1Client("NetTCPBinding_IService1");
                Console.WriteLine(service1Client.WelcomeMessage("Hello from net.tcp"));
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

But it throws an exception:

Exception: You have tried to create a channel to a service that does not support .Net Framing. It is possible that you are encountering an HTTP endpoint.

Inner Exception:

{"Expected record type 'PreambleAck', found '72'."}

Edit 2:

I have hosted my WCF service in the IIS 7.5 and added net.tcp binding using edit binding... option. as well as from command line:

set site "DemoWCF" -+bindings.[protocol='net.tcp',bindingInformation='10108:*']

Web service shows running perfectly.

enter image description here

But consuming this WCF service at client side still the same exception.

Client Code:

try
{
    ServiceReference1.Service1Client s = new ServiceReference1.Service1Client();
    Response.Write(s.WelcomeMessage("Hello from net.tcp"));
    Response.End();
}
catch (Exception ex)
{
    Response.Write(ex);
    Response.End();
}

Client Web.config:

<bindings>
  <netTcpBinding>
    <binding name="NetTCPBinding_IService1Client">
      <security mode="None" />
    </binding>
  </netTcpBinding>
</bindings>
<client>
  <endpoint address="net.tcp://localhost:10108/Service1.svc" binding="netTcpBinding"
    bindingConfiguration="NetTCPBinding_IService1Client" contract="ServiceReference1.IService1"
    name="NetTCPBinding_IService1Client" />
</client>

Same Exception: You have tried to create a channel to a service that does not support .Net Framing. It is possible that you are encountering an HTTP endpoint.

Inner Exception:

{"Expected record type 'PreambleAck', found '72'."}

Edit 3:

Tried consuming the service by below code too:

IService1 vService;

                NetTcpBinding b = new NetTcpBinding();
                b.Security.Mode = SecurityMode.Message;
                b.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

                EndpointAddress vEndPoint = new EndpointAddress("net.tcp://localhost:10108/Service1/");
                ChannelFactory<IService1> cf = new ChannelFactory<IService1>(b, vEndPoint);

                vService = cf.CreateChannel();

                try
                {
                    Response.Write("Result from the system " + vService.WelcomeMessage("Hello"));
                }
                catch (Exception ex)
                {
                    Response.Write("Error Occured while connection establish to " + vEndPoint.Uri.ToString());
                }
                Response.End();

Still getting the same exception, Can anyone help?

Answer

Sergey Yakovlev picture Sergey Yakovlev · Dec 25, 2013

You need something similar to this

<system.serviceModel>
<services>
  <service name="MyApp.Service1"
           behaviorConfiguration="MyServiceBehavior">
    <endpoint name="MyEndPoint"
              address="net.tcp://localhost:9000/Service1"
              binding="netTcpBinding"
              bindingConfiguration="EndPointConfiguration"
              contract="MyApp.IService1" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:9000/Service1" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior">
      <serviceDebug includeExceptionDetailInFaults="True" />
      <dataContractSerializer maxItemsInObjectGraph="65536" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <netTcpBinding>
    <binding name="EndPointConfiguration"
             sendTimeout="00:01:00">
      <security mode="None" />
    </binding>
  </netTcpBinding>
</bindings>

'MyApp.Service1' - Means the full name of the service, including namespace. 'MyApp.IService1' - Means the full name of the service interface, including the namespace that the service implements.