Entity framework Windsor和DbContext每个请求-已释放DbContext

Entity framework Windsor和DbContext每个请求-已释放DbContext,entity-framework,castle-windsor,dbcontext,Entity Framework,Castle Windsor,Dbcontext,我在HomeController中有一个方法,我试图通过URL访问该方法,如下所示: http://localhost/web/home/GetSmth public class HomeController : Controller { private ISomeService _someService; public HomeController(ISomeService someService) { _someSe

我在HomeController中有一个方法,我试图通过URL访问该方法,如下所示:

http://localhost/web/home/GetSmth
public class HomeController : Controller
{
        private ISomeService _someService;

        public HomeController(ISomeService someService)
        {
            _someService = someService;            
        }

        public ActionResult Index()
        {     
            return View();
        }

        public JsonResult GetSmth()
        {
            var data = _someService.GetData().ToList();
            return Json(data, JsonRequestBehavior.AllowGet);
        }
}
第一次工作时,但刷新页面后,出现以下错误:

The operation cannot be completed because the DbContext has been disposed.
正如标题所述,我试图在每个请求中使用Castle Windsor和DbContext

       public class Installer1 : IWindsorInstaller
            {
                public void Install(IWindsorContainer container, IConfigurationStore store)
                {
                    container.Register(Classes.FromThisAssembly()
                                    .BasedOn<IController>()
                                    .LifestyleTransient()                            
                                    );

                    var connString = ConfigurationManager.ConnectionStrings["MainDbContext"].ConnectionString;

                    container.Register(Component.For<MainDbContext>().DependsOn(Property.ForKey("conn").Eq(connString)).LifeStyle.PerWebRequest);
                    container.Register(Component.For<ISomeService>().ImplementedBy<SomeService>());
                }
}

您正在使用默认生命周期(即singleton)注册
ISomeService
。创建后,它将继续使用相同的DbContext。最简单的解决方案是将其生命周期更改为按请求或瞬态

container.Register(Component.For<ISomeService>()
                            .ImplementedBy<SomeService>()
                            .LifeStyle.PerWebRequest);
container.Register(Component.For())
.由()实施
.生活方式。个人网络请求);

这意味着所有使用DbContext的类(服务、存储库、查询对象等)都应该使用这个生命周期?@andree这是真的。Castle Windsor甚至警告您,请在调试器上查看。