Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetTcpBinding - Self-Hosted WCF - Can't get client connected

Trying to get a simple demo of NetTcpBinding working in order to expand it into another project.

Architecture: 2 console apps (1 host/server, 1 client) and 1 type library project. Both console apps have a reference to the type library project.

Host application:

class Program
{ 
    static void Main()
    {
        var netTcpBinding = new NetTcpBinding(SecurityMode.None)
        {
            PortSharingEnabled = true
        };

        var netTcpAdddress = new Uri("net.tcp://127.0.0.1:1234/HelloWorldService/");

        var tcpHost = new ServiceHost(typeof(HelloWorldService), netTcpAdddress);
        tcpHost.AddServiceEndpoint(typeof(IHelloWorld), netTcpBinding, "IHelloWorld");

        tcpHost.Open();
        Console.WriteLine($"tcpHost is {tcpHost.State}.  Press enter to close.");

        Console.ReadLine();
        tcpHost.Close();
    }
}


public class HelloWorldService : IHelloWorld
{
    public void HelloWorld()
    {
        Console.WriteLine("Hello World!");
    }

    public void WriteMe(string text)
    {
        Console.WriteLine($"WriteMe: {text}");
    }
}

Client application:

    static void Main()
    {
        Console.WriteLine("Press enter when the service is opened.");
        Console.ReadLine();


        var endPoint = new EndpointAddress("net.tcp://127.0.0.1:1234/HelloWorldService/");
        var binding = new NetTcpBinding ();
        var channel = new ChannelFactory<IHelloWorld>(binding, endPoint);
        var client = channel.CreateChannel();

        try
        {
            Console.WriteLine("Invoking HelloWorld on TcpService.");
            client.HelloWorld();
            Console.WriteLine("Successful.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Exception: {ex.Message}");
        }

        Console.WriteLine("Press enter to quit.");
        Console.ReadLine();
    }

Type Library:

[ServiceContract]
public interface IHelloWorld
{
    [OperationContract]
    void HelloWorld();

    [OperationContract]
    void WriteMe(string text);
}

I believe I have all necessary services installed and running:

enter image description here

Obviously I'm trying to do all the config at runtime.

I consistently get this error message on the client:

Invoking HelloWorld on TcpService.

Exception: There was no endpoint listening at net.tcp://127.0.0.1:1234/HelloWorldService/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. Press enter to quit.

Am I missing something obvious?

like image 596
Wesley Long Avatar asked Jan 17 '26 13:01

Wesley Long


1 Answers

Your service is exposing the endpoint at address:

net.tcp://127.0.0.1:1234/HelloWorldService/IHelloWorld

but your client is connecting to:

net.tcp://127.0.0.1:1234/HelloWorldService/

You'll also need to set the client NetTcpBinding SecurityMode the same as the server (None).

like image 196
lesscode Avatar answered Jan 20 '26 03:01

lesscode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!