コード例 #1
0
        public static Func <string, State> GetStateMethod(object owner)
        {
            Type ownerType = owner.GetType();

            return(_getStateMethods.Retrieve(ownerType, () =>
            {
                Type stateMachineType = ownerType;
                while (stateMachineType != typeof(object))
                {
                    if (stateMachineType.IsGenericType && stateMachineType.GetGenericTypeDefinition() == typeof(StateMachine <>))
                    {
                        break;
                    }

                    stateMachineType = stateMachineType.BaseType;
                }

                if (stateMachineType == typeof(object))
                {
                    throw new StateMachineException("The owner type is not a state machine: " + ownerType.FullName);
                }

                MethodInfo mi = stateMachineType.GetMethod("GetState", BindingFlags.Static | BindingFlags.Public);
                if (mi == null)
                {
                    throw new StateMachineException("Could not find GetState method on " + ownerType.FullName);
                }

                var input = Expression.Parameter(typeof(string), "input");
                var method = Expression.Call(mi, input);

                Func <string, State> invoker = Expression.Lambda <Func <string, State> >(method, input).Compile();
                return invoker;
            }));
        }
コード例 #2
0
        public IEndpoint GetEndpoint(Uri uri)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("The endpoint resolver has been disposed");
            }

            Guard.AgainstNull(uri, "uri", "Uri cannot be null");

            try
            {
                var key = new Uri(uri.ToString().ToLowerInvariant());

                return(_endpoints.Retrieve(key, () => _endpointFactory.CreateEndpoint(uri)));
            }
            catch (TransportException)
            {
                throw;
            }
            catch (EndpointException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new ConfigurationException("An exception was thrown retrieving the endpoint:" + uri, ex);
            }
        }
コード例 #3
0
        private Func <BatchSubscriber, ISubscriberContext, UnsubscribeAction> GetInvoker <TComponent>()
        {
            Type componentType = typeof(TComponent);

            return(_components.Retrieve(componentType, () =>
            {
                Func <BatchSubscriber, ISubscriberContext, UnsubscribeAction> invoker = null;

                // since we don't have it, we're going to build it

                foreach (Type interfaceType in componentType.GetInterfaces())
                {
                    Type messageType;
                    Type keyType;
                    if (!GetMessageType(interfaceType, out messageType, out keyType))
                    {
                        continue;
                    }

                    var typeArguments = new[] { typeof(TComponent), messageType, keyType };
                    MethodInfo genericMethod = FindMethod(GetType(), "Connect", typeArguments, new[] { typeof(ISubscriberContext) });
                    if (genericMethod == null)
                    {
                        throw new PipelineException(string.Format("Unable to subscribe for type: {0} ({1})",
                                                                  typeof(TComponent).FullName, messageType.FullName));
                    }

                    var interceptorParameter = Expression.Parameter(typeof(BatchSubscriber), "interceptor");
                    var contextParameter = Expression.Parameter(typeof(ISubscriberContext), "context");

                    var call = Expression.Call(interceptorParameter, genericMethod, contextParameter);
                    var parameters = new[] { interceptorParameter, contextParameter };
                    var connector = Expression.Lambda <Func <BatchSubscriber, ISubscriberContext, UnsubscribeAction> >(call, parameters).Compile();

                    Func <BatchSubscriber, ISubscriberContext, UnsubscribeAction> method = (interceptor, context) =>
                    {
                        if (context.HasMessageTypeBeenDefined(messageType))
                        {
                            return () => false;
                        }

                        context.MessageTypeWasDefined(messageType);
                        context.MessageTypeWasDefined(_batchType.MakeGenericType(messageType, keyType));

                        return connector(interceptor, context);
                    };

                    if (invoker == null)
                    {
                        invoker = method;
                    }
                    else
                    {
                        invoker += method;
                    }
                }

                return invoker;
            }));
        }
コード例 #4
0
        public static Type GetProxyFor(Type typeToProxy)
        {
            return(_proxyTypes.Retrieve(typeToProxy, () =>
            {
                ModuleBuilder moduleBuilder = GetModuleBuilderForType(typeToProxy);

                return BuildTypeProxy(moduleBuilder, typeToProxy);
            }));
        }
コード例 #5
0
        private static XmlSerializer GetDeserializerFor(Type type)
        {
            return(_deserializers.Retrieve(type, () =>
            {
                XmlAttributeOverrides overrides = new XmlAttributeOverrides();
                overrides.Add(type, _attributes);

                return new XmlSerializer(type, overrides);
            }));
        }
コード例 #6
0
        /// <summary>
        ///     Builds the inbound transport for the service bus endpoint,
        /// </summary>
        /// <param name="settings"> using these settings </param>
        /// <returns> A non-null instance of the inbound transport. </returns>
        public virtual IInboundTransport BuildInbound([NotNull] ITransportSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            EnsureProtocolIsCorrect(settings.Address.Uri);

            var address = _addresses.Retrieve(settings.Address.Uri,
                                              () => AzureServiceBusEndpointAddressImpl.Parse(settings.Address.Uri));

            _logger.DebugFormat("building inbound transport for address '{0}'", address);

            var client = GetConnection(address);

            return(new InboundTransportImpl(address, client, new PurgeImpl(settings.PurgeExistingMessages, address),
                                            MessageNameFormatter, _receiverSettings));
        }
コード例 #7
0
 private ConnectionHandler <StompConnection> GetConnection(IEndpointAddress address)
 {
     return(_connectionCache
            .Retrieve(address.Uri,
                      () =>
     {
         var connection = _connectionFactory.Build(address.Uri);
         var connectionHandler = new ConnectionHandlerImpl <StompConnection>(connection);
         return connectionHandler;
     }));
 }
コード例 #8
0
        public static Type GetProxyFor(Type typeToProxy)
        {
            return(_proxyTypes.Retrieve(typeToProxy, () =>
            {
                Type proxyType = null;
                GetModuleBuilderForType(typeToProxy, moduleBuilder =>
                {
                    proxyType = BuildTypeProxy(moduleBuilder, typeToProxy);
                });

                return proxyType;
            }));
        }
コード例 #9
0
        /// <summary>
        ///   Gets the connection.
        /// </summary>
        /// <param name = "address">The address.</param>
        /// <returns></returns>
        private StompClient GetConnection(IEndpointAddress address)
        {
            EnsureProtocolIsCorrect(address.Uri);

            var serverAddress = new UriBuilder("ws", address.Uri.Host, address.Uri.Port).Uri;

            return(_connectionCache
                   .Retrieve(address.Uri,
                             () =>
            {
                var client = new StompClient();
                client.Connect(serverAddress);
                return client;
            }));
        }
コード例 #10
0
        ConnectionHandler <ConnectionImpl> GetConnection([NotNull] AzureServiceBusEndpointAddress address)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            _logger.DebugFormat("get connection( address:'{0}' )", address);

            return(_connCache.Retrieve(address.Uri, () =>
            {
                var connection = new ConnectionImpl(address);
                var connectionHandler = new ConnectionHandlerImpl <ConnectionImpl>(connection);

                return connectionHandler;
            }));
        }
コード例 #11
0
ファイル: Distributor.cs プロジェクト: bmavity/MassTransit
        public void Consume(WorkerAvailable <T> message)
        {
            WorkerDetails worker = _workers.Retrieve(message.ControlUri, () =>
            {
                return(new WorkerDetails
                {
                    ControlUri = message.ControlUri,
                    DataUri = message.DataUri,
                    InProgress = message.InProgress,
                    InProgressLimit = message.InProgressLimit,
                    Pending = message.Pending,
                    PendingLimit = message.PendingLimit,
                    LastUpdate = message.Updated,
                });
            });

            worker.UpdateInProgress(message.InProgress, message.InProgressLimit, message.Pending, message.PendingLimit, message.Updated);
        }
コード例 #12
0
        ConnectionHandler <RabbitMqConnection> GetConnection(IRabbitMqEndpointAddress address)
        {
            return(_connectionCache.Retrieve(address.Uri, () =>
            {
                ConnectionFactoryBuilder builder = _connectionFactoryBuilders.Retrieve(address.Uri, () =>
                {
                    var configurator = new ConnectionFactoryConfiguratorImpl(address);

                    return configurator.CreateBuilder();
                });

                ConnectionFactory connectionFactory = builder.Build();

                var connection = new RabbitMqConnection(connectionFactory);
                var connectionHandler = new ConnectionHandlerImpl <RabbitMqConnection>(connection);
                return connectionHandler;
            }));
        }
コード例 #13
0
        public IEndpoint GetEndpoint(Uri uri)
        {
            Check.Parameter(uri).IsNotNull();
            if (_disposed)
            {
                throw new ObjectDisposedException("The object has been disposed");
            }

            try
            {
                Uri key = new Uri(uri.ToString().ToLowerInvariant());

                return(_endpoints.Retrieve(key, () => BuildEndpoint(uri)));
            }
            catch (EndpointException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new ConfigurationException("An error occurred retrieving the endpoint for " + uri, ex);
            }
        }
コード例 #14
0
        private Func <TInterceptor, ISubscriberContext, UnsubscribeAction> GetInvoker <TComponent>()
        {
            Type componentType = typeof(TComponent);

            return(_components.Retrieve(componentType, () =>
            {
                Func <TInterceptor, ISubscriberContext, UnsubscribeAction> invoker = null;

                foreach (Type interfaceType in componentType.GetInterfaces())
                {
                    Type messageType = GetMessageType(interfaceType);
                    if (messageType == null)
                    {
                        continue;
                    }

                    MethodInfo genericMethod = FindMethod(GetType(), "Connect", new[] { typeof(TComponent), messageType }, new[] { typeof(ISubscriberContext) });
                    if (genericMethod == null)
                    {
                        throw new PipelineException(string.Format("Unable to subscribe for type: {0} ({1})",
                                                                  typeof(TComponent).FullName, messageType.FullName));
                    }

                    var interceptorParameter = Expression.Parameter(typeof(TInterceptor), "interceptor");
                    var contextParameter = Expression.Parameter(typeof(ISubscriberContext), "context");

                    var call = Expression.Call(interceptorParameter, genericMethod, contextParameter);

                    var connector = Expression.Lambda <Func <TInterceptor, ISubscriberContext, UnsubscribeAction> >(call, new[] { interceptorParameter, contextParameter }).Compile();

                    if (invoker == null)
                    {
                        invoker = (interceptor, context) =>
                        {
                            if (context.HasMessageTypeBeenDefined(messageType))
                            {
                                return () => true;
                            }

                            context.MessageTypeWasDefined(messageType);
                            return connector(interceptor, context);
                        };
                    }
                    else
                    {
                        invoker += (interceptor, context) =>
                        {
                            if (context.HasMessageTypeBeenDefined(messageType))
                            {
                                return () => true;
                            }

                            context.MessageTypeWasDefined(messageType);
                            return connector(interceptor, context);
                        };
                    }
                }

                return invoker;
            }));
        }
コード例 #15
0
        private Func <ConsumesForSubscriber, ISubscriberContext, object, UnsubscribeAction> GetInvokerForInstance <TComponent>()
        {
            Type componentType = typeof(TComponent);

            return(_instances.Retrieve(componentType, () =>
            {
                Func <ConsumesForSubscriber, ISubscriberContext, object, UnsubscribeAction> invoker = null;

                // since we don't have it, we're going to build it

                foreach (Type interfaceType in componentType.GetInterfaces())
                {
                    if (!interfaceType.IsGenericType)
                    {
                        continue;
                    }

                    Type genericType = interfaceType.GetGenericTypeDefinition();

                    if (genericType != _interfaceType)
                    {
                        continue;
                    }

                    Type[] types = interfaceType.GetGenericArguments();

                    Type messageType = types[0];

                    Type keyType = types[1];

                    MethodInfo genericMethod = FindMethod(GetType(), "Connect", new[] { messageType, keyType }, new[] { typeof(ISubscriberContext), typeof(TComponent) });
                    if (genericMethod == null)
                    {
                        throw new PipelineException(string.Format("Unable to subscribe for type: {0} ({1})",
                                                                  typeof(TComponent).FullName, messageType.FullName));
                    }

                    var interceptorParameter = Expression.Parameter(typeof(ConsumesForSubscriber), "interceptor");
                    var contextParameter = Expression.Parameter(typeof(ISubscriberContext), "context");
                    var instanceParameter = Expression.Parameter(typeof(object), "instance");

                    var instanceCast = Expression.Convert(instanceParameter, typeof(TComponent));

                    var call = Expression.Call(interceptorParameter, genericMethod, contextParameter, instanceCast);

                    var connector = Expression.Lambda <Func <ConsumesForSubscriber, ISubscriberContext, object, UnsubscribeAction> >(call, new[] { interceptorParameter, contextParameter, instanceParameter }).Compile();

                    if (invoker == null)
                    {
                        invoker = (interceptor, context, obj) =>
                        {
                            if (context.HasMessageTypeBeenDefined(messageType))
                            {
                                return () => true;
                            }

                            context.MessageTypeWasDefined(messageType);

                            return connector(interceptor, context, obj);
                        };
                    }
                    else
                    {
                        invoker += (interceptor, context, obj) =>
                        {
                            if (context.HasMessageTypeBeenDefined(messageType))
                            {
                                return () => true;
                            }

                            context.MessageTypeWasDefined(messageType);

                            return connector(interceptor, context, obj);
                        };
                    }
                }

                return invoker;
            }));
        }
コード例 #16
0
        private static XmlSerializer GetSerializerFor <T>()
        {
            Type type = typeof(T);

            return(_serializers.Retrieve(type, () => new XmlSerializer(typeof(XmlMessageEnvelope), new[] { type })));
        }