Unity DI in AuthorisationFilter WebApi - c#

I have used this link here as a reference;
https://unity.codeplex.com/discussions/446780
So as per the link I have added a UnityActionFilterProvider class;
public class UnityActionFilterProvider : ActionDescriptorFilterProvider, IFilterProvider
{
private readonly IUnityContainer container;
public UnityActionFilterProvider(IUnityContainer container)
{
this.container = container;
}
public new IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
{
var filters = base.GetFilters(configuration, actionDescriptor);
foreach (var filter in filters)
{
container.BuildUp(filter.Instance.GetType(), filter.Instance);
}
return filters;
}
}
and I then have added to my UnityConfig.cs
public static void RegisterFilterProviders(IUnityContainer UnityDependencyResolver)
{
var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
GlobalConfiguration.Configuration.Services.Add(
typeof(IFilterProvider),
new UnityActionFilterProvider(UnityDependencyResolver));
var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);
GlobalConfiguration.Configuration.Services.Remove(typeof(IFilterProvider), defaultprovider);
}
which I then call in my Startup.cs
public void Configuration(IAppBuilder app)
{
HttpConfiguration = new HttpConfiguration();
LoggingConfig.RegisterLogger();
ConfigureOAuth(app);
var unityContainer = UnityConfig.GetConfiguredContainer();
HttpConfiguration.DependencyResolver = new UnityDependencyResolver(unityContainer);
//for DI in the filters
UnityConfig.RegisterFilterProviders(unityContainer);
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(HttpConfiguration);
app.UseCors(CorsOptions.AllowAll);
app.UseWebApi(HttpConfiguration);
}
Finally, I have the following filter (Note: I have also tried this as an ActionFilter to see if this made a difference)
public class HasPermissionAttribute : AuthorizationFilterAttribute
{
[Dependency]
public UserPermissionService UserService { get; set; }
public override void OnAuthorization(HttpActionContext actionContext)
{
}
}
However, the UserService is null when this is being hit? Any ideas on what I am doing wrong here please?

Hopefully this will help someone, someday.
Basically it was all down to the fact I was using OAuth with the API.
Therefore I simply needed to ensure the configuration was passed across with the filters by editing the startup.cs as follows;
HttpConfiguration = new HttpConfiguration();
LoggingConfig.RegisterLogger();
ConfigureOAuth(app);
var unityContainer = UnityConfig.GetConfiguredContainer();
HttpConfiguration.DependencyResolver = new UnityDependencyResolver(unityContainer);
//for DI in the filters
UnityConfig.RegisterFilterProviders(unityContainer, HttpConfiguration);
where the registration was simply then ammended with;
public static void RegisterFilterProviders(IUnityContainer UnityDependencyResolver, HttpConfiguration configuration)
{
var providers = configuration.Services.GetFilterProviders().ToList();
configuration.Services.Add(
typeof(IFilterProvider),
new UnityActionFilterProvider(UnityDependencyResolver));
var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);
configuration.Services.Remove(typeof(IFilterProvider), defaultprovider);
}

Related

Property Injection does not work when using RegisterApiControllers extension method

Consider my simple controller class where I want to use a logger (ILogger is coming from Castle in this case).
[RoutePrefix("api/orders")]
public class SignalController : ApiController
{
public ILogger Logger { get; set; } = new NullLogger();
// POST api/orders/update
[HttpPost, Route("update")]
public virtual void UpdateHandler(ChangeStateDto update)
{
this.Logger.Info($"Received ChangeStateDto with status {update.Status}");
}
}
Then I've got a self-hosted webhost, that looks like this.
public class WebHost
{
private readonly string url;
private IDisposable disposable;
private readonly ILifetimeScope scope;
public WebHost(string url, ILifetimeScope scope)
{
this.url = url;
this.scope = scope;
}
public ILogger Logger { get; set; } = new NullLogger();
// ...
public void Start()
{
try
{
this.Logger.Info($"Starting web host at {url}");
this.disposable = WebApp.Start(this.url, app =>
{
var config = new HttpConfiguration
{
DependencyResolver = new AutofacWebApiDependencyResolver(scope)
};
config.MapHttpAttributeRoutes();
app.UseWebApi(config);
});
// ...
}
catch (Exception e)
{
// ...
this.Logger.Error(e.Message, e);
}
}
}
And this is the calling type
public class SomeCallerClass
{
public SomeCallerClass()
{
var webHostLogger = new SomeILoggerImplementation(this, "WebHost");
var builder = new ContainerBuilder();
builder.RegisterApiControllers()
.WithProperty("Logger", webHostLogger);
var container = builder.Build();
this.webHost = new WebHost("http://localhost:9000", container)
{
Logger = webHostLogger
};
}
}
Now the issue I am having is that property injection is not working for my SignalController type. It always holds a reference to the NullLogger instance. I just can't figure out why.
I solved it by using .RegisterType<> instead of .RegisterApiControllers
So the following works as expected. However I still don't understand why my first approach wasn't working.
var builder = new ContainerBuilder();
builder.RegisterType<SignalController>()
.WithProperty("Logger", webHostLogger)
.InstancePerRequest();
var container = builder.Build();
It probably wasn't working because it wasn't registering any controllers.
In your code you have:
builder
.RegisterApiControllers()
.WithProperty("Logger", webHostLogger);
However, you have to tell the registration extension which assemblies your controllers are in. You can see that in the examples shown in the docs.
Try:
builder
.RegisterApiControllers(Assembly.GetExecutingAssembly())
.WithProperty("Logger", webHostLogger);

How to Inject dependencies in a Unity IoC container for SignalR hub?

I have an application that is written with c# on the top of the ASP.NET MVC 5 Framework. I implemented Unity.Mvc into my project. Now, I am trying to inject dependencies objects into my SignalR Hub.
I created a class called UnityHubActivator
My class looks like this
public class UnityHubActivator : IHubActivator
{
private readonly IUnityContainer _container;
public UnityHubActivator(IUnityContainer container)
{
_container = container;
}
public IHub Create(HubDescriptor descriptor)
{
return (IHub)_container.Resolve(descriptor.HubType);
}
}
Then in my UnityConfig class I added the following to my RegisterTypes method
var unityHubActivator = new UnityHubActivator(container);
container.RegisterInstance<IHubActivator>(unityHubActivator);
My hub looks like this
[Authorize]
public class ChatHub : Hub
{
protected IUnitOfWork UnitOfWork { get; set; }
public ChatHub(IUnitOfWork unitOfWork)
: base()
{
UnitOfWork = unitOfWork;
}
}
But when I run the hub, the constructor never get called and the connection never takes place.
How can I correctly use Unity framework to inject dependencies into my hub?
UPDATED
I tried to created a custom container like so
public class UnitySignalRDependencyResolver: DefaultDependencyResolver
{
protected IUnityContainer Container;
private bool IsDisposed = false;
public UnitySignalRDependencyResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
Container = container.CreateChildContainer();
}
public override object GetService(Type serviceType)
{
if (Container.IsRegistered(serviceType))
{
return Container.Resolve(serviceType);
}
return base.GetService(serviceType);
}
public override IEnumerable<object> GetServices(Type serviceType)
{
if (Container.IsRegistered(serviceType))
{
return Container.ResolveAll(serviceType);
}
return base.GetServices(serviceType);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if(IsDisposed)
{
return;
}
if(disposing)
{
Container.Dispose();
}
IsDisposed = true;
}
}
Then here is how I configured the hub in the Startup class
public class Startup
{
public IUnityContainer Container { get; set; }
public Startup(IUnityContainer container)
{
Container = container;
}
public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
var resolver = new UnitySignalRDependencyResolver(Container);
var hubConfiguration = new HubConfiguration
{
Resolver = resolver
};
map.RunSignalR(hubConfiguration);
});
}
}
But still now working... the hub constructor never get called.
Here is how I am calling my hub from the client
<script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
var app = $.connection.chatHub;
console.log('Getting things ready....');
app.client.outOfTasks = function () {
console.log('Nothing to do here')
};
app.client.logError = function (message) {
console.log(message)
};
app.client.logNote = function (message) {
console.log(message)
};
app.client.assignTask = function (taskId) {
app.server.taskReceived();
console.log('task received!!!' + taskId);
};
// Start the connection.
$.connection.hub.start().done(function () {
console.log('Connection Started....');
});
});
</script>
The UnitySignalRDependencyResolver appears accurate for that container.
Taken from the official documentation,
Dependency Injection in SignalR
try the following example for configuring Startup
public class Startup{
public void Configuration(IAppBuilder app) {
IUnityContainer container = GetContainer();
var resolver = new UnitySignalRDependencyResolver(container);
var config = new HubConfiguration {
Resolver = resolver
};
app.MapSignalR("/signalr", config);
}
IUnityContainer GetContainer() {
//...call the unity config related code.
var container = UnityConfig.Container;
//...any other code to populate container.
return container;
}
}
Make sure to register the necessary objects, including the hub (ChatHub) with the container as the container needs to know the object graph in order to resolve the necessary dependencies.

Self-hosted SignalR2 multi-layer dependency injection

I've just implemented DI using Ninject for my self-hosted SignalR2 project.
public class NinjectSignalRDependencyResolver : DefaultDependencyResolver
{
private readonly IKernel _kernel;
public NinjectSignalRDependencyResolver(IKernel kernel)
{
_kernel = kernel;
}
public override object GetService(Type serviceType)
{
return _kernel.TryGet(serviceType) ?? base.GetService(serviceType);
}
public override IEnumerable<object> GetServices(Type serviceType)
{
return _kernel.GetAll(serviceType).Concat(base.GetServices(serviceType));
}
}
And my Startup class:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
var kernel = new StandardKernel();
var resolver = new NinjectSignalRDependencyResolver(kernel);
kernel.Bind<MyDbContext>().ToSelf();
kernel.Bind<IRealTimeDataEngine>().To<RealTimeDataEngine>().InSingletonScope();
kernel.Bind<IHistoricalDataEngine>().To<HistoricalDataEngine>().InSingletonScope();
kernel.Bind(typeof(IHubConnectionContext<dynamic>)).ToMethod(context =>
resolver.Resolve<IConnectionManager>().GetHubContext<MyHub>().Clients
).WhenInjectedInto<IRealTimeDataEngine>();
var config = new HubConfiguration {Resolver = resolver};
ConfigureSignalR(app, config);
}
public static void ConfigureSignalR(IAppBuilder app, HubConfiguration config)
{
app.MapSignalR(config);
}
}
In my signalr hub constructor I expect an IRealTimeDataEngine.
public MyHub(IRealTimeDataEngine realTimeDataEngine, IHistoricalDataEngine historicalDataEngine)
In my host, which is a console application, I need the same IRealTimeDataEngine to be injected.
public DummyProvider(IRealTimeDataEngine realTimeDataEngine)
In my Main method I need to create a DummyProvider object.
If I'm not mistaken, creating a new kernel would not give me the same object in the two different projects, so how should I ask for the same IRealTimeDataEngine at my composition root?
You're right you must use only one kernel per application. That means you should create your kernel outside of Startup class in your case. This can be achieved by using overloaded call of WebApp.Start method like:
class Program
{
static void Main(string[] args)
{
var kernel = new StandardKernel();
var server = WebApp.Start("http://localhost:8080/", (appBuilder) =>
{
var resolver = new NinjectSignalRDependencyResolver(kernel);
var config = new HubConfiguration {Resolver = resolver};
...
});
...

Ninject: property injection into ActionFilterAttribute

I want to implement DI inside ActionFilterAttribute by Ninject. I've found this manual (actually I've read tens of them today) but it's for Unity. My attempt to implement it on Ninject is here:
public class MyFilter : ActionFilterAttribute
{
[Inject]
public IDepend Depend { get; set; }
// another code
}
bindings:
public class NinjectResolver : IDependencyResolver
{
// another code
private IKernel AddRequestBindings(IKernel kernel)
{
// another code
kernel.Bind<IDepend>().To<Depend>();
return kernel;
}
}
custom FilterProvider:
public class WebApiNinjectActionFilterProvider : ActionDescriptorFilterProvider, IFilterProvider
{
private readonly IKernel _kernel;
private readonly IEnumerable<IFilterProvider> _filterProviders;
public WebApiNinjectActionFilterProvider(IKernel kernel, IEnumerable<IFilterProvider> filterProviders)
{
_kernel = kernel;
_filterProviders = filterProviders;
}
public new IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
{
var filters = _filterProviders.SelectMany(fp => fp.GetFilters(configuration, actionDescriptor)).ToList();
foreach (var filter in filters)
{
// GET ActivationException HERE!!!!!
_kernel.Inject(filter.Instance);
}
return filters;
}
public static void RegisterFilterProviders(IKernel ninjectKernel)
{
var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
GlobalConfiguration.Configuration.Services.Add(typeof(IFilterProvider),
new WebApiNinjectActionFilterProvider(ninjectKernel, providers));
var defaultprovider = providers.First(i => i is ActionDescriptorFilterProvider);
GlobalConfiguration.Configuration.Services.Remove(typeof(IFilterProvider), defaultprovider);
}
}
is called from WebApiConfig:
public static void Register(HttpConfiguration config)
{
var ninjectKernel = new StandardKernel();
config.DependencyResolver = new NinjectResolver(ninjectKernel);
WebApiNinjectActionFilterProvider.RegisterFilterProviders(ninjectKernel);
}
and finally action:
[MyFilter]
public async Task<string> Test()
{
return "Hello world";
}
The problem is I get an ActivationException in this line:
_kernel.Inject(filter.Instance);
ActivationException's Activation path:
2) Injection of dependency IDepend into property Depend of type MyFilter
1) Request for MyFilter
If to inject same property to controller - it injects fine. Any ideas how to improve my code?
I was doing IActionFilter DI in wrong way. The right way I've found in this blogpost of Mark Seemann.
DI via Ninject was implemented this way:
var myFilter = new MyFilter(ninjectKernel.Get<IDepend>());
config.Filters.Add(myFilter);

Self-Registering Libraries with Autofac 4 and vNext

i'd like to create a Plugin Enviroment for my ASP.Net 5.0 / MVC 6 Application. I'm using Autofac as IOC Container and i like to load the Plugins (Class Libraries) from the build in DNX LibraryManager. The goal of using the Library Manager is, that i don't have to care about NuGet Packages and Frameworks.
The Problem i have is the LifeCycle, i have to build the IOC Container before the instance of the LibraryManager is available. Because the Autofac Container provides his own IServiceProvider Instance which i have to inject within the ConfigureService() Method call (AddAutofac).
Does anyone know how to get this working?
Update: I have fixed my problem with Davids help and updated the code to get it working with the release candidates. Also i have added support for configuration.
In my DNX Class Library i implemented a Class for Self-Registration:
public class AutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new SimpleService())
.As<IService>()
.InstancePerLifetimeScope();
}
}
In my MVC WebApplication i have added the Class Library as Dependency.
Startup.cs
public IConfiguration Configuration { get; set; }
public class Startup
{
public Startup( IApplicationEnvironment applicationEnvironment )
{
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.SetBasePath( applicationEnvironment.ApplicationBasePath );
configurationBuilder.AddJsonFile( "appsettings.json" );
configurationBuilder.AddJsonFile( "autofac.json" );
configurationBuilder.AddEnvironmentVariables();
this.Configuration = configurationBuilder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDependencies();
}
public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment hostingEnvironment)
{
applicationBuilder.UseDependencies( this.Configuration );
applicationBuilder.UseStaticFiles();
applicationBuilder.UseMvc();
}
}
I have created an DependencyResolver to keep the ContainerBuilder instance.
DependencyResolver.cs
public class DependencyResolver : IDependencyResolver
{
private IContainer container;
private readonly ContainerBuilder builder;
public DependencyResolver()
{
this.builder = new ContainerBuilder();
}
public void RegisterModule( IModule module )
{
this.builder.RegisterModule( module );
}
public void RegisterModules( IEnumerable<Assembly> assemblies )
{
this.builder.RegisterAssemblyModules(assemblies.ToArray());
}
public void Populate( IServiceCollection services)
{
this.builder.Populate( services );
}
public void Build()
{
this.container = this.builder.Build();
}
public T Resolve<T>() where T : class
{
return this.container?.Resolve<T>();
}
}
IDependencyResolver.cs
public interface IDependencyResolver
{
void RegisterModule( IModule module );
void RegisterModules( IEnumerable<Assembly> assemblies );
void Populate(IServiceCollection services);
void Build();
T Resolve<T>() where T : class;
}
Last but not least i have created an Extension Class
DependencyResolverExtensions.cs
public static class DependencyResolverExtensions
{
public static IServiceCollection AddDependencies( this IServiceCollection services )
{
DependencyResolver dependencyResolver = new DependencyResolver();
dependencyResolver.Populate(services);
ServiceDescriptor serviceDescriptor = new ServiceDescriptor(typeof ( IDependencyResolver ), dependencyResolver );
services.TryAdd(serviceDescriptor);
return services;
}
public static IApplicationBuilder UseDependencies(this IApplicationBuilder applicationBuilder, IConfiguration configuration)
{
IDependencyResolver dependencyResolver = applicationBuilder.GetService<IDependencyResolver>();
if (dependencyResolver == null) return applicationBuilder;
ILibraryManager libraryManager = applicationBuilder.GetService<ILibraryManager>();
if (libraryManager == null) return applicationBuilder;
IEnumerable<Assembly> assemblies = libraryManager.GetLoadableAssemblies();
dependencyResolver.RegisterModules(assemblies);
ConfigurationModule configurationModule = new ConfigurationModule( configuration );
dependencyResolver.RegisterModule( configurationModule );
dependencyResolver.Build();
IServiceProvider serviceProvider = dependencyResolver.Resolve<IServiceProvider>();
applicationBuilder.ApplicationServices = serviceProvider;
return applicationBuilder;
}
public static IEnumerable<Assembly> GetLoadableAssemblies(this ILibraryManager libraryManager)
{
List<Assembly> result = new List<Assembly>();
IEnumerable<Library> libraries = libraryManager.GetLibraries();
IEnumerable<AssemblyName> assemblyNames = libraries.SelectMany(e => e.Assemblies).Distinct();
assemblyNames = Enumerable.Where(assemblyNames, e => e.Name.StartsWith("MyLib."));
foreach (AssemblyName assemblyName in assemblyNames)
{
Assembly assembly = Assembly.Load(assemblyName);
result.Add(assembly);
}
return result;
}
public static T GetService<T>(this IApplicationBuilder applicationBuilder) where T : class
{
return applicationBuilder.ApplicationServices.GetService(typeof (T)) as T;
}
}
If you need to switch between different implementations, like mock and real data you can use the Autofac Configuration.
autofac.json
{
"components": [
{
"type": "MyLib.Data.EF.EntitiesData, MyLib.Data.EF",
"services": [
{
"type": "MyLib.Abstractions.IDataRepository, MyLib.Abstractions"
}
]
}
]
}
It's a shame that ConfigureServices is not injectable, that would make this a lot easier.
Looking at the code you should be safe to replace the IServiceProvider inside Configure(...) instead of inside ConfigureServices(...) and get the intended behavior. ApplicationServices is setable.
In your UseAutofac method you should be able to do something like:
public static IApplicationBuilder UseAutofac( [NotNull] this IApplicationBuilder applicationBuilder )
{
IAutofacResolver autofacResolver = applicationBuilder.GetService<IAutofacResolver>();
ILibraryManager libraryManager = applicationBuilder.GetService<ILibraryManager>();
autofacResolver.RegisterLibraryModules( libraryManager);
applicationBuilder.ApplicationServices = autofacResolver.Resolve();
return applicationBuilder;
}
I've come up with a solution that uses part of this, but also uses a ComponentContainer that addresses the potential memory leaks in the DependencyResolver. This also works with RC1. Not sure yet about RC2 as it's not complete enough for me to test.
The ComponentContainer looks like this:
public static class ComponentContainer {
static IContainer _container;
static ContainerBuilder _containerBuilder;
public static ContainerBuilder Builder {
get {
if (_containerBuilder == null)
_containerBuilder = new ContainerBuilder();
return _containerBuilder;
}
}
public static IServiceProvider ServiceProvider {
get {
if (_container == null)
_container = _containerBuilder.Build();
return _container.Resolve<IServiceProvider>();
}
}
public static ComponentFactory<TObject> Component<TObject>() => new ComponentFactory<TObject>(_container);
public static void RegisterAssembly(Assembly assembly) {
if (assembly == null) return;
foreach (var obj in assembly.GetTypes().Where(t => t.GetCustomAttribute<ExportAttribute>() != null)) {
ExportAttribute att = obj.GetCustomAttribute<ExportAttribute>();
if (att.ContractType != null) {
_containerBuilder.RegisterType(obj).As(att.ContractType);
} else {
foreach (var intf in obj.GetInterfaces())
_containerBuilder.RegisterType(obj).As(intf);
}
}
}
}
public class ComponentFactory<TObject> : IDisposable {
protected TObject CurrentObject;
protected ILifetimeScope CurrentScope;
public TObject Current => (TObject)CurrentObject;
public ComponentFactory(IContainer container) {
CurrentScope = container.BeginLifetimeScope();
CurrentObject = CurrentScope.Resolve<TObject>();
}
public TObject Component => CurrentObject;
public void Dispose() {
(CurrentObject as IDisposable)?.Dispose();
CurrentScope.Dispose();
}
}
Then in Startup.cs I do the following:
public virtual IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddMvc();
services.AddOptions();
services.AddSession();
services.AddCaching();
var assemblyLoadContextAccessor = services.FirstOrDefault(s => s.ServiceType == typeof(IAssemblyLoadContextAccessor)).ImplementationInstance as IAssemblyLoadContextAccessor;
var libraryManager = services.FirstOrDefault(s => s.ServiceType == typeof(ILibraryManager)).ImplementationInstance as ILibraryManager;
var loadContext = assemblyLoadContextAccessor.Default;
foreach(var library in libraryManager.GetLibraries()) {
var assembly = loadContext.Load(library.Name);
if(assembly != null) {
var module = assembly.GetTypes().FirstOrDefault(t => t == typeof(IModule));
if(module != null)
ComponentContainer.Builder.RegisterAssemblyModules(assembly);
else
ComponentContainer.RegisterAssembly(assembly);
}
}
ComponentContainer.Builder.Populate(services);
return ComponentContainer.ServiceProvider;
}
To export modules within an assembly, I either mark them with an ExportAttribute or add a class to the assembly that implements Autofac's IModule. The code in ConfigureServices will enumerate through the application's modules and feed them to the static Builder in ComponentContainer. Once the container has been built, you can either resolve modules through injection in a constructor or you can request a specific type by:
(using var myComponentFactory = ComponentContainer.Component<IMyModule>()) {
//You can now access your component through myComponentFactory.Component
//Once it passes out of scope of using, it will be properly disposed of
//along with the scope from which it was created.
}
Edit: With the release of RC2, this code is no longer valid as the enumeration of assemblies and classes will fail. I haven't come up with a good solution yet. If anyone else has any suggestions for enumerating assemblies in RC2, please let me know.

Categories