Sitecore 8.1 MVC 项目中的 Castle Windser 实施问题
Castle Windser implementation issue in Sitecore 8.1 MVC project
我将在 Sitecore 8.1 MVC project.At 网站级别实现 castle windsor DI 我已经创建了界面,class 和存储库,因为 well.I 已经阅读了各种文章但没有'没有找到直接的方法。
控制器部分如下:
public class CommonController : Controller
{
private readonly ICommon _service;
public CommonController(ICommon commonService)
{
this._service = commonService;
}
public ActionResult GetProductDetail()
{
var CommonModel = _service.GetProductDetail();
return View(CommonModel);
}
}
浏览时出现以下错误:
Server Error in '/' Application.
No parameterless constructor defined for this object.
我知道还需要实现 DI 部分。
我已经完成了以下文章的解决方案:Using Castle Windsor with Sitecore MVC for Dependency Injection
但现在出现以下错误:
The given key was not present in the dictionary. Description: An unhandled exception occurred.
有什么建议吗?
文章告诉你如何配置windsor。但是它没有告诉你的是你必须配置你的容器。
要让 windsor 能够解析您的 ICommon commonService,您需要告诉它必须如何解析。
有几种方法可以做到这一点。下面只是我如何在一个项目中使用它的一个示例。
希望对你有帮助。
汉斯
public class WebInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For(typeof(ICommon)).ImplementedBy(typeof(CommonService)).LifestyleTransient());
}
}
public static class DependencyInjection
{
private static IWindsorContainer container;
private static readonly object padlock = new object();
public static IWindsorContainer Container
{
get
{
lock (padlock)
{
if (container == null)
{
InitializeDependencyInjection();
}
return container;
}
}
}
private static void InitializeDependencyInjection()
{
container = new WindsorContainer();
container.AddFacility<FactorySupportFacility>();
container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));
container.Install(new Core.Installers.WebInstaller());
// Wiring up DI for the SOLR integration
//new WindsorSolrStartUp(container).Initialize();
}
/// <summary>
/// http://www.superstarcoders.com/blogs/posts/using-castle-windsor-with-sitecore-mvc-for-dependency-injection.aspx
/// </summary>
public static void SetupControllerFactory()
{
Container.Install(FromAssembly.This());
// Transient needed for sitecore running multiple instances of a controller for controller renderings
Container.Register(Classes.FromThisAssembly().BasedOn<IController>().LifestyleTransient());
IControllerFactory controllerFactory = new WindsorControllerFactory(Container);
var scapiSitecoreControllerFactory = new SitecoreControllerFactory(controllerFactory);
ControllerBuilder.Current.SetControllerFactory(scapiSitecoreControllerFactory);
}
}
public class InitializeWindsorControllerFactory
{
public virtual void Process(PipelineArgs args)
{
DependencyInjection.SetupControllerFactory();
}
}
我将在 Sitecore 8.1 MVC project.At 网站级别实现 castle windsor DI 我已经创建了界面,class 和存储库,因为 well.I 已经阅读了各种文章但没有'没有找到直接的方法。 控制器部分如下:
public class CommonController : Controller
{
private readonly ICommon _service;
public CommonController(ICommon commonService)
{
this._service = commonService;
}
public ActionResult GetProductDetail()
{
var CommonModel = _service.GetProductDetail();
return View(CommonModel);
}
}
浏览时出现以下错误:
Server Error in '/' Application.
No parameterless constructor defined for this object.
我知道还需要实现 DI 部分。
我已经完成了以下文章的解决方案:Using Castle Windsor with Sitecore MVC for Dependency Injection
但现在出现以下错误:
The given key was not present in the dictionary. Description: An unhandled exception occurred.
有什么建议吗?
文章告诉你如何配置windsor。但是它没有告诉你的是你必须配置你的容器。
要让 windsor 能够解析您的 ICommon commonService,您需要告诉它必须如何解析。
有几种方法可以做到这一点。下面只是我如何在一个项目中使用它的一个示例。
希望对你有帮助。
汉斯
public class WebInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For(typeof(ICommon)).ImplementedBy(typeof(CommonService)).LifestyleTransient());
}
}
public static class DependencyInjection
{
private static IWindsorContainer container;
private static readonly object padlock = new object();
public static IWindsorContainer Container
{
get
{
lock (padlock)
{
if (container == null)
{
InitializeDependencyInjection();
}
return container;
}
}
}
private static void InitializeDependencyInjection()
{
container = new WindsorContainer();
container.AddFacility<FactorySupportFacility>();
container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));
container.Install(new Core.Installers.WebInstaller());
// Wiring up DI for the SOLR integration
//new WindsorSolrStartUp(container).Initialize();
}
/// <summary>
/// http://www.superstarcoders.com/blogs/posts/using-castle-windsor-with-sitecore-mvc-for-dependency-injection.aspx
/// </summary>
public static void SetupControllerFactory()
{
Container.Install(FromAssembly.This());
// Transient needed for sitecore running multiple instances of a controller for controller renderings
Container.Register(Classes.FromThisAssembly().BasedOn<IController>().LifestyleTransient());
IControllerFactory controllerFactory = new WindsorControllerFactory(Container);
var scapiSitecoreControllerFactory = new SitecoreControllerFactory(controllerFactory);
ControllerBuilder.Current.SetControllerFactory(scapiSitecoreControllerFactory);
}
}
public class InitializeWindsorControllerFactory
{
public virtual void Process(PipelineArgs args)
{
DependencyInjection.SetupControllerFactory();
}
}