creating WindsorContainer results in type conversion error

放肆的年华 提交于 2019-12-10 12:18:42

问题


I am trying to following book : Pro ASP.NET MVC Framework by Steven Sanderson...

The line container = new WindsorContainer(xi); produces this error:

Could not convert from 'DomainModel.Abstract.IPeopleRepository, DomainModel' to System.Type - Maybe type could not be found**

public WindsorControllerFactory()
{
    ConfigResource confres = new ConfigResource("castle");

    XmlInterpreter xi = new XmlInterpreter(confres);

    container = new WindsorContainer(xi);

    // Also register all the controller types as transient
    var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                          where typeof(IController).IsAssignableFrom(t)
                          select t;

    foreach (Type t in controllerTypes)
    {
        container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
    }
}

The castle config:

  <castle>
    <components>
      <component id="PeopleRepository"
                 service="DomainModel.Abstract.IPeopleRepository, DomainModel"
                 type="DomainModel.Concrete.FakePeopleRepository, DomainModel" 
                 >

How can this be fixed?


回答1:


Sanderson's book is outdated about the MVC-Windsor integration. Don't code your own (or the book's) controller factory. Use MVCContrib's Windsor integration instead.

Also, registering controllers within the controller factory is plain bad practice. The controller factory is there to instantiate controllers, not to manage container registrations.

UPDATE: MvcContrib for ASP.NET MVC 3 removed all controller factories and deprecated all IoC integrations.




回答2:


I'd lose the empty PeopleController constructor



来源:https://stackoverflow.com/questions/4103383/creating-windsorcontainer-results-in-type-conversion-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!