/// <summary> /// This static method is used by <see cref="RabbitMqServiceBuilder{T}"/> to construct a singleton hosted service /// for a consumer of a producer. /// /// Its purpose is to instantiate the <c>TDerived</c>, passing in the <see cref="IServiceProvider"/> argument. /// /// The services that can be injected this way are the ones that have been registered up to the call site of /// <see cref="Create{TDerived}"/>. /// </summary> /// <param name="isConsumer"/> /// <param name="busProxy"/> /// <param name="rmqConfig"/> /// <param name="onConnected"/> /// <param name="serviceProvider"/> /// <typeparam name="TDerived"> /// The type of the consumer or producer that is being instantiated. It must match type parameter <c>T</c> of /// type <see cref="RabbitMqService{T}"/>. /// </typeparam> /// <returns> /// An instantiated <see cref="RabbitMqService{TDerived}"/> which should be used to register a consumer or /// producer as a singleton hosted service. /// </returns> public static TDerived Create <TDerived>( bool isConsumer, IBusProxy busProxy, IRabbitMqConfig rmqConfig, List <OnConnectedCallback> onConnected, IServiceProvider serviceProvider) where TDerived : RabbitMqService <TDerived> { // ReSharper disable once ConstantConditionalAccessQualifier Debug.Assert(busProxy?.Bus != null, $"{nameof(IBusProxy.Bus)} must not be null."); Debug.Assert(rmqConfig != null, $"{nameof(IRabbitMqConfig)} must not be null."); var logger = serviceProvider.GetService <ILogger <TDerived> >(); TDerived service; try { service = ActivatorUtilities.CreateInstance <TDerived>(serviceProvider); } catch (Exception) { // ReSharper disable once ConstantConditionalAccessQualifier logger?.LogCritical( $"Could not instantiate a {typeof(TDerived).Name} in {nameof(RabbitMqService<T>)}." + $"{nameof(Create)}."); throw; } service._isConsumer = isConsumer; service.IncomingMessageInterceptor = serviceProvider.GetService <IIncomingMessageInterceptor <T> >() ?? serviceProvider.GetService <IIncomingMessageInterceptor>(); service.OutgoingMessageInterceptor = serviceProvider.GetService <IOutgoingMessageInterceptor <T> >() ?? serviceProvider.GetService <IOutgoingMessageInterceptor>(); service._busProxy = busProxy; service._rmqConfig = rmqConfig; service._onConnected = onConnected; service._logger = logger; service._isProperlyInitialized = true; service.Initialize(); return(service); }
private BusSubscriptionToken AddSubscriptionInternal <TMessage>(Action <TMessage> deliveryAction, Func <TMessage, bool> messageFilter, bool strongReference, IBusProxy proxy) where TMessage : class, IBusMessage { if (deliveryAction == null) { throw new ArgumentNullException("deliveryAction"); } if (messageFilter == null) { throw new ArgumentNullException("messageFilter"); } if (proxy == null) { throw new ArgumentNullException("proxy"); } var subscriptionToken = new BusSubscriptionToken(this, typeof(TMessage)); IBusSubscription subscription; if (strongReference) { subscription = new StrongBusSubscription <TMessage>(subscriptionToken, deliveryAction, messageFilter); } else { subscription = new WeakBusSubscription <TMessage>(subscriptionToken, deliveryAction, messageFilter); } lock (_SubscriptionsPadlock) { _Subscriptions.Add(new SubscriptionItem(proxy, subscription)); } return(subscriptionToken); }
public BusSubscriptionToken Subscribe <TMessage>(Action <TMessage> deliveryAction, Func <TMessage, bool> messageFilter, bool useStrongReferences, IBusProxy proxy) where TMessage : class, IBusMessage { return(AddSubscriptionInternal <TMessage>(deliveryAction, messageFilter, useStrongReferences, proxy)); }
public BusSubscriptionToken Subscribe <TMessage>(Action <TMessage> deliveryAction, IBusProxy proxy) where TMessage : class, IBusMessage { return(AddSubscriptionInternal <TMessage>(deliveryAction, (m) => true, true, proxy)); }
public SubscriptionItem(IBusProxy proxy, IBusSubscription subscription) { Proxy = proxy; Subscription = subscription; }