简体   繁体   中英

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:

在此输入图像描述

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?

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM