1

I've a 2 WCF services, hosted in a winform app: 1 exposing a basic http binding, 1 exposing a IClientAccessService (webhttpbinding) and an interface with a PollingDuplexHttpBinding and NTLM security for a silverlight 5 client. In some circomstances, the pollingduplex endpoint is not opening correctly, resulting in not connecting clients. To be able to detect this, a silverlight out of browser app is polling the interface, and when a response is received it notifies the service via a tcp socket.

When i don't receive a notification of the out-of-browser app, I'd like to restart the wcf services. by calling servicehost.close() on both services, and after that servicehost.open().

the weird thing is the open / close of the service with the basichttp binding succeeds, but opening the service with the polling duplex binding fails with a Channeldispatcher exception: A registration already exists for URI http://localhost:18524/IService/ this because the port is still claimed by the closed and aborted servicehost.

i've been playing with the open / close timeout of the pollingduplex channel, delaying the time between close / open, but no effect.

Anyone an idea how to release the port of the pollingduplex connection?

Thanks for help.

the open close code: public void StartServices() { StopServices();

        try
        {
            var configSyncService = new ConfigurationSyncService();
            configSyncService.Open();

            var mainService = new Service();
            mainService.Open();

            serviceHosts.Add(configSyncService);
            serviceHosts.Add(mainService);
        }
        catch (Exception ex)
        {
            string msg = String.Format("Exception starting WCF service for Centrale:  {0}", ex.Message);

            LOG.Fatal(
                LogProperties.Bedienpost,
                ex,
                Resources.ERROR_STARTING_WCF,
                ex.Message);

            throw ex;
        }
     }

    public void StopServices()
    {
        try
        {
            var result = Parallel.ForEach(serviceHosts, service =>
            {
                if ((service != null) && (service.State == CommunicationState.Opened))
                {
                    try
                    {
                        service.Close();
                    }
                    finally
                    {
                        service.Dispose();
                        service = null;
                    }
                }
                else if ((service != null) && (service.State != CommunicationState.Faulted))
                {
                    service.Dispose();
                    service = null;
                }
            });

            if (result.IsCompleted)
            {
                serviceHosts.Clear();
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

The configuration of the polling duplex channel:

private Binding CreateCustomPollingDuplexBinding()
    {
        CustomBinding binding = new CustomBinding();
        binding.Name = "pollingDuplexHttpBinding";

        binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
        binding.SendTimeout = new TimeSpan(0, 5, 0);
        binding.OpenTimeout = new TimeSpan(0, 5, 0);
        binding.CloseTimeout = new TimeSpan(0, 5, 0);

        binding.Elements.Add(new PollingDuplexBindingElement(PollingDuplexMode.SingleMessagePerPoll));

        TextMessageEncodingBindingElement textEncoding = new TextMessageEncodingBindingElement();
        textEncoding.ReaderQuotas.MaxArrayLength = int.MaxValue;
        textEncoding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
        textEncoding.ReaderQuotas.MaxDepth = int.MaxValue;
        textEncoding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
        textEncoding.ReaderQuotas.MaxStringContentLength = int.MaxValue;

        binding.Elements.Add(textEncoding);

        HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
        httpTransport.MaxBufferPoolSize = int.MaxValue;
        httpTransport.MaxBufferSize = int.MaxValue;
        httpTransport.MaxReceivedMessageSize = int.MaxValue;
        httpTransport.AuthenticationScheme = AuthenticationSchemes.Ntlm;

        binding.Elements.Add(httpTransport);

        return binding;
    }
impeeNL
  • 21
  • 4

0 Answers0