3

我在温莎城堡有问题。请帮我!我正在使用 Castle Windsor 3.2.0.0、.Net 4.0 和 Mvc 3.0.0.0

非常感谢!

问题:找不到支持服务 Mvc IControllerFactory 的组件这里有一些代码。

public class WindsorDependencyResolver : IDependencyResolver
{
   private readonly IWindsorContainer _container;
   public WindsorDependencyResolver(IWindsorContainer container)
   {
       this._container = container;
   }

   public object GetService(Type serviceType)
   {
       return _container.Resolve(serviceType);
   }

   public IEnumerable<object> GetServices(Type serviceType)
   {
       return _container.ResolveAll(serviceType).Cast<object>();
       //return _container.ResolveAll<object>(serviceType);
   }
}

//Castle Windsor register
protected void Application_Start()
{
var container = new WindsorContainer();

container.AddFacility<TypedFactoryFacility>()
    .Register(Component.For<IServiceFactory>().AsFactory().LifeStyle.Transient);

        //Do register the service/type to container
container.Register(
  Types.FromThisAssembly()
  .BasedOn(typeof(Controller))
  .WithService.Self().LifestyleTransient());

container.Register(
  Component.For<IRepository>()
  .ImplementedBy<GenericRepository>().DependsOn(Dependency.OnValue("context", "ProjectContext"))
  .LifeStyle.PerWebRequest);

container.Register(
  Types.FromAssembly(Assembly.GetAssembly(typeof(IUserService)))
  .BasedOn(typeof(ServiceBase))
  .WithService.DefaultInterfaces().LifestylePerWebRequest());

DependencyResolver.SetResolver(new WindsorDependencyResolver(container));
}
4

2 回答 2

4

远离 DependencyResolver... 改用 ControllerFactory。

看看教程

于 2013-07-02T07:22:10.593 回答
1

同意克里斯蒂亚诺 - 这是我的做法,但它是两者的混合:

public class ContainerHostDependencyResolver : IDependencyResolver
{
    private static readonly IServiceLocator Locator = new ServiceLocator();
    private readonly IDependencyResolver _defaultResolver;
    private static readonly Type ControllerFactoryType = typeof (IControllerFactory);

    public ContainerHostDependencyResolver(IDependencyResolver defaultResolver)
    {
        _defaultResolver = defaultResolver;
    }

    public object GetService(Type serviceType)
    {
        if (ControllerFactoryType.IsAssignableFrom(serviceType))
            return Locator.Resolve(serviceType);

        return _defaultResolver.GetService(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return _defaultResolver.GetServices(serviceType);
    }
}

    protected void Application_Start()
    {
        DependencyResolver.SetResolver(new ContainerHostDependencyResolver(DependencyResolver.Current));
    }

编辑#1:这是一种更安全、更直接的方法,可以满足 Cristiano 的教程链接的要求。就我而言,我将 Windsor 包裹在我们自己的称为 ContainerHost/ServiceLocator 的后面。

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ControllerBuilder.Current.SetControllerFactory(new ContainerHostControllerFactory());
    }
}

public class ContainerHostControllerFactory : DefaultControllerFactory
{
    private static readonly IServiceLocator Locator = new ServiceLocator();

    public override IController CreateController(RequestContext requestContext, string controllerName)
    {
        return Locator.Resolve<IController>(controllerName + "Controller");
    }

    public override void ReleaseController(IController controller)
    {
        Locator.Release(controller);
    }
}
于 2013-07-02T17:34:18.730 回答