Windsor容器组件在第一个控制器操作中不可用

时间:2010-09-17 11:07:29

标签: asp.net-mvc model-view-controller asp.net-mvc-2 castle-windsor ioc-container

我正在使用global.asax.cs中的配置来注册组件,但看起来容器尚未在第一个http请求(HomeController> Index action)中初始化,它给了我一个“The ObjectContext实例已被释放,不能再用于需要连接的操作。“错误。

我无法找到解决方案并让我发疯!

我的global.asax.cs摘录:

protected void Application_Start()
{
    InitializeContainer();
    InitializeDatabase();
    RegisterRoutes(RouteTable.Routes);
}

private void InitializeContainer()
{
    _container = new WindsorContainer();

    ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));

    // Register context manager.
    _container.Register(
        Component.For<IContextManager>()
        .ImplementedBy<CoursesContextManager>()
        .LifeStyle.Singleton
        .Parameters(
    Parameter.ForKey("connectionString").Eq(ConfigurationManager.ConnectionStrings["CoursesConnection"].ConnectionString)   
        )
    );
    // Register specifc repository implementations (can we do this more generic?)
    _container.Register(
        Component.For<ICourseRepository>()
        .ImplementedBy<CourseRepository>()
        .LifeStyle.Singleton
    );

    [...other interfaces and controllers registered...]
}

在第一个http请求中抛出异常的控制器:

public class HomeController : Controller
{
    private ICourseRepository _courseRepository;

    public HomeController(ICourseRepository courseRepository)
    {
        _courseRepository = courseRepository;
    }

    public ActionResult Index()
    {
        var courses = _courseRepository.Find(); //here is where it fails
        return View(courses);
    }

}

存储库/接口:

通用接口:

public interface IRepository<T>
{
    IQueryable<T> Find();
}

通用存储库:

public class MyRepository<T> : IRepository<T> where T : class
{
    private IContextManager _contextManager;
    private string _qualifiedEntitySetName;
    private string _keyName;

    protected ObjectContext CurrentObjectContext
    {
        get { return _contextManager.GetContext(); }
    }

    protected ObjectSet<T> ObjectSet
    {
        get { return CurrentObjectContext.CreateObjectSet<T>(); }
    }

    public MyRepository(IContextManager contextManager)
    {
        this._contextManager = contextManager;
        this._qualifiedEntitySetName = string.Format("{0}.{1}"
            , this.ObjectSet.EntitySet.EntityContainer.Name
            , this.ObjectSet.EntitySet.Name);
        this._keyName = this.ObjectSet.EntitySet.ElementType.KeyMembers.Single().Name;
    }

    public IQueryable<T> Find()
    {
        return ObjectSet;
    }
}

基于通用存储库的接口课程:

public interface ICourseRepository : IRepository<Course>
{
}

2 个答案:

答案 0 :(得分:0)

如果您使用工作单元模式,您将解决您的问题

查看此帖子Unit Of Work Pattern,非常有用

答案 1 :(得分:0)

我找到了一种至少暂时处理这个问题的方法。因为问题发生在第一个请求上,我刚刚在我的控制器中添加了另一个操作并将索引操作重定向到它。可能不是最好的解决方案,但不能在这个问题上花更多的时间!

public class HomeController : Controller
{
    private ICourseRepository _courseRepository;

    public HomeController(ICourseRepository courseRepository)
    {
        _courseRepository = courseRepository;
    }

    public ActionResult Index() // Default action in the controller, first hit
    {
       return RedirectToAction("Home");
    }

    public ActionResult Home() //The repository is available here, no exception thrown
    {
        var courses = _courseRepository.Find(); //here is where it fails
        return View(courses);
    }

}
相关问题