Example #1
0
        /// <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 <T>(string serverAddress, out ChannelFactory <T> channelFactory)
        {
            _readerWriterLock.AcquireReaderLock(1000);
            try
            {
                ClientEndpoint clientEndpoint;

                if (ClientEndpoints.TryGetValue(typeof(T).Name + serverAddress, out clientEndpoint))
                {
                    EndpointChannelFactory endpointChannelFactory = clientEndpoint.Endpoint;

                    channelFactory = endpointChannelFactory.ChannelFactory as ChannelFactory <T>;

                    return(true);
                }
                else
                {
                    channelFactory = null;
                    return(false);
                }
            }
            finally
            {
                _readerWriterLock.ReleaseReaderLock();
            }
        }
Example #2
0
        /// <summary>
        /// returns the ClientEndPoint for a specific contract and a specified name
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private static ClientEndpoint GetClientEndpoint <T>(string serverAddress, List <IEndpointBehavior> endpointBehaviors)
        {
            EndpointAddress baseAddress = new EndpointAddress(serverAddress);

            var clientEndpoint = new ClientEndpoint(typeof(T), serverAddress);

            var endpointChannelFactory = new EndpointChannelFactory
            {
                EndpointAddress = baseAddress
            };

            endpointChannelFactory.ChannelFactory = new ChannelFactory <T>(ClientServerBindingHelper.GetBinding(false).ApplyClientBasicHttpBinding(),
                                                                           endpointChannelFactory.EndpointAddress);
            foreach (var endpointBehavior in endpointBehaviors)
            {
                endpointChannelFactory.ChannelFactory.Endpoint.Behaviors.Add(endpointBehavior);
            }

            endpointChannelFactory.ChannelFactory.Open();

            clientEndpoint.Endpoint = endpointChannelFactory;

            return(clientEndpoint);
        }