0

I am using simple injector (4.8.1) on a project that uses asp.net core identity. I tried to crosswise UserManager but I got an error saying No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Tenancy.Core.Domain.AppUser]' has been registered." the SimpleInjectorConfig class is:

public static class SimpleInjectorConfig
{
    private static Container _container;

    public static void ConfigureServices(IServiceCollection services, IConfiguration config,
        IWebHostEnvironment env)
    {
        _container = new Container();

        _container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

        _container.ConfigureCore(config, env);

        _container.RegisterInitializer<BaseApiController>(controller =>
        {
            controller.Mediator = _container.GetInstance<IMediator>();
        });

        services.AddSingleton(_container);
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        services.AddControllers();

        services.AddLogging();
        // Sets up the basic configuration that for integrating Simple Injector with
        // ASP.NET Core by setting the DefaultScopedLifestyle, and setting up auto
        // cross wiring.
        services.AddSimpleInjector(_container, options =>
        {
            // AddAspNetCore() wraps web requests in a Simple Injector scope and
            // allows request-scoped framework services to be resolved.
            options
                .AddAspNetCore()
                .AddControllerActivation();
            options.AddLogging();
            options.CrossWire<UserManager<AppUser>>();
        });
    }

    public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseSimpleInjector(_container);
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            IdentityModelEventSource.ShowPII = true;
        }
        //app.UseHttpsRedirection();

        _container.Verify();
    }
}

and I called it in my Startup.cs file

 public class Startup
{
    public IConfiguration _configuration { get; }
    public IWebHostEnvironment _env { get; }

    public Startup(IConfiguration configuration, IWebHostEnvironment env)
    {
        _configuration = configuration;
        _env = env;
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        SimpleInjectorConfig.ConfigureServices(services, _configuration, _env);
        CorsConfig.ConfigureServices(services);
        DatabaseConfig.ConfigureServices(services, _configuration);
        MvcConfig.ConfigureServices(services);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        SimpleInjectorConfig.Configure(app, env);
        CorsConfig.Configure(app, _configuration);
        MvcConfig.Configure(app);
    }
}

The configurations have their own class and called in the Startup called in the Startup class just to keep it clean, SimpleInjectorConfig is the config class for simple injector.

4
  • 1
    Please show more relevant parts of your configuration. Currently, your problem is not reproducible.
    – Steven
    Dec 22, 2020 at 8:04
  • @Steven I made an update. hope it's better now. Thanks
    – James D
    Dec 23, 2020 at 10:11
  • Are you missing an AddIdentity call? Generally speaking, there is no need to explicitly call CrossWire as Simple Injector's AddSimpleInjector does this for you under the covers. However, the requested type must still exist in .NET Core configuration system.
    – Steven
    Dec 23, 2020 at 11:16
  • Arrgh...How could I have missed that. Thanks @Steven. That was the issue. Add Identity .
    – James D
    Dec 23, 2020 at 16:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy