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

谢谢

4

1 回答 1

0

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.
于 2010-01-22T11:06:40.983 回答