Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Container fails on verification #622

Closed
voroninp opened this issue Oct 12, 2018 · 3 comments
Closed

Container fails on verification #622

voroninp opened this issue Oct 12, 2018 · 3 comments
Labels

Comments

@voroninp
Copy link

voroninp commented Oct 12, 2018

I am using Simple Injector with ASP.NET Core. Cross-wiring is enabled and components are auto-cross-wired.

I registered EF's MyDbContext with AddDbContext extension method, and MyDbContext implements IFoo interface.
I register service MyService which expects IFoo as a constructor parameter.

_container.Register<IFoo, MyDbContext>(Lifestyle.Scoped);
_container.Register<IMyService, MyService>(Lifestyle.Scoped);

When I call Verify on container inside InitializeContaner method, MyService is constructed and receives null for IFoo.

@dotnetjunkie
Copy link
Collaborator

dotnetjunkie commented Oct 12, 2018

When I call Verify on container inside InitializeContaner method, MyService is constructed and receives null for IFoo.

This last statement makes little sense, because Simple Injector never injects null into a constructor, and this is a very conscious design. Please explain what you are doing in particular, because if null is injected`, either Simple Injector is not involved, or you are doing something differently that would be interesting information for your question.

@voroninp
Copy link
Author

voroninp commented Oct 12, 2018

@dotnetjunkie , ok, here's mu Startup class:

public class Startup
{
    private readonly Container _container = new Container();

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddDbContext<DeviceContext>(o =>
            {
                o.UseSqlServer(@"[connstr]");
            });

        IntegrateSimpleInjector(services);
    }

    // This method gets called by the runtime. Use this method to configure the 
    // HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        InitializeContainer(app);

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc();
    }

    private void IntegrateSimpleInjector(IServiceCollection services)
    {
        _container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        services.UseSimpleInjectorAspNetRequestScoping(_container);
        services.EnableSimpleInjectorCrossWiring(_container);
    }

    private void InitializeContainer(IApplicationBuilder app)
    {
        // Allow Simple Injector to resolve services from ASP.NET Core.
        _container.AutoCrossWireAspNetComponents(app);

        _container.Register<IFoo, DeviceContext>(Lifestyle.Scoped);
        _container.Register<IDeviceTypeService, DeviceTypeService>(Lifestyle.Scoped);

        _container.Verify(VerificationOption.VerifyAndDiagnose);
    }
}

and here is the service:

public class DeviceTypeService : IDeviceTypeService
{
    private readonly IFoo _foo;

    public DeviceTypeService(IFoo foo)
    {
        Guard.ArgumentNotNull(_foo, nameof(foo)); // this throws, if foo is null.

        _foo = foo;
    }
}

public interface IFoo { }

VS shows me exception:

System.InvalidOperationException
HResult=0x80131509
Message=The configuration is invalid. Creating the instance for type DeviceTypeService failed. Expected instance of type 'Connect.Device.BusinessModel.Services.IFoo, Connect.Device.BusinessModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null', but got null in member '.ctor'.
Parameter name: foo
Source=SimpleInjector
StackTrace:
at SimpleInjector.InstanceProducer.VerifyInstanceCreation()
at SimpleInjector.Container.VerifyInstanceCreation(InstanceProducer[] producersToVerify)
at SimpleInjector.Container.VerifyInternal(Boolean suppressLifestyleMismatchVerification)
at SimpleInjector.Container.Verify(VerificationOption option)
at Connect.Device.Service.Startup.InitializeContainer(IApplicationBuilder app) in Startup.cs:line 85
at Connect.Device.Service.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in Startup.cs:line 50

Inner Exception 1:
ActivationException: Expected instance of type 'Connect.Device.BusinessModel.Services.IFoo, Connect.Device.BusinessModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null', but got null in member '.ctor'.
Parameter name: foo

Inner Exception 2:
ArgumentNullException: Expected instance of type 'Connect.Device.BusinessModel.Services.IFoo, Connect.Device.BusinessModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null', but got null in member '.ctor'.

As soon as I remove DbContext registration, I get the expected complaint from injector that IFoo is not registered.

@voroninp
Copy link
Author

Ooops. I am an Idiot. Sorry =(

mistyped... foo and _foo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants