private static IActiveMqBuilder AddProducer <TProducer>(this IActiveMqBuilder builder, ProducerConfiguration producerConfiguration, ServiceLifetime producerLifetime) where TProducer : class
        {
            if (builder.Services.Any(x => x.ServiceType == typeof(TProducer)))
            {
                var message =
                    $"There has already been registered Producer with the type '{typeof(TProducer).FullName}'. " +
                    "Typed Producer must be unique. " +
                    "Consider using inheritance to create multiple unique types with the same API surface.";
                throw new InvalidOperationException(message);
            }

            builder.Services.Configure <ActiveMqOptions>(builder.Name, options =>
            {
                if (!options.AddressConfigurations.TryGetValue(producerConfiguration.Address, out var routingTypes))
                {
                    routingTypes = new HashSet <RoutingType>();
                    options.AddressConfigurations.Add(producerConfiguration.Address, routingTypes);
                }

                if (producerConfiguration.RoutingType.HasValue)
                {
                    routingTypes.Add(producerConfiguration.RoutingType.Value);
                }
                else
                {
                    routingTypes.Add(RoutingType.Anycast);
                    routingTypes.Add(RoutingType.Multicast);
                }
            });

            builder.Services.AddSingleton(provider =>
            {
                var sendObservable           = provider.GetServices <SendObservable>().Single(x => x.Name == builder.Name);
                var logger                   = provider.GetService <ILogger <TypedActiveMqProducer <TProducer> > >();
                var contextualSendObservable = new ContextualSendObservable(sendObservable)
                {
                    Address     = producerConfiguration.Address,
                    RoutingType = producerConfiguration.RoutingType
                };
                return(new TypedActiveMqProducer <TProducer>(logger, async token =>
                {
                    var connection = await provider.GetConnection(builder.Name, token);
                    return await connection.CreateProducerAsync(producerConfiguration, token).ConfigureAwait(false);
                }, contextualSendObservable));
            });
            builder.Services.AddSingleton <IActiveMqProducer>(provider => provider.GetRequiredService <TypedActiveMqProducer <TProducer> >());
            builder.Services.Add(ServiceDescriptor.Describe(typeof(TProducer),
                                                            provider => ActivatorUtilities.CreateInstance <TProducer>(provider, provider.GetRequiredService <TypedActiveMqProducer <TProducer> >()),
                                                            producerLifetime));
            return(builder);
        }
예제 #2
0
 public TypedActiveMqProducer(ILogger <TypedActiveMqProducer <T> > logger, Func <CancellationToken, Task <IProducer> > producerFactory, ContextualSendObservable sendObservable)
 {
     _logger          = logger;
     _producerFactory = producerFactory;
     _sendObservable  = sendObservable;
 }