ASP.NET Core 2 with EventFlow configuration

1.5k views Asked by At

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?

2

There are 2 answers

1
Piotr Kula On BEST ANSWER

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

  • EventFlow.Aspnetcore.Middlewares;
  • EventFlow.AspNetCore.Extensions;
  • EventFlow.Autofac.Extensions;

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
    }
0
Johnny On

Following on from the startup setup provided, i have created a simple web api solution that integrates with EventFlow with .net core 2.2. It uses the same commands/events from the source

Hope that helps!