Is threre a way to use custom activator for OWIN middlewares?

Viewed 38

I would like to control the way how OWIN creates registered middlewares. For example when using following code to register MyCustomMiddleware:

app.Use<MyCustomMiddleware>();

I want to be the one who calls MyCustomMiddleware constructor and pass all needed arguments. As a motivation please imagine for example following scenarios:

  • MyCustomMiddleware constructor requires some arguments that should be created based on current request,
  • Arguments should be resolved from from DI container,
  • I would like to control lifetime of MyCustomMiddleware.

I have found out some comments on how to use OWIN with SimpleInjector or Ninject. However I was not able to find anything independent on particular DI library.

1 Answers
app.Use((c, next) =>
{
    IMiddleware middleware = new MyCustomMiddleware(...);
    return middleware.InvokeAsync(c, _ => next());
});
Related