예제 #1
0
 private void SetDefaultImplementations()
 {
     RequestProcessorImplementation      = typeof(RequestProcessor);
     AsyncRequestProcessorImplementation = typeof(AsyncRequestProcessor);
     CacheManagerImplementation          = typeof(CacheManager);
     CacheProviderImplementation         = typeof(InMemoryCacheProvider);
     RequestTypeRegistry    = new WcfKnownTypesBasedRequestTypeRegistry();
     RequestHandlerRegistry = new RequestHandlerRegistry();
     RegisterRequestHandlerInterceptor <CachingInterceptor>();
 }
예제 #2
0
        private void RegisterRequestHandlers()
        {
            var oneWayHandlerType             = typeof(IOneWayRequestHandler);
            var openOneWayHandlerType         = typeof(IOneWayRequestHandler <>);
            var requestResponseHandlerType    = typeof(IRequestHandler);
            var openRequestReponseHandlerType = typeof(IRequestHandler <>);

            var requestWithRequestHandlers = new Dictionary <Type, Type>();

            foreach (var assembly in requestHandlerAssemblies)
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (type.IsAbstract)
                    {
                        continue;
                    }

                    if (!oneWayHandlerType.IsAssignableFrom(type) && !requestResponseHandlerType.IsAssignableFrom(type))
                    {
                        continue;
                    }

                    RequestHandlerRegistry.Register(type);

                    var requestType = GetRequestType(type);

                    if (requestType != null)
                    {
                        Type handlerType = null;
                        if (oneWayHandlerType.IsAssignableFrom(type))
                        {
                            handlerType = openOneWayHandlerType.MakeGenericType(requestType);
                        }
                        else if (requestResponseHandlerType.IsAssignableFrom(type))
                        {
                            handlerType = openRequestReponseHandlerType.MakeGenericType(requestType);
                        }

                        if (handlerType != null)
                        {
                            if (requestWithRequestHandlers.ContainsKey(requestType))
                            {
                                throw new InvalidOperationException(String.Format("Found two request handlers that handle the same request: {0}. "
                                                                                  + " First request handler: {1}, second: {2}. "
                                                                                  + " For each request type there must by only one request handler.", requestType.FullName, type.FullName, requestWithRequestHandlers[requestType].FullName));
                            }

                            IoC.Container.Register(handlerType, type, Lifestyle.Transient);
                            requestWithRequestHandlers.Add(requestType, type);
                        }
                    }
                }
            }
        }