Exemplo n.º 1
0
        /// <summary>
        /// This factory class creates three different kinds of client. This method gathers the code of client creation
        /// and adds the retry logic. The retry can avoid transient connection error especially when connect to Azure.
        /// </summary>
        /// <param name="clientType">the type of the client</param>
        /// <returns>the client</returns>
        private ICommunicationObject CreateClientWithRetry(ClientType clientType)
        {
            ICommunicationObject client    = null;
            Exception            exception = null;

            try
            {
                exception = null;
                switch (clientType)
                {
                case ClientType.SendRequest:
                    if (this.brokerClientFactory == null)
                    {
                        ClientCredentials clientCredentials = new ClientCredentials();
                        clientCredentials.UserName.UserName = this.info.Username;
                        clientCredentials.UserName.Password = this.info.InternalPassword;
                        BindingParameterCollection bindingParams = new BindingParameterCollection();
                        bindingParams.Add(clientCredentials);
                        this.brokerClientFactory = this.binding.BuildChannelFactory <IRequestChannel>(bindingParams);
                        this.brokerClientFactory.Open();
                    }

                    client = this.brokerClientFactory.CreateChannel(GenerateEndpointAddress(this.info.BrokerEpr, this.scheme, this.info.Secure, this.info.IsAadOrLocalUser));
                    break;

                case ClientType.Controller:
                    BrokerControllerClient controllerClient = new BrokerControllerClient(
                        this.binding,
                        GenerateEndpointAddress(this.info.ControllerEpr, this.scheme, this.info.Secure, this.info.IsAadOrLocalUser));
                    controllerClient.ClientCredentials.UserName.UserName = this.info.Username;
                    controllerClient.ClientCredentials.UserName.Password = this.info.InternalPassword;
                    client = controllerClient;
                    break;

                default:
                    throw new NotSupportedException();
                }

                client.Open();
            }
            catch (Exception e)
            {
                if (this.brokerClientFactory != null)
                {
                    try
                    {
                        this.brokerClientFactory.Abort();
                        this.brokerClientFactory = null;
                    }
                    catch
                    {
                        // abandon the factory, swallow the exception
                    }
                }

                if (client != null)
                {
                    try
                    {
                        client.Abort();
                        client = null;
                    }
                    catch
                    {
                        // abandon the channel, swallow the exception
                    }
                }

                exception = e;
            }

            if (exception != null)
            {
                throw exception;
            }

            return(client);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This factory class creates three different kinds of client. This method gathers the code of client creation
        /// and adds the retry logic. The retry can avoid transient connection error especially when connect to Azure.
        /// </summary>
        /// <param name="clientType">the type of the client</param>
        /// <returns>the client</returns>
        private ICommunicationObject CreateClientWithRetry(ClientType clientType)
        {
            ICommunicationObject client    = null;
            Exception            exception = null;

            try
            {
                exception = null;
                switch (clientType)
                {
                case ClientType.SendRequest:

                    if (this.brokerClientFactory == null)
                    {
                        BindingParameterCollection bindingParms      = new BindingParameterCollection();
                        ClientCredentials          clientCredentials = new ClientCredentials();
                        if (this.info.UseAad)
                        {
                            // Authentication will be taken care of by adding message header to request
                        }
                        else if (this.SetClientCredential(clientCredentials))
                        {
                            bindingParms.Add(clientCredentials);
                        }

                        this.brokerClientFactory = this.binding.BuildChannelFactory <IDuplexSessionChannel>(bindingParms);
                        this.brokerClientFactory.Open();
                    }

                    client = this.brokerClientFactory.CreateChannel(GenerateEndpointAddress(this.info.BrokerEpr, this.scheme, this.info.Secure, this.info.IsAadOrLocalUser));
                    break;

                case ClientType.GetResponse:

                    BrokerResponseServiceClient responseClient = new BrokerResponseServiceClient(
                        this.binding,
                        GenerateEndpointAddress(this.info.ResponseEpr, this.scheme, this.info.Secure, this.info.IsAadOrLocalUser),
                        new InstanceContext(this.ResponseCallback));
                    this.SetClientCredential(responseClient);


                    client = responseClient;
                    break;

                case ClientType.Controller:

                    BrokerControllerClient controllerClient = new BrokerControllerClient(
                        this.binding,
                        GenerateEndpointAddress(this.info.ControllerEpr, this.scheme, this.info.Secure, this.info.IsAadOrLocalUser));
                    this.SetClientCredential(controllerClient);

                    client = controllerClient;
                    break;

                default:
                    throw new NotSupportedException();
                }

                client.Open();
            }
            catch (Exception e)
            {
                if (this.brokerClientFactory != null)
                {
                    try
                    {
                        this.brokerClientFactory.Abort();
                        this.brokerClientFactory = null;
                    }
                    catch
                    {
                        // abandon the factory, swallow the exception
                    }
                }

                if (client != null)
                {
                    try
                    {
                        client.Abort();
                        client = null;
                    }
                    catch
                    {
                        // abandon the channel, swallow the exception
                    }
                }

                exception = e;
            }


            if (exception != null)
            {
                throw exception;
            }

            return(client);
        }