none
WCF NetTcp Error RRS feed

  • Question

  • Hi I am trying to implement net.tcp binding. The service starts and listens at specified uri. But when the client tries to connect I get error:

    An unhandled exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in mscorlib.dll Additional information: Could not connect to net.tcp://localhost:8077/Service1. The connection attempt lasted for a time span of 00:00:02.0921196. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8077.

    I am following through with Pluralsight coutrse. I created a much simpler application but still get the same error. 

    Wcf service code:

    namespace WcfService1
    {
        
        [ServiceContract]
        public interface IService1
        {
    
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            CompositeType GetDataUsingDataContract(CompositeType composite);
    
            // TODO: Add your service operations here
        }
       
        [DataContract]
        public class CompositeType
        {
            bool boolValue = true;
            string stringValue = "Hello ";
    
            [DataMember]
            public bool BoolValue
            {
                get { return boolValue; }
                set { boolValue = value; }
            }
    
            [DataMember]
            public string StringValue
            {
                get { return stringValue; }
                set { stringValue = value; }
            }
        }
    }
    
    namespace WcfService1
    {  
        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;
            }
        }
    }
    

    wcf host :

    namespace wcfserviceHost
    {
           class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ServiceHost(typeof   (WcfService1.Service1));         
                
                host.Open();
                Console.WriteLine(host.Description.Endpoints[0].Address);  
    
    
            }
        }
    }
    Client code:
    namespace ConsumewcfApp
    {
        public class Prox : ClientBase<WcfService1.IService1>,WcfService1.IService1
        {
            public string GetData(int value)
            {
                           return Channel.GetData(value);
            }
    
              public WcfService1.CompositeType GetDataUsingDataContract(WcfService1.CompositeType composite)
            {
                throw new NotImplementedException();
            }
        }
        
       
        class Program
        {
            static void Main(string[] args)
            {
               
                Prox px = new Prox();
                
             Console.WriteLine(   px.GetData(9154));
             Console.Read();
    
               
            }
        }
    }
    

    host App config    
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
        </startup>
      
      
     <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding receiveTimeout="00:20:00" sendTimeout="00:02:00"/>
          </netTcpBinding>
        </bindings>
    
        <services>
          <service name="WcfService1.Service1">
            <endpoint address="net.tcp://localhost:8077/Service1"
                      binding="netTcpBinding" contract="WcfService1.IService1"/>
          </service>     
         
        </services>
      </system.serviceModel>
    </configuration>
    

    client App config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
        </startup>
      <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding receiveTimeout="00:20:00" sendTimeout="00:02:00"/>
          </netTcpBinding>
        </bindings>
        <client>
          <endpoint address="net.tcp://localhost:8077/Service1" binding="netTcpBinding"
                    contract="WcfService1.IService1"/>
         
        </client>
      </system.serviceModel>
    </configuration>
    

    Thursday, November 14, 2019 4:06 PM

Answers

  • Hi,
    First, there is something wrong with the process of hosting the service.
    We need to add the below code to prevent the service host from automatically closing.

      ServiceHost host = new ServiceHost(typeof(Service1));
                host.Open();
                Console.WriteLine(host.Description.Endpoints[0].Address);
    //add the below code to pause.
                Console.ReadLine();
                host.Close();


    The other question is the way we call the service. manually implementing a client proxy might not be an advisable idea, I suggest you consume the service by adding service reference or using Channel Factory.
    Adding service reference will generate a client proxy class as well as the SVCUtil.exe tools, please refer to the below link.
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
    Channel Factory will create a channel to communication with server. As with calling services through a client proxy, we need to make sure that the configuration is consistent between the server-side and the client-side.
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/channel-factory
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory

    I have made a simple demo, wish it is useful to you.
    Server-side.

    class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ServiceHost(typeof(Service1));
                host.Open();
                Console.WriteLine(host.Description.Endpoints[0].Address);
                Console.ReadLine();
                host.Close();
            }
        }
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string GetData(int value);
        }
        public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
        }
    


    Appconfig(server-side).

    <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding receiveTimeout="00:20:00" sendTimeout="00:02:00"/>
          </netTcpBinding>
        </bindings>
        <services>
          <service name="ConsoleApp4.Service1">
            <endpoint address="net.tcp://localhost:8077/Service1"
                      binding="netTcpBinding" contract="ConsoleApp4.IService1"/>
          </service>     
        </services>
      </system.serviceModel>
    

    Client-side.

    class Program
        {
            static void Main(string[] args)
            {
                Uri uri = new Uri("net.tcp://10.157.13.69:8077/service1");
                NetTcpBinding binding = new NetTcpBinding();
                ChannelFactory<IService1> factory = new ChannelFactory<IService1>(binding, new EndpointAddress(uri));
                factory.Credentials.Windows.ClientCredential.UserName = "administrator";
                factory.Credentials.Windows.ClientCredential.Password = "abcd1234!";
                IService1 service = factory.CreateChannel();
                var result = service.GetData(34);
                Console.WriteLine(result);
            }
        }
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string GetData(int value);
    }

    Result.
    https://i.stack.imgur.com/x7AT1.png
    Finally,the default authentication mode of the Nettcpbinding is message security mode and authenticate the client with windows credential, if the client and the server are not the same machine, we should provide windows credential when calling the service on the server-side.

      ServiceReference6.ServiceClient client = new ServiceReference6.ServiceClient();
                client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
                client.ClientCredentials.Windows.ClientCredential.Password = "abcd1234!";
    


    Feel free to let me know if there is anything I can help with.
    Best Regards
    Abraham

    • Marked as answer by kobosh3 Monday, November 18, 2019 5:01 PM
    Friday, November 15, 2019 5:57 AM
    Moderator

All replies

  • What version of windows are you on? I believe you have to turn on Non-Http Activation

    Programs->Programs and features-> Turn windows feature on or off) -> Windows Communication Foundation Non-Http Activation


    william xifaras

    Thursday, November 14, 2019 4:36 PM
  • thanks william it is turned on. I am on windows 7 home premium
    Thursday, November 14, 2019 7:45 PM
  • Hi,
    First, there is something wrong with the process of hosting the service.
    We need to add the below code to prevent the service host from automatically closing.

      ServiceHost host = new ServiceHost(typeof(Service1));
                host.Open();
                Console.WriteLine(host.Description.Endpoints[0].Address);
    //add the below code to pause.
                Console.ReadLine();
                host.Close();


    The other question is the way we call the service. manually implementing a client proxy might not be an advisable idea, I suggest you consume the service by adding service reference or using Channel Factory.
    Adding service reference will generate a client proxy class as well as the SVCUtil.exe tools, please refer to the below link.
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
    Channel Factory will create a channel to communication with server. As with calling services through a client proxy, we need to make sure that the configuration is consistent between the server-side and the client-side.
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/channel-factory
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory

    I have made a simple demo, wish it is useful to you.
    Server-side.

    class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ServiceHost(typeof(Service1));
                host.Open();
                Console.WriteLine(host.Description.Endpoints[0].Address);
                Console.ReadLine();
                host.Close();
            }
        }
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string GetData(int value);
        }
        public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
        }
    


    Appconfig(server-side).

    <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding receiveTimeout="00:20:00" sendTimeout="00:02:00"/>
          </netTcpBinding>
        </bindings>
        <services>
          <service name="ConsoleApp4.Service1">
            <endpoint address="net.tcp://localhost:8077/Service1"
                      binding="netTcpBinding" contract="ConsoleApp4.IService1"/>
          </service>     
        </services>
      </system.serviceModel>
    

    Client-side.

    class Program
        {
            static void Main(string[] args)
            {
                Uri uri = new Uri("net.tcp://10.157.13.69:8077/service1");
                NetTcpBinding binding = new NetTcpBinding();
                ChannelFactory<IService1> factory = new ChannelFactory<IService1>(binding, new EndpointAddress(uri));
                factory.Credentials.Windows.ClientCredential.UserName = "administrator";
                factory.Credentials.Windows.ClientCredential.Password = "abcd1234!";
                IService1 service = factory.CreateChannel();
                var result = service.GetData(34);
                Console.WriteLine(result);
            }
        }
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string GetData(int value);
    }

    Result.
    https://i.stack.imgur.com/x7AT1.png
    Finally,the default authentication mode of the Nettcpbinding is message security mode and authenticate the client with windows credential, if the client and the server are not the same machine, we should provide windows credential when calling the service on the server-side.

      ServiceReference6.ServiceClient client = new ServiceReference6.ServiceClient();
                client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
                client.ClientCredentials.Windows.ClientCredential.Password = "abcd1234!";
    


    Feel free to let me know if there is anything I can help with.
    Best Regards
    Abraham

    • Marked as answer by kobosh3 Monday, November 18, 2019 5:01 PM
    Friday, November 15, 2019 5:57 AM
    Moderator
  • Thanks Abraham will try it and comeback
    Friday, November 15, 2019 12:25 PM
  • Hi Abraham  your  service is console application and I assume it contains both service and host? because I cannot see name spaces in the sample it is difficult for me to find out. Please can you clarify if you have both service and host in one project. Any way I tried it that way and I am getting errorr when I start host/service:"The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail.
    Press any key to continue . . ."
    Friday, November 15, 2019 1:02 PM
  • Hi,
    I host the service and consume the service in the different machine, therefore I provide the window credential on the client console application. The windows credential is the windows account on the side-side.
    The server is a console application and create the service host in the Main method, configure the service settings in the System.servicemode section the Appconfig file.
    Due to the fact that the machine port occupation requires permission, we had better run the server console application with administrative privilege.
    Feel free to let me know if the problem still exists.
    Best Regards
    Abraham

    Monday, November 18, 2019 2:32 AM
    Moderator
  • thanks it is working now
    Monday, November 18, 2019 5:02 PM