即使服务端点地址有效,WCF也会发送相同的异常 [英] WCF sending the same exception even if the service endpoint address is valid

查看:148
本文介绍了即使服务端点地址有效,WCF也会发送相同的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

谢谢

推荐答案

您捕获了异常e,然后又捕获了另一个异常,但是您抛出了原始异常e.

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.

编辑

现在我已经仔细阅读了.

Now I have read it a bit more carefully.

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

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

  • 检查地址是否匹配,CONSTANTS.Protocol的值是什么?
  • 检查配置文件中是否没有冲突.

这篇关于即使服务端点地址有效,WCF也会发送相同的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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