Use Ninject DI inside my own OWIN middleware
Asked Answered
N

2

6

I've made a simple piece of OWIN middleware that will get me a User object and add it to HttpContext.Current.Items so that's available for all controllers and views for each request.

Here's my code:

public class SetCurrentUserMiddleware : OwinMiddleware
{
    public SetCurrentUserMiddleware(OwinMiddleware next) : base(next)
    {
    }

    public override Task Invoke(IOwinContext context)
    {
        if (context.Request.User.Identity.IsAuthenticated)
        {
            // Do some work to get a userId... (omitted)
            var repo = new UserRepository();
            User user = repo.Get(userId);
            HttpContext.Current.Items["CurrentUserContext"] = user;
        }

        return Next.Invoke(context);
    }
}

I'm using Ninject in my web application - how can I refactor this middleware so that my UserRepository is injected as a dependency? Is this possible at all?

Necessity answered 21/5, 2014 at 7:43 Comment(2)
Have you chosen any implementation. I too have this questionFrizz
I hope that I am not doing a stupid question, but have you tried to bind it inside your IoC/DI container? Bind<IContextManager>().To<ContextManager>();Muckrake
S
0

According to this page, you can just supply your own constructor arguments.

public class SetCurrentUserMiddleware : OwinMiddleware
{
    private readonly IUserRepository userRepository;

    public SetCurrentUserMiddleware(OwinMiddleware next, IUserRepository userRepository) : base(next)
    {
        if (userRepository == null)
            throw new ArgumentNullException("userRepository");
        this.userRepository = userRepository;
    }

    public override Task Invoke(IOwinContext context)
    {
        if (context.Request.User.Identity.IsAuthenticated)
        {
            // Do some work to get a userId... (omitted)
            User user = this.userRepository.Get(userId);
            HttpContext.Current.Items["CurrentUserContext"] = user;
        }

        return Next.Invoke(context);
    }
}
Spam answered 29/11, 2014 at 18:11 Comment(2)
Make sure you use the Ninject Owin packages as shown here github.com/ninject/Ninject.Web.Common/wiki/…Spermary
When I do this I get an exception on the line where I want to register the middleware (app.use<MyMiddleware>()) Additional information: The class 'InCube.DigitalAdvisory.WebApi.LoggingMiddleware' does not have a constructor taking 1 arguments.Prolong
V
0

A little late to the party but just trying to help if anyone else stumbles on this.

I assume you are registering your custom middleware in Startup.cs. Something similar to app.Use<SetCurrentUserMiddleware>();.

Noted that the app.Use<T>() accepts extra parameter as args[], which means you can do

//example using AutoFac
app.Use<SetCurrentUserMiddleware>(container.Resolve<IUserRepository>());

the extra params will be provided as any additional constructor parameters to your middleware when it's being constructed.

Vanhouten answered 3/4, 2020 at 1:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.