views:

287

answers:

2

Hi,

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

A: 

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.
Shiraz Bhaiji
This is an expected behavior, I want to update the upper level that here was exception thrown, but close the ServiceHost gracefully.
ALexr111
But you only throw it if the closing of the service host generates an exception. If the closing of the service host works the upper level will not get the exception
Shiraz Bhaiji
You are right the same line should be added to the upper exception as well, but unfortunately the initial problem still unresolved.
ALexr111
Hello Shiraz:The CONSTANTS.Protocol value is:public const string PROTOCOL = "net.tcp";There is no conflicts in configuration files.How does it possible that context save previous address if exception thrown? If I try to change one valid address to another valid the operation succeed, it fails only after exception.Is it possible to clear context on server side ?
ALexr111
A: 

Were you able to solve this problem? I am having the exact same issue.