Skip to content

How to use it with EF Core bundle? #280

Description

@Herve-M

Hello!

I tried to migrated a project to this Mediator implementation, but I encounter an issue related to the build of EF Core bundle (and also during most ef design time flows) when the DbContext require IMediator while having a custom design-time setup.

One issue is related during the build of the bundle, who use BundleProjectGenerator.cs which csproj can't be customized neither be managed using preprocessor / symbol.

Another issue is related to most dotnet ef call, who fails as we can't have a dedicated setup flow, see below.

Context

  • .NET 8
  • EFCore 8.0.20
  • Mediator 3.0.2

Example

internal sealed class ADbContext {
         public ADbContext (DbContextOptions<ADbContext > options, ILogger<ADbContext > logger, IMediator mediator)
            : base(options)
        {
            _logger = logger;
            _mediator = mediator;
        }
}
  • While having a specific DI like:
internal sealed class Program
{
    // only used for EF Core design time resolving
    private static IHostBuilder CreateHostBuilder(string[] args)
    {
        IHostBuilder builder = Host.CreateDefaultBuilder(args);
        builder.ConfigureServices((hostContext, services) =>
        {
            services
                .AddMediator((MediatorOptions options) =>{ })
                 .UseInfrastructureLayer(hostContext.Configuration, hostContext.HostingEnvironment)
                      .WithDatabase() // call AddDbContext<>
                .Build();
        });
        return builder;
    }
   // only used for normal runs
   private static void Main(string[] args)
   {
     <many other stuff>
     builder.Services
            .AddMediator((MediatorOptions options) =>
            {
                options.Namespace = "myapp.Api.Server.Mediator";
                options.ServiceLifetime = ServiceLifetime.Scoped;
                options.GenerateTypesAsInternal = true;
                options.NotificationPublisherType = typeof(Mediator.ForeachAwaitPublisher);
                options.Assemblies =
                [
                    typeof(myapp.Domain.DependencyInjection).Assembly,
                    typeof(myapp.Application.DependencyInjection).Assembly,
                    typeof(myapp.Infrastructure.DependencyInjection).Assembly
                ];
                options.PipelineBehaviors =
                [
                    typeof(ValidationBehavior<,>),
                    typeof(InventoryTransactionBehavior<,>)
                ];
                options.StreamPipelineBehaviors = [];
            })
   }
}

while trying efcore cli run:

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Invalid configuration detected for Mediator. Generated code for 'Scoped' lifetime, but got 'Singleton' lifetime from options. This means that the source generator hasn't seen the 'AddMediator' method call during compilation. Make sure that the 'AddMediator' method is called from the project that references the Mediator.SourceGenerator package.

Then could try with setting up basic options:

    private static IHostBuilder CreateHostBuilder(string[] args)
    {
        IHostBuilder builder = Host.CreateDefaultBuilder(args);
        builder.ConfigureServices((hostContext, services) =>
        {
            services
                .AddMediator((MediatorOptions options) =>
                {
                    options.ServiceLifetime = ServiceLifetime.Scoped;
                    options.Assemblies = [];
                    options.PipelineBehaviors = [];
                    options.StreamPipelineBehaviors = [];
                })
                .UseInfrastructureLayer(hostContext.Configuration, hostContext.HostingEnvironment)
                      .WithDatabase() // call AddDbContext<>
                .Build();
        });
        return builder;
    }

but this can't be compiled due to this error: MediatorGenerator could not parse MediatorOptions-based configuration. Only compile-time constant values can be used in MediatorOptions configuration. Assemblies can only be configured once(MSG0007)

Is there anyway to setup an empty Mediator? (or did I miss something?)

Thanks,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions