public class PasswordDigestChannelFactory<TPortTypeClient, TPlugin> where TPortTypeClient : ClientBase<TPlugin>, TPlugin, new() where TPlugin : class { public PasswordDigestChannelFactory(string endpointConfigurationName) { _endpointConfigurationName = endpointConfigurationName; } private readonly string _endpointConfigurationName; public TPlugin GetClient() { var args = new[] {_endpointConfigurationName}; var portInstance = Activator.CreateInstance(typeof (TPortTypeClient), args) as ClientBase<TPlugin>; // replace ClientCredentials with UsernameClientCredentials var username = "username"; var password = "password"; var credentials = new UsernameClientCredentials(new UsernameInfo(username, password)); portInstance.ChannelFactory.Endpoint.Behaviors.Remove(typeof (ClientCredentials)); portInstance.ChannelFactory.Endpoint.Behaviors.Add(credentials); return portInstance as TPlugin; } } // StructureMap injection For<PortType>() .Use((new PasswordDigestChannelFactory<PortTypeClient, PortType>("endPoint")).GetClient());
时间: 2024-10-20 12:56:44