private ChannelFactory CreateChannelFactory(Type serviceContract, Uri baseUri)
        {
            var uri = GetFullUri(serviceContract, baseUri);
            var channelFactoryClass = typeof(ChannelFactory <>).MakeGenericType(new [] { serviceContract });

            return(_args.Configuration.ConfigureChannelFactory(
                       new ServiceChannelConfigurationArgs(channelFactoryClass,
                                                           uri,
                                                           AuthenticationAttribute.IsAuthenticationRequired(serviceContract),
                                                           _args.MaxReceivedMessageSize,
                                                           _args.CertificateValidationMode,
                                                           _args.RevocationMode)));
        }
        public object GetService(Type serviceContract)
        {
            // check if the service is provided by this provider
            if (CanProvideService(serviceContract))
            {
                return(null);
            }

            // create the channel
            var authenticationRequired = AuthenticationAttribute.IsAuthenticationRequired(serviceContract);
            var credentials            = authenticationRequired
                                                ? new ChannelCredentials {
                UserName = this.UserName, Password = this.Password
            }
                                                : null;
            var channel = _channelProvider.GetPrimary(serviceContract, credentials);

            // create an AOP proxy around the channel, and return that
            return(CreateChannelProxy(serviceContract, channel));
        }
        /// <summary>
        /// Creates a channel for the specified contract, using the specified factory.
        /// Note that this method modifies the factory, therefore it cannot be re-used!
        /// </summary>
        /// <param name="serviceContract"></param>
        /// <param name="factory"></param>
        /// <param name="credentials"></param>
        /// <returns></returns>
        private static IClientChannel CreateChannel(Type serviceContract, ChannelFactory factory, ChannelCredentials credentials)
        {
            var authenticationRequired = AuthenticationAttribute.IsAuthenticationRequired(serviceContract);

            if (authenticationRequired)
            {
                factory.Credentials.UserName.UserName = credentials.UserName;
                factory.Credentials.UserName.Password = credentials.Password;
            }

            // invoke the CreateChannel method on the factory
            var createChannelMethod = factory.GetType().GetMethod("CreateChannel", Type.EmptyTypes);
            var channel             = (IClientChannel)createChannelMethod.Invoke(factory, null);

            Platform.Log(LogLevel.Debug, "Created service channel instance for service {0}, authenticationRequired={1}, endpoint={2}",
                         serviceContract.Name, authenticationRequired, factory.Endpoint.Address.Uri);

            // add some identifying information to the channel, in case we are asked to obtain a failover channel
            channel.Extensions.Add(new ChannelInfo(serviceContract, credentials));

            return(channel);
        }