Skip to content

EnableCurrentScope option gets overwritten #193

@kemsky

Description

@kemsky

Currently we are migrating legacy app to Asp.Net Core 2.2 as the first step, so we are using LightInject.Microsoft.AspNetCore.Hosting.

There are several interceptors that access current scope, for example transaction interceptor that automatically starts DB transaction, within interceptor we want to get scoped instance of EF context: Container.GetInstance<IContext>(), it requires current scope to work. I've tried the following:

        public static IWebHost CreateWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseLightInject(options =>
                {
                    options.EnableCurrentScope = true;
                })
                .UseStartup<Startup>()
                .Build();

But it turns out that EnableCurrentScope gets overwritten:

        public LightInjectServiceProviderFactory(ContainerOptions options)
        {
            var clonedOptions = options.Clone();
            clonedOptions.WithMicrosoftSettings(); // <--- Here!
            containerFactory = () => new ServiceContainer(clonedOptions);
        }

Now I'm confused, was it intentional? Maybe we are doing something wrong?

Looks like this is the same issue as #87.


I tried another overload:

                .UseLightInject(new ServiceContainer(new ContainerOptions
                {
                    EnableVariance = false,
                    EnableCurrentScope = true,
                    EnableOptionalArguments = true,
                    EnablePropertyInjection = false
                }))

It kinda works, but only for the first call, next times scopes are in incorrect order and it breaks.

Does LightInject support current scope/nested scopes in Asp.Net Core at all?

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