我使用温莎与ASP.NET MVC4
和我已经写了一个自定义RoleProvider
围绕遗留安全框架。我需要将连接字符串和文件路径注入提供程序,以便我可以将这些提供给传统框架,但是当我使用AuthorizeAttribute
时,我意识到我不知道如何拦截提供程序的构造以便注入这些值。如何使用Windsor将依赖注入到自定义RoleProvider中?
如果它有助于包括代码,我的角色提供有这种构造的:
public class CustomRoleProvider : RoleProvider
{
public CustomRoleProvider(string connectionString, string logPath)
{
LegacySecurity.ConnectionString = connectionString;
LegacySecurity.LogPath = logPath;
}
[Method overrides go here...]
}
我AppSettingsConvention
看起来是这样的:
public class AppSettingsConvention : ISubDependencyResolver
{
public bool CanResolve(CreationContext context,
ISubDependencyResolver contextHandlerResolver,
ComponentModel model,
DependencyModel dependency)
{
return ConfigurationManager.AppSettings.AllKeys.Contains(dependency.DependencyKey)
&& TypeDescriptor.GetConverter(dependency.TargetType).CanConvertFrom(typeof (string));
}
public object Resolve(CreationContext context,
ISubDependencyResolver contextHandlerResolver,
ComponentModel model,
DependencyModel dependency)
{
return TypeDescriptor.GetConverter(dependency.TargetType)
.ConvertFrom(ConfigurationManager.AppSettings[dependency.DependencyKey]);
}
}
(从这里原来:http://blog.ploeh.dk/2012/07/02/PrimitiveDependencies/)
我希望我可以以某种方式替换其中一项服务,就像我将HttpControllerActivator
替换为使用依赖项注入一样与ApiController
s。
这可能吗?还是我需要看看提供这些依赖关系的另一种方式?
看看这个相关的问题:https://stackoverflow.com/questions/15410575/custom-membership-provider-and-unity-injection – Steven
对不起,我真的不知道这是如何帮助我直接。它给了我实现我自己的'IDependencyResolver'的想法,这可能工作,我猜? – adhocgeek
问题是,你不能在你的'CustomRoleProvider'上进行依赖注入(就像你无法使用成员提供者一样),因为它是基于某些XML配置创建该类型的.NET框架。所以诀窍是创建一个你用XML配置的外观,它解析了容器中的'真实'提供者。这基本上是相关问题描述的内容。 – Steven