Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to use Simple Injector with Asp.net core MVC

$
0
0

Hello All,

I am developing an application using Asp.Net core MVC. And want to use Simple Injector as Dependency resolver. so I am done with everything as per guidelines but when run the application it asking to register the ApplicationDBContext.

Exceptions as :

An exception of type 'System.InvalidOperationException' occurred in SimpleInjector.dll but was not handled in user code

Additional information: The configuration is invalid. Creating the instance for type HomeController failed. No registration for type ApplicationDbContext could be found and an implicit registration could not be made. The constructor of type ApplicationDbContext contains the parameter with name 'options' and type DbContextOptions<ApplicationDbContext> that is not registered. Please ensure DbContextOptions<ApplicationDbContext> is registered, or change the constructor of ApplicationDbContext.

See below code of Startup file: 

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            //services.AddSingleton<IEmployeeServices, EmployeeServices>();

             services.AddSingleton<IControllerActivator>(new SimpleInjectorControllerActivator(container));
             services.AddSingleton<IViewComponentActivator>(new SimpleInjectorViewComponentActivator(container));

            // Configure database connection
            //services.AddDbContext<ApplicationDbContext>(options =>
            //        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            //Simple Injector
            app.UseSimpleInjectorAspNetRequestScoping(container);
            container.Options.DefaultScopedLifestyle = new AspNetRequestLifestyle();
            InitializeContainer(app);
            container.Verify();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseStaticFiles();
            app.UseMvc(configureRoutes);

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
        private void InitializeContainer(IApplicationBuilder app)
        {     // Add application presentation components:
            container.RegisterMvcControllers(app);
            container.RegisterMvcViewComponents(app);

            // Add application services. For instance:

            var optionsBuilder = new DbContextOptionsBuilder();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            container.Register(() => new DbContext(optionsBuilder.Options), Lifestyle.Scoped);

            container.Register<ApplicationDbContext>(Lifestyle.Scoped);
            container.Register<IEmployeeServices, EmployeeServices>(Lifestyle.Singleton);

        }

 


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>