public static IApplicationBuilder UseEventBus(this IApplicationBuilder app) { using (var serviceScope = app.ApplicationServices .GetRequiredService <IServiceScopeFactory>() .CreateScope()) { var busClient = serviceScope.ServiceProvider.GetService <IBusClient>(); IntegrationEvent.Subscribe(busClient, message => { using (var mediatorServiceScope = app.ApplicationServices .GetRequiredService <IServiceScopeFactory>() .CreateScope()) { return(mediatorServiceScope.ServiceProvider.GetService <IMediator>().Publish(message)); } }); } return(app); }