WCF sending the same exception even if the service endpoint address is valid

Posted by ALexr111 on Stack Overflow See other posts from Stack Overflow or by ALexr111
Published on 2010-01-21T20:07:56Z Indexed on 2010/04/07 11:03 UTC
Read the original article Hit count: 619

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about wcf

Related posts about service