I'm using Windsor with ASP.NET MVC4
and I've written a custom RoleProvider
around a legacy security framework. I need to inject a connection string and file path into the provider so I can provide these to the legacy framework, but when I come to use the AuthorizeAttribute
, I realise that I have no idea how to intercept the construction of the provider in order to inject these values.
If it helps to include code, my role provider has this kind of constructor :
public class CustomRoleProvider : RoleProvider
{
public CustomRoleProvider(string connectionString, string logPath)
{
LegacySecurity.ConnectionString = connectionString;
LegacySecurity.LogPath = logPath;
}
[Method overrides go here...]
}
My AppSettingsConvention
looks like this :
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]);
}
}
(originally from here : http://blog.ploeh.dk/2012/07/02/PrimitiveDependencies/ )
I'm hoping that I can replace one of the services somehow the same way I'm replacing the HttpControllerActivator
to use dependency injection with the ApiController
s.
Is this possible? Or do I need to look at another way of providing these dependencies?