使用Castle.Windsor从web.config注入值

问题描述 投票:2回答:1

我正在将一些应用程序迁移到.NET Core,并且必须从web.config注入值。

使用.Net Framework(4.7.2),我使用Dependency.OnAppSettingsValue做到了。但是,当我迁移到.NET Core(3.0)或.NET Standard(2.0)时,再也找不到此选项。

我正在使用温莎城堡(5.0.1)和Castle Core(4.4.0)。

container.Register(Component.For<IMigration>()
    .ImplementedBy<SchemaMigration>()
    .LifestyleTransient()
    .DependsOn(Dependency.OnAppSettingsValue("createIndexes", "NHibernate.CreateIndexes")));

如何在.NET Core 3.0中做到这一点?

c# asp.net-core dependency-injection castle-windsor castle
1个回答
0
投票

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@-&&& /////////// && // && / && / && / &&&&&&&&&&& ///////////////////////////// &&&&&&&&&&&&&&&& ///////////////////////////////////////////&///////////。//////// &&&&---也是。

要从IConfiguration解析,您可以实现这样的变体

his blogpost

通过传递IConfiguration对象将解析器添加到windsor

public class ConfigurationConvention : ISubDependencyResolver
{
    private readonly Microsoft.Extensions.Configuration.IConfiguration _configuration;

    public ConfigurationConvention(Microsoft.Extensions.Configuration.IConfiguration configuration)
    {
        _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
    }

    public bool CanResolve(
       CreationContext context,
       ISubDependencyResolver contextHandlerResolver,
       ComponentModel model,
       DependencyModel dependency)
    {
        return _configuration[dependency.DependencyKey] != null
            && TypeDescriptor
            .GetConverter(dependency.TargetType)
            .CanConvertFrom(typeof(string));
    }

    public object Resolve(
        CreationContext context,
        ISubDependencyResolver contextHandlerResolver,
        ComponentModel model,
        DependencyModel dependency)
    {
        return TypeDescriptor
            .GetConverter(dependency.TargetType)
            .ConvertFrom(
                _configuration[dependency.DependencyKey]);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.