配置和启动WCF服务时出现问题 [英] Problem with configuring and starting WCF service

查看:60
本文介绍了配置和启动WCF服务时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在配置和启动WCF服务时遇到问题.在我的应用程序中,有一种启动服务的方法.像这样

I have a problem with configuration and starting WCF service. In my application there is a method that starts service. Something like this

void Start(string protocol, string address, string port)
{
    host = new ServiceHost(_myService,
        new Uri(String.Format("{0}://{1}{2}/Sample", protocol, address, port)));
    //...Some configuration (bindings, behaviors, etc.)
    host.Open();
}

让我的计算机具有IP 192.168.0.1.当我将值"192.168.0.2"的地址"参数传递给我时,发生了错误

Let my computer has an IP 192.168.0.1. When I pass 'address' parameter with a value '192.168.0.2' an error occurred

"A TCP error (10049: The requested address is not valid in its context.)
 occurred while listening on IP Endpoint=192.168.0.2:1234"

是正确的,因为这不是我的IP.但是在那之后,如果我传递正确的值(我的真实IP),我将得到关于IP 192.168.0.2的相同错误!因此,如果不重新启动应用程序,我将无法重新配置和重新启动服务器.

That's right because it's not my IP. But after that if I pass correct value (my real IP) I get the same error about IP 192.168.0.2! So I can't reconfigure and restart server without restarting application.

为什么会发生?如何避免这种行为?

Why does it happen? How can I avoid such behavior?

推荐答案

我从您的问题中看不到您如何添加正确的端点,但是我怀疑您正在尝试修改端点地址.使用WCF服务,您不能在调用

I can't see from your question how you are adding the correct endpoint, but I suspect you are attempting to modify the endpoint address. With WCF services, you cannot make changes to the endpoint address after calling

host.Open();

因为此时服务已打开(如果没有错误),并且正在接受来自指定地址和端口号的客户端的请求.

because at this point the service is (if you have no errors) up and accepting requests from clients at the specified address and port number.

如果要在新地址上托管服务,则需要使用正确的终结点地址创建一个新的ServiceHost对象(并处置旧的终结点).

You need to create a new ServiceHost object with the correct endpoint address (and dispose of the old one) if you wish to host the service at a new address.

在研究了您发布的示例解决方案之后,我找到了解决该问题的方法.我认为出了点问题,因为两次尝试都使用相同的端口号(在我下载的示例解决方案中,您未指定此端口号,因此端口默认为808).如果您按照以下方式更改代码以在第二次尝试的基址中指定其他端口号,则您遇到的错误将消失:

After playing around with the example solution you have posted, I have found a solution to the issue. I think something is going wrong because you are using the same port number for both tries (in the example solution I downloaded you don't specify this, so the port defaulted to 808). The error you are experiencing vanishes if you change your code as follows to specify a different port number in the base address for the 2nd try:

 try
        {
            var host2 = CreateServiceHost("localhost:5432", serviceImpl);

            Console.WriteLine("#2, config: " + host2.BaseAddresses.First().ToString());

            host2.Open();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

WCF本身下面似乎有一些错误,可能是在套接字级别,在第一次IP错误错误后,该端口仍然不可用.

There seems to be something buggy underneath WCF itself, probably on a socket level, where the port is somehow still unavailable after the first error with the incorrect IP.

我做了一个快速的google和

I did a quick google and found this article where someone experienced a delay in reusing a port after closing a socket. If you always need to use the same port number, perhaps you could wait a certain amount of time for the port to become free again before trying to create the service host again.

这篇关于配置和启动WCF服务时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆