/// <summary> /// Tries to populate the channelFactory out parameter if a channelFactory can be found /// for the contract type T identified by a enpointName. /// If there is not channelfactory for the type the parameter will be set to null /// and false will be returned /// The method uses a readlock, so many threads can call it at the same time /// </summary> /// <typeparam name="T"></typeparam> /// <param name="channelFactory"></param> /// <returns></returns> private bool TryGetChannelFactory(string serverAddress, out DuplexChannelFactory <T> channelFactory) { _readerWriterLock.AcquireReaderLock(1000); try { DuplexClientEndpoint <T> clientEndpoint; if (ClientEndpoints.TryGetValue(typeof(T).Name + serverAddress, out clientEndpoint)) { DuplexEndpointChannelFactory <T> endpointChannelFactory = clientEndpoint.Endpoint; channelFactory = endpointChannelFactory.ChannelFactory as DuplexChannelFactory <T>; return(true); } else { channelFactory = null; return(false); } } finally { _readerWriterLock.ReleaseReaderLock(); } }
/// <summary> /// returns the ClientEndPoint for a specific contract and a specified name /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> private static DuplexClientEndpoint <T> GetClientEndpoint <T>(string serverAddress, List <IEndpointBehavior> endpointBehaviors, InstanceContext callbackInstance) { EndpointAddress baseAddress = new EndpointAddress(serverAddress); var clientEndpoint = new DuplexClientEndpoint <T>(typeof(T), serverAddress); var endpointChannelFactory = new DuplexEndpointChannelFactory <T> { EndpointAddress = baseAddress }; endpointChannelFactory.ChannelFactory = new DuplexChannelFactory <T>(callbackInstance, ClientServerBindingHelper.GetBinding(true).ApplyClientWsDualHttpBinding(), endpointChannelFactory.EndpointAddress); foreach (var endpointBehavior in endpointBehaviors) { endpointChannelFactory.ChannelFactory.Endpoint.Behaviors.Add(endpointBehavior); } endpointChannelFactory.ChannelFactory.Open(); clientEndpoint.Endpoint = endpointChannelFactory; return(clientEndpoint); }