asp.net.net-coreevent-flow

ASP.NET Core 2 with EventFlow configuration


EventFlow have avery limited example on how to configure on dotnetcore which is based on dotnet core 1 and things changed a little but in dotnet core 2

Is there a way to use EventFlow configuration without Autofac?

There is discussion about here and the last comments are about the same thing I am asking here but no answers

https://github.com/eventflow/EventFlow/issues/158

basically I want to find a way to use the build in DI with doing some thing like

services.AddEventFlowOptions.New...

or

var resolver = EventFlowOptions.New.UseDotnetCoreServices(services)...

or ... anything else that you guys used?


Solution

  • I used this and it is working fine. What it looks like is that you pass in services into EventFlow's IoC AuotFac and it wraps around that.

    As you can see you use the known ASP.NET Core API as usual, you Inject the same way without change in your Contollers, etc.

    The only thing I changed was void ConfigureServices to IServiceProvider ConfigureServices - I am not sure if that actually impacts anything but it works.

    You will need these packages

    In Startup.cs

    public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
    
            var containerBuilder = new ContainerBuilder();
    
            var container = EventFlowOptions.New
                .UseAutofacContainerBuilder(containerBuilder)
                .AddDefaults(EventFlowTestHelpers.Assembly)
                .AddAspNetCoreMetadataProviders();
    
    
            containerBuilder.Populate(services);
    
            return new AutofacServiceProvider(containerBuilder.Build());
        }
    

    and you need to use some MiddleWare provided by the package

      public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseMiddleware<CommandPublishMiddleware>();
            app.UseMvcWithDefaultRoute();//or whatever you are doing
        }