即使服务端点地址有效,WCF 也会发送相同的异常

发布于 2024-08-18 08:00:38 字数 1618 浏览 6 评论 0原文

我在使用 WCF 时遇到了一个非常奇怪的问题。如果收到无法到达的端点 IP 地址或服务无法绑定,我需要为 WCF 服务实现一些恢复行为。 如果应用程序因服务创建异常而失败,则流程很简单,它会终止应用程序并向用户请求另一个 IP 地址并再次尝试创建服务。 (下面的代码片段)。如果地址无效,我会收到“监听 IP Endpoint=.121.10.11.11 时发生 TCP 错误(10049:请求的地址在其上下文中无效)”异常,但出于任何原因,如果我尝试第二次尝试有效地址 我在之前的尝试中遇到了同样的异常,IP 地址错误。这是代码:

ServiceHost service = null;
try
{
    Uri[] uris = { new Uri(Constants.PROTOCOL + "://" + address + ":" + port) };
    service = new ServiceHost(typeof(IRemoteService), uris);
    NetTcpBinding tcpBinding =
        WcfTcpRemoteServicesManager.LessLimitedNewNetTcpBinding(int.MaxValue,
            int.MaxValue, int.MaxValue);
    ServiceEndpoint ep = service.AddServiceEndpoint(implementedContract.FullName,
        tcpBinding, serviceName);
    var throttle =
        service.Description.Behaviors.Find<ServiceThrottlingBehavior>();
    if (throttle == null)
    {
        throttle = new ServiceThrottlingBehavior
        {
            MaxConcurrentCalls = Constants.MAX_CONCURRENT_CALLS,
            MaxConcurrentSessions = Constants.MAX_CONCURRENT_SESSIONS,
            MaxConcurrentInstances = Constants.MAX_CONCURRENT_INSTANCES
        };
        service.Description.Behaviors.Add(throttle);
    }
    service.Open();

}
catch (Exception e)
{
    _debugLog.WriteLineMessage(
        "Failed to open or create service exception. Exception message:" +
            e.Message);
    if (service!=null)
    {
        try
        {
            service.Close();
        }
        catch (Exception)
        {
            service.Abort();
            service.Close();
            throw e;
        }
    }
}

谢谢

I'm running into a really strange problem with WCF. I need to implement some recovery behavior for WCF service if not reachable endpoint IP address received or service can not bind.
The flow is simple if the application fail on exception on service creation it terminate it and request from user another IP address and perform another attempt to create the service. (The code snippet below). If the address is not valid I get "A TCP error (10049: The requested address is not valid in its context) occurred while listening on IP Endpoint=.121.10.11.11" exception, but for any reason if I try the second attempt with valid address I've got the same exception with wrong IP address from previous attempt. Here is a code:

ServiceHost service = null;
try
{
    Uri[] uris = { new Uri(Constants.PROTOCOL + "://" + address + ":" + port) };
    service = new ServiceHost(typeof(IRemoteService), uris);
    NetTcpBinding tcpBinding =
        WcfTcpRemoteServicesManager.LessLimitedNewNetTcpBinding(int.MaxValue,
            int.MaxValue, int.MaxValue);
    ServiceEndpoint ep = service.AddServiceEndpoint(implementedContract.FullName,
        tcpBinding, serviceName);
    var throttle =
        service.Description.Behaviors.Find<ServiceThrottlingBehavior>();
    if (throttle == null)
    {
        throttle = new ServiceThrottlingBehavior
        {
            MaxConcurrentCalls = Constants.MAX_CONCURRENT_CALLS,
            MaxConcurrentSessions = Constants.MAX_CONCURRENT_SESSIONS,
            MaxConcurrentInstances = Constants.MAX_CONCURRENT_INSTANCES
        };
        service.Description.Behaviors.Add(throttle);
    }
    service.Open();

}
catch (Exception e)
{
    _debugLog.WriteLineMessage(
        "Failed to open or create service exception. Exception message:" +
            e.Message);
    if (service!=null)
    {
        try
        {
            service.Close();
        }
        catch (Exception)
        {
            service.Abort();
            service.Close();
            throw e;
        }
    }
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2024-08-25 08:00:38

您捕获异常 e,然后捕获另一个异常,但抛出原始异常 e。

不确定这是否是您的问题,但这可能会导致您收到令人困惑的异常。

编辑

现在我已经更仔细地阅读了它。

问题是上下文和地址之间不匹配。我看到代码使用了netTcpBinding:

  • 检查地址是否匹配,CONSTANTS.Protocol 的值是多少?
  • 检查配置文件是否有冲突。

You catch exception e, then later you catch a different exception, but you throw the original exception e.

Not sure if this is your problem, but it could result in you getting a confusing exception back.

EDIT

Now I have read it a bit more carefully.

The problem is a mismatch between the context and the address. I see that the code uses netTcpBinding:

  • Check that the address matches, what is the value of CONSTANTS.Protocol?
  • Check that there is no conflict in the configuration files.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文