I am trying to inject the API dependency and MVC dependency. But when I try to inject the MVC Dependency I get the error:
The configuration is invalid. The following diagnostic warnings were reported: -[Lifestyle Mismatch] FeedbackDbRepository (Web Request) depends on ChatBotDbContext (Transient). -[Disposable Transient Component] ChatBotDbContext is registered as transient, but implements IDisposable. 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.
Which happens in the RegisterMvcDependencies()
in here:
public static void RegisterWebApiDependencies()
{
//TODO: setup dependency injection for Web Api
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
container.Register<IAnswerGenerator, PxlAnswerGenerator>(Lifestyle.Singleton);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
}
public static void RegisterMvcDependencies()
{
//TODO: setup dependency injection for MVC
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
container.Register<IFeedbackRepository, FeedbackDbRepository>(Lifestyle.Scoped);
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
This is my application_start():
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
DependencyConfig.RegisterWebApiDependencies();
DependencyConfig.RegisterMvcDependencies();
}
It does work when I comment out RegisterMvcDependencies()
how can I solve this?
EDIT ChatBotDbContext is registered here:
public class FeedbackDbRepository : IFeedbackRepository//TODO: implement IFeedbackRepository
{
private readonly ChatBotDbContext _context;
public FeedbackDbRepository(ChatBotDbContext context)
{
_context = context;
}
//Tip1: use async await
//Tip2: use SaveChangesAsync() instead of SaveChanges()
public async Task AddAsync(Feedback newFeedback)
{
_context.Feedbacks.Add(newFeedback);
await _context.SaveChangesAsync();
}
}
ChatBoxDBContext:
public class ChatBotDbContext : DbContext //TODO: inherit from some other class
{
public ChatBotDbContext()
: base("DefaultConnection")
{
}
public DbSet<Feedback> Feedbacks { get; set; }
public DbSet<User> Users { get; set; }
public static ChatBotDbContext Create()
{
return new ChatBotDbContext();
}
public static void SetInitializer()
{
//TODO: make sure Entity Framework creates the database if it does not exists and migrates an existing database to the latest version
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ChatBotDbContext, Migrations.Configuration>());
}
}
Change it to be a scoped service. From the docs:
Warning: Transient instances are not tracked by the container. This means that Simple Injector will not dispose transient instances. Simple Injector will detect disposable instances that are registered as transient when calling container.Verify(). Please view Diagnostic Warning - Disposable Transient Components for more information.
You can read more on the error here: https://simpleinjector.readthedocs.io/en/latest/disposabletransientcomponent.html