/// <summary> /// Checks to see whether a channel factory exists in the cache for the current thread /// </summary> /// <param name="ofType">The service interface type</param> /// <param name="channelFactoryFactory"> /// The <see cref="IChannelFactoryFactory"/> to use to create the appropriate /// <see cref="ChannelFactory{TChannel}"/>. /// </param> /// <returns>True if there is a channel factory in the cache, false otherwise</returns> private bool CheckCacheItemExists(Type ofType, IChannelFactoryFactory channelFactoryFactory) { using (_ReaderWriterLock.AcquireDisposableRead()) { return(_Cache.ContainsKey(Thread.CurrentThread) && _Cache[Thread.CurrentThread].ContainsKey(ofType) && _Cache[Thread.CurrentThread][ofType].ContainsKey(channelFactoryFactory)); } }
/// <summary> /// Create a new channel factory and puts it into the cache /// </summary> /// <param name="ofType">The service interface type</param> /// <param name="channelFactoryFactory"> /// The <see cref="IChannelFactoryFactory"/> to use to create the appropriate /// <see cref="ChannelFactory{TChannel}"/>. /// </param> private void CreateChannelFactory(Type ofType, IChannelFactoryFactory channelFactoryFactory) { using (_ReaderWriterLock.AcquireDisposableWrite()) { if (_Cache.ContainsKey(Thread.CurrentThread) == false) { _Cache.Add(Thread.CurrentThread, new Dictionary <Type, IDictionary <IChannelFactoryFactory, CacheItem> >()); } if (_Cache[Thread.CurrentThread].ContainsKey(ofType) == false) { _Cache[Thread.CurrentThread].Add(ofType, new Dictionary <IChannelFactoryFactory, CacheItem>()); } ChannelFactory channelFactory = channelFactoryFactory.CreateChannelFactory(ofType); CacheItem cacheItem = new CacheItem(channelFactory, CompileCreateChannelFunc(channelFactory)); _Cache[Thread.CurrentThread][ofType][channelFactoryFactory] = cacheItem; } }
/// <summary> /// Creates a WCF channel object for the specified service interface /// </summary> /// <param name="ofType"> /// The service interface type (used as the type parameter for <see cref="ChannelFactory{TChannel}"/> /// </param> /// <param name="channelFactoryFactory"> /// The <see cref="IChannelFactoryFactory"/> to use to create the appropriate /// <see cref="ChannelFactory{TChannel}"/>. /// </param> /// <returns>The channel</returns> public object CreateChannel(Type ofType, IChannelFactoryFactory channelFactoryFactory) { CleanCacheOfDeadThreads(); if (CheckCacheItemExists(ofType, channelFactoryFactory) == false) { CreateChannelFactory(ofType, channelFactoryFactory); } object channel; try { using (_ReaderWriterLock.AcquireDisposableRead()) { channel = _Cache[Thread.CurrentThread][ofType][channelFactoryFactory].CreateChannelFunc(); } } catch (Exception e) { //If we're not an expected channel error, throw if (e is CommunicationException == false && e is ObjectDisposedException == false && e is TimeoutException == false) { throw; } //Expected channel error. Toss the original factory and create a new one //If the new one fails too, any exception will be thrown as normal CreateChannelFactory(ofType, channelFactoryFactory); using (_ReaderWriterLock.AcquireDisposableRead()) { channel = _Cache[Thread.CurrentThread][ofType][channelFactoryFactory].CreateChannelFunc(); } } return(channel); }
/// <summary> /// Creates a WcfProxyBuildPlanPolicy that will create a channel with the specified endpoint /// </summary> /// <param name="channelFactoryFactory"> /// The <see cref="IChannelFactoryFactory"/> to use to create the appropriate /// <see cref="ChannelFactory{TChannel}"/>. /// </param> /// <param name="serviceInterfaceType"></param> public WcfProxyBuildPlanPolicy(IChannelFactoryFactory channelFactoryFactory, Type serviceInterfaceType) { _ChannelFactoryFactory = channelFactoryFactory; _ServiceInterfaceType = serviceInterfaceType; }
/// <summary> /// Registers a service interface type with the container so when /// <see cref="IUnityContainer.Resolve{T}()"/> is called with this type, a channel proxy /// class will be returned. /// </summary> /// <typeparam name="TServiceInterfaceType">The service interface type</typeparam> /// <typeparam name="TRegisterAsType"> /// The type to register the service interface as. The type must be assignable from /// <typeparamref name="TServiceInterfaceType"/>. This will be the type that you request from Unity /// and therefore is normally either the same as <typeparamref name="TServiceInterfaceType"/> /// or a super type. /// </typeparam> /// <param name="channelFactoryFactory"> /// The <see cref="IChannelFactoryFactory"/> to use to create the appropriate /// <see cref="ChannelFactory{TChannel}"/> with which to create WCF channels. /// </param> /// <returns>This object</returns> public IWcfProxyExtensionConfiguration RegisterType <TServiceInterfaceType, TRegisterAsType>(IChannelFactoryFactory channelFactoryFactory) { return(RegisterType(typeof(TServiceInterfaceType), typeof(TRegisterAsType), channelFactoryFactory)); }
/// <summary> /// Registers a service interface type with the container so when /// <see cref="IUnityContainer.Resolve{T}()"/> is called with this type, a channel proxy class will be /// returned. /// </summary> /// <param name="serviceInterfaceType">The service interface type</param> /// <param name="registerAsType"> /// The type to register the service interface as. The type must be assignable from /// <paramref name="serviceInterfaceType"/>. This will be the type that you request from Unity /// and therefore is normally either the same as <paramref name="serviceInterfaceType"/> /// or a super type. /// </param> /// <param name="name">The name of the mapping</param> /// <param name="channelFactoryFactory"> /// The <see cref="IChannelFactoryFactory"/> to use to create the appropriate /// <see cref="ChannelFactory{TChannel}"/> with which to create WCF channels. /// </param> /// <returns>This object</returns> public IWcfProxyExtensionConfiguration RegisterType(Type serviceInterfaceType, Type registerAsType, string name, IChannelFactoryFactory channelFactoryFactory) { WcfProxyBuildPlanPolicy policy = new WcfProxyBuildPlanPolicy(channelFactoryFactory, serviceInterfaceType); Context.Policies.Set <IBuildPlanPolicy>(policy, new NamedTypeBuildKey(registerAsType, name)); return(this); }
/// <summary> /// Registers a service interface type with the container so when /// <see cref="IUnityContainer.Resolve{T}()"/> is called with this type, a channel proxy /// class will be returned. /// </summary> /// <param name="serviceInterfaceType">The service interface type</param> /// <param name="name">The name of the mapping</param> /// <param name="channelFactoryFactory"> /// The <see cref="IChannelFactoryFactory"/> to use to create the appropriate /// <see cref="ChannelFactory{TChannel}"/> with which to create WCF channels. /// </param> /// <returns>This object</returns> public IWcfProxyExtensionConfiguration RegisterType(Type serviceInterfaceType, string name, IChannelFactoryFactory channelFactoryFactory) { return(RegisterType(serviceInterfaceType, serviceInterfaceType, name, channelFactoryFactory)); }
/// <summary> /// Registers a service interface type with the container so when /// <see cref="IUnityContainer.Resolve{T}()"/> is called with this type, a channel proxy /// class will be returned. /// </summary> /// <typeparam name="TServiceInterfaceType">The service interface type</typeparam> /// <param name="name">The name of the mapping</param> /// <param name="channelFactoryFactory"> /// The <see cref="IChannelFactoryFactory"/> to use to create the appropriate /// <see cref="ChannelFactory{TChannel}"/> with which to create WCF channels. /// </param> /// <returns>This object</returns> public IWcfProxyExtensionConfiguration RegisterType <TServiceInterfaceType>(string name, IChannelFactoryFactory channelFactoryFactory) { return(RegisterType(typeof(TServiceInterfaceType), name, channelFactoryFactory)); }
/// <summary> /// Registers a service interface type with the container so when /// <see cref="IUnityContainer.Resolve{T}()"/> is called with this type, a channel proxy /// class will be returned. /// </summary> /// <param name="serviceInterfaceType">The service interface type</param> /// <param name="registerAsType"> /// The type to register the service interface as. The type must be assignable from /// <paramref name="serviceInterfaceType"/>. This will be the type that you request from Unity /// and therefore is normally either the same as <paramref name="serviceInterfaceType"/> /// or a super type. /// </param> /// <param name="channelFactoryFactory"> /// The <see cref="IChannelFactoryFactory"/> to use to create the appropriate /// <see cref="ChannelFactory{TChannel}"/> with which to create WCF channels. /// </param> /// <returns>This object</returns> public IWcfProxyExtensionConfiguration RegisterType(Type serviceInterfaceType, Type registerAsType, IChannelFactoryFactory channelFactoryFactory) { return(RegisterType(serviceInterfaceType, registerAsType, null, channelFactoryFactory)); }