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; })); }
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); } }
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; })); }
public static Type GetProxyFor(Type typeToProxy) { return(_proxyTypes.Retrieve(typeToProxy, () => { ModuleBuilder moduleBuilder = GetModuleBuilderForType(typeToProxy); return BuildTypeProxy(moduleBuilder, typeToProxy); })); }
private static XmlSerializer GetDeserializerFor(Type type) { return(_deserializers.Retrieve(type, () => { XmlAttributeOverrides overrides = new XmlAttributeOverrides(); overrides.Add(type, _attributes); return new XmlSerializer(type, overrides); })); }
/// <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)); }
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; })); }
public static Type GetProxyFor(Type typeToProxy) { return(_proxyTypes.Retrieve(typeToProxy, () => { Type proxyType = null; GetModuleBuilderForType(typeToProxy, moduleBuilder => { proxyType = BuildTypeProxy(moduleBuilder, typeToProxy); }); return proxyType; })); }
/// <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; })); }
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; })); }
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); }
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; })); }
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); } }
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; })); }
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; })); }
private static XmlSerializer GetSerializerFor <T>() { Type type = typeof(T); return(_serializers.Retrieve(type, () => new XmlSerializer(typeof(XmlMessageEnvelope), new[] { type }))); }