简体   繁体   中英

WCF Service is not hosted on the machine

I have implemented a simple chat console app and it worked well. When i tried to apply the same concept on GUI app. the service side when hosting , there is any error but if i use CMD command netstat -ao to show all ports , it is not exists.So when i run client app , there is an Exception (No connection could be made because the target machine actively refused). How can i solve these problem ?

Server

ServiceHost host;
using (host = new ServiceHost(typeof(Service), new Uri("net.tcp://localhost:4111")))
{
    host.AddServiceEndpoint(typeof(IService), new NetTcpBinding(), "IService");

    try
    {
        host.Open();

    }
    catch
    {
    }
}

Client

public bool Connect()
{
    DuplexChannelFactory<IService> pipeFactory = new DuplexChannelFactory<IService>(new InstanceContext(this),
                                                                                    new NetTcpBinding(),
                                                                                    new EndpointAddress(AppConfiguration.GetValue(net.tcp://localhost:4111/IService"));

    try
    {
        pipeProxy = pipeFactory.CreateChannel();
        if (pipeProxy.Register())
        {
            return true;
        }
    }
    catch
    {
    }
    return false;
}

Assuming you are showing all your code.

You need to add a line after host.Open();, you could add Console.ReadLine();

This would get the program to stop from existing. What happens now is that the host opens, then the program exists, and the host gets closed/garbage collected.

I have solve it. In GUI remove the (using) at defined new ServiceHost. Why i don't know but it works!!!

ServiceHost host;
host = new ServiceHost(typeof(Service), new Uri("net.tcp://localhost:4111"));

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