/// <summary>
        /// Create a mediator, which sends messages to consumers, handlers, and sagas. Messages are dispatched to the consumers asynchronously.
        /// Consumers are not directly coupled to the sender. Can be used entirely in-memory without a broker.
        /// </summary>
        /// <param name="selector"></param>
        /// <param name="configure"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IMediator CreateMediator(this IBusFactorySelector selector, Action <IMediatorConfigurator> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            var topologyConfiguration = new InMemoryTopologyConfiguration(InMemoryBus.MessageTopology);
            var busConfiguration      = new InMemoryBusConfiguration(topologyConfiguration, new Uri("loopback://localhost"));

            var endpointConfiguration = busConfiguration.HostConfiguration.CreateReceiveEndpointConfiguration("mediator");

            var configurator = new MediatorConfiguration(busConfiguration.HostConfiguration, endpointConfiguration);

            configure(configurator);

            var mediatorDispatcher = configurator.Build();

            var responseEndpointConfiguration = busConfiguration.HostConfiguration.CreateReceiveEndpointConfiguration("response");
            var responseConfigurator          = new ReceivePipeDispatcherConfiguration(busConfiguration.HostConfiguration, responseEndpointConfiguration);

            configurator = new MediatorConfiguration(busConfiguration.HostConfiguration, responseEndpointConfiguration);

            configure(configurator);

            var responseDispatcher = responseConfigurator.Build();

            return(new MassTransitMediator(LogContext.Current, endpointConfiguration, mediatorDispatcher, responseEndpointConfiguration, responseDispatcher));
        }
Esempio n. 2
0
        public Mediator(IServiceFactory serviceFactory, Action <MediatorConfiguration <T> > configure = null)
        {
            _serviceFactory = serviceFactory;
            MediatorConfiguration <T> config = new MediatorConfiguration <T>();

            configure?.Invoke(config);
            _configuration = config;
        }
Esempio n. 3
0
        public async Task Test()
        {
            var mediator = new MediatorConfiguration()
                           .RegisterHandlers(typeof(TestEvent).Assembly)
                           .CreateMediator();

            await mediator.PublishAsync(new TestEvent
            {
                Message = "test event"
            });

            TestStore.EventStore.Count.ShouldBe(2);
        }
        public async Task Test()
        {
            var mediator = new MediatorConfiguration()
                           .RegisterHandler <TestCommandHandler>()
                           .CreateMediator();

            var command = new TestCommand(Guid.NewGuid());

            await mediator.SendAsync(command);

            TestStore.CommandStore.Count.ShouldBe(1);
            TestStore.CommandStore.Single().ShouldBe(command);
        }
        public async Task Test()
        {
            var mediator = new MediatorConfiguration()
                           .RegisterHandlers(typeof(TestMultipleEvent1).Assembly)
                           .CreateMediator();

            await mediator.PublishAsync(new TestMultipleEvent1());

            TestStore.EventStore.Count.ShouldBe(1);

            await mediator.PublishAsync(new TestMultipleEvent2());

            TestStore.EventStore.Count.ShouldBe(2);
        }
Esempio n. 6
0
        public async Task Test()
        {
            var mediator = new MediatorConfiguration()
                           .RegisterHandler <TestRequestHandler>()
                           .CreateMediator();

            var request = new TestRequest
            {
                Message = "test request"
            };

            var response = await mediator.RequestAsync <TestRequest, TestResponse>(request);

            response.ShouldNotBeNull();
        }
Esempio n. 7
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            var mediator = new MediatorConfiguration()
                           .RegisterHandler <TestCommandHandlers>()
                           .UseMiddleware <TestMiddleware>()
                           .UseMiddleware(next =>
            {
                return(async message =>
                {
                    await next(message);
                });
            })
                           .CreateMediator();

            services.AddScoped(x => mediator);
        }
Esempio n. 8
0
 public static void Mediator(ref IServiceCollection services, Assembly startup, string externalAssemblyName = "")
 {
     MediatorConfiguration.Configure(services, startup, externalAssemblyName);
 }