Castle Windsor web.config参数

时间:2017-04-07 09:54:46

标签: c# web-config castle-windsor

我会在我的webapplication的web.config中传输Windor安装程序,但我需要传递一个类的静态属性参数。有一个例子:

// (namespace Web)

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.Register(
        Component.For(typeof(IRepository))
            .ImplementedBy(typeof(Repository<MyCtx>))
            .DependsOn(Dependency.OnValue("store", MvcApplication.GlobalStore))
            .LifestylePerWebRequest()
    );
}

我的实际web.config:

<components>
  <component
      service="Core.Business.IRepository, Core"
      type="EF.Business.Repository, EF"
      lifestyle="PerWebRequest">
    <parameters>
      <store>Web.MvcAppltication.GlobalStore ???</store>
    </parameters>
  </component>
</components>

1 个答案:

答案 0 :(得分:0)

我总结说必须在代码中定义一些东西。因此,我决定了创建设施的方式。

我的新代码:

<强>的Web.config

 <component
      service="Core.Business.IRepository, Core"
      type="EF.Business.Repository`1[[EF.CreationContext, Nodiink.Arcow.Data.EF]], EF"
      lifestyle="PerWebRequest">
  </component>

<强> DependencyResolver.cs

public class RuntimeDataSupportFacility : AbstractFacility
{
    protected override void Init()
    {
        Kernel.Resolver.AddSubResolver(new RuntimeDataDependencyResolver());
    }
}

public class RuntimeDataDependencyResolver : ISubDependencyResolver
{
    private Type[] acceptedTypes = new[] { typeof(IDictionary<String, Object>) };

    public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
    {
        if (ReferenceEquals(dependency.TargetType, acceptedTypes[0])) // typeof(IDictionary<String, Object>
            return MvcApplication.GlobalStore;

        return null;
    }

    public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
    {
        if (acceptedTypes.Any(t => ReferenceEquals(t, dependency.TargetType)))
            return true;

        return false;
    }
}

Global.asax .Application_Start():

Container =
    new WindsorContainer()
    .AddFacility<RuntimeDataSupportFacility>() // New code
    .Install(Configuration.FromAppConfig());