Exemple #1
0
        /// <summary>
        /// Adds MassTransit and its dependencies to the <paramref name="collection" />, and allows consumers, sagas, and activities to be configured
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="configure"></param>
        public static IServiceCollection AddMassTransit(this IServiceCollection collection, Action <IServiceCollectionBusConfigurator> configure = null)
        {
            if (collection.Any(d => d.ServiceType == typeof(IBus)))
            {
                throw new ConfigurationException(
                          "AddMassTransit() was already called and may only be called once per container. To configure additional bus instances, refer to the documentation: https://masstransit-project.com/usage/containers/multibus.html");
            }

            var configurator = new ServiceCollectionBusConfigurator(collection);

            configure?.Invoke(configurator);

            return(collection);
        }
Exemple #2
0
        /// <summary>
        /// Configure a MassTransit bus instance, using the specified <typeparamref name="TBus" /> bus type, which must inherit directly from <see cref="IBus" />.
        /// A type that implements <typeparamref name="TBus" /> is required, specified by the <typeparamref name="TBusInstance" /> parameter.
        /// </summary>
        /// <param name="collection">The service collection</param>
        /// <param name="configure">Bus instance configuration method</param>
        public static IServiceCollection AddMassTransit <TBus, TBusInstance>(this IServiceCollection collection,
                                                                             Action <IServiceCollectionBusConfigurator <TBus> > configure)
            where TBus : class, IBus
            where TBusInstance : BusInstance <TBus>, TBus
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            if (collection.Any(d => d.ServiceType == typeof(TBus)))
            {
                throw new ConfigurationException(
                          $"AddMassTransit<{typeof(TBus).Name},{typeof(TBusInstance).Name}>() was already called and may only be called once per container. To configure additional bus instances, refer to the documentation: https://masstransit-project.com/usage/containers/multibus.html");
            }

            var configurator = new ServiceCollectionBusConfigurator <TBus, TBusInstance>(collection);

            configure(configurator);

            return(collection);
        }