我正在嘗試在MVC Core中構建一個簡單的博客網站。如何使用Simple Injector將類注入到我的自定義模型聯編程序提供程序中?
目前,我有機管局自定義模型粘合劑供應商看起來是這樣的(其中IBlogRepository是使用NHibernate我的數據庫存儲庫):
public class PostModelBinderProvider : IModelBinderProvider
{
IBlogRepository _blogRepository;
public PostModelBinderProvider(IBlogRepository blogRepository)
{
_blogRepository = blogRepository;
}
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
//code
}
}
而且它註冊這樣的:
services.AddMvc(config => config.ModelBinderProviders.Insert(0, new PostModelBinderProvider(container.GetInstance<IBlogRepository>())));
但是,當我嘗試運行該應用程序時遇到此異常: SimpleInjector.ActivationException: 'The ISession is registered as 'Async Scoped' lifestyle, but the instance is requested outside the context of an active (Async Scoped) scope.'
當前,我的前端進行ajax調用,將博客文章數據(標題,內容和標籤)發送到服務器。標籤由與其在數據庫中的唯一ID相對應的整數表示。模型聯編程序正在對標記數據庫進行查找,然後保存該帖子。
所以我在網上看看,似乎有關於是否應該在模型活頁夾中觸摸數據庫的分歧意見。
問:
因此,假設它的好,從在模型綁定數據庫中獲取數據,如何讓我與它簡單的噴油器的工作?
或者,如果不能從模型聯編程序中的數據庫中獲取數據,我會在哪裏放置此邏輯?
輝煌,那就是訣竅!謝謝! – nightflames