Reputation: 388
I'm new to using DI and ASP.NET and I'm having a hard time getting Simple Injector to work. I've added SimpleInjector 3.1.5 as a reference using NuGet. Here is what I have so far:
Global.asax.cs:
...
using SimpleInjector;
using SimpleInjector.Integration.WebApi;
using SimpleInjector.Integration.Web;
using SimpleInjector.Integration.Web.Mvc;
...
protected void Application_Start()
{
Database.SetInitializer<ProductContext>(new ProductInitializer());
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
container.Register<IProductRepository, ProductRepository>(Lifestyle.Scoped);
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
container.RegisterMvcIntegratedFilterProvider();
container.Verify();
}
Main controller:
...
public class DefaultController : Controller
{
private ProductRepository productRepo;
public DefaultController(ProductRepository pr)
{
this.productRepo = pr;
}
...
Product Repository class:
...
public class ProductRepository:IProductRepository
{
private ProductContext db;
public ProductRepository(ProductContext db)
{
this.db = db;
}
public IEnumerable<Product> GetAll()
{
return db.Products.ToList();
}
...
When I try to run my program, I get this error:
The configuration is invalid. The following diagnostic warnings were reported:
-[Lifestyle Mismatch] ProductRepository (Web Request) depends on ProductContext (Transient).
-[Short Circuited Dependency] DefaultController might incorrectly depend on unregistered type ProductRepository (Transient) instead of IProductRepository (Web Request).
-[Disposable Transient Component] ProductContext is registered as transient, but implements IDisposable.
-[Ambiguous Lifestyles] The registration for IProductRepository (Web Request) maps to the same implementation (ProductRepository) as the registration for ProductRepository (Transient) does, but the registration maps to a different lifestyle. This will cause each registration to resolve to a different instance.
-[Ambiguous Lifestyles] The registration for ProductRepository (Transient) maps to the same implementation (ProductRepository) as the registration for IProductRepository (Web Request) does, but the registration maps to a different lifestyle. This will cause each registration to resolve to a different instance.
See the Error property for detailed information about the warnings. Please see https://simpleinjector.org/diagnostics how to fix problems and how to suppress individual warnings.
Upvotes: 2
Views: 1734
Reputation: 658
Please update you Simple Injector to latest version and check updated documentation:
ASP.NET Core: https://simpleinjector.readthedocs.io/en/latest/aspnetintegration.html ASP.NET MVC: https://simpleinjector.readthedocs.io/en/latest/mvcintegration.html
Or select your integration option from left sidebar menu
Upvotes: 0