如何使用Castle Windsor与mvc web应用程序不同的程序集?

时间:2020-11-26 00:05:53

I'm stuck. I want to configure Castle Windsor in a different class library and I configured, build is fine, no error, ok... But I get an exception at run time.

我卡住了。我想要在另一个类库中配置Castle Windsor,我配置了build is fine, no error, ok…但是在运行时我有一个例外。

Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service App.Web.UI.Controllers.HomeController was found

Castle.MicroKernel。ComponentNotFoundException:没有支持服务App.Web.UI.Controllers的组件。HomeController被发现

When I get the configure file back to the same assembly (App.Web.UI), I don't get any exception at run time, work is fine.

当我将配置文件恢复到相同的程序集(App.Web.UI)时,在运行时不会出现任何异常,工作是正常的。

I tried many ways, but I could not. Is there another bind method except FromThisAssembly? Or Solution?

我尝试了很多方法,但我做不到。除了这个程序集之外,还有其他绑定方法吗?或解决方案?

Castle Windsor configuration is here:

温莎城堡的配置如下:

namespace App.Infrastructure.Configurations
{
    public class WindsorControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel _kernel;

        public WindsorControllerFactory(IKernel kernel)
        {
            _kernel = kernel;
        }

        public override void ReleaseController(IController controller)
        {
            _kernel.ReleaseComponent(controller);
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
            }

            return (IController)_kernel.Resolve(controllerType);
        }
    }

    public class ControllersInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(Classes.FromThisAssembly().BasedOn<IController>().Unless(x => x.Name == "BaseController").LifestyleTransient());
        }
    }

    public class ServiceInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
                Component.For(typeof (IRepository<>)).ImplementedBy(typeof (Repository<>)).LifestyleTransient(),
                Component.For<IUserService>().ImplementedBy<UserService>().LifestylePerWebRequest(),
                Component.For<IFormsAuthenticationService>().ImplementedBy<FormsAuthenticationService>().LifestylePerWebRequest(),
                Component.For<ILoggingService>().ImplementedBy<LoggingService>().LifestyleTransient(),
                Component.For<IFeedbackService>().ImplementedBy<FeedbackService>().LifestylePerWebRequest()
                ); 
        }
    }
}

1 个解决方案

#1


2  

In your code, the Classes.FromThisAssembly() references the assembly containing your Windsor stuff -- not your web application. Try telling Windsor to register components from a specific assembly. This should work:

在代码中,class . fromthisassembly()引用包含Windsor内容的程序集——而不是web应用程序。尝试告诉Windsor从特定程序集中注册组件。这应该工作:

Classes.FromAssembly(Assembly.GetEntryAssembly())

The entry assembly should be the AppDomain running your MVC code.

入口程序集应该是运行MVC代码的AppDomain。

#1


2  

In your code, the Classes.FromThisAssembly() references the assembly containing your Windsor stuff -- not your web application. Try telling Windsor to register components from a specific assembly. This should work:

在代码中,class . fromthisassembly()引用包含Windsor内容的程序集——而不是web应用程序。尝试告诉Windsor从特定程序集中注册组件。这应该工作:

Classes.FromAssembly(Assembly.GetEntryAssembly())

The entry assembly should be the AppDomain running your MVC code.

入口程序集应该是运行MVC代码的AppDomain。