繁体   English   中英

WCF服务未托管在计算机上

[英]WCF Service is not hosted on the machine

我已经实现了一个简单的聊天控制台应用程序,并且运行良好。 当我尝试在GUI应用程序上应用相同的概念时。 托管时在服务端出现任何错误,但是如果我使用CMD命令netstat -ao显示所有端口,则该端口不存在。因此,当我运行客户端应用程序时,会出现异常(由于目标计算机无法建立连接积极拒绝)。 我该如何解决这些问题?

服务器

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
    {
    }
}

客户

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;
}

假设您正在显示所有代码。

您需要在host.Open();之后添加一行,可以添加Console.ReadLine();。

这将使程序从现有版本停止运行。 现在发生的事情是主机打开,然后程序存在,并且主机被关闭/收集了垃圾。

我已经解决了。 在GUI中,删除(使用)已定义的新ServiceHost。 为什么我不知道但是它有效!!!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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