Windsor: UseMiddlewareFromWindsor cannot pass arguments into the middleware

This issue is related to https://github.com/fir3pho3nixx/Windsor/tree/aspnet-core-windsor-final branch

UseMiddlewareFromWindsor extension method (https://github.com/fir3pho3nixx/Windsor/blob/fa3617bf19e4f53430e018d543ababa00388e005/src/Castle.Facilities.AspNetCore/WindsorRegistrationExtensions.cs#L90) cannot pass arguments into the middleware.

I had to come up with a modified version with a new parameter argumentsAsAnonymousType:

public static void UseMiddlewareFromWindsor<T>(this IApplicationBuilder app, IWindsorContainer container, object argumentsAsAnonymousType)
	where T : class, IMiddleware
{
	container.Register(Component.For<T>());
	app.Use(async (context, next) =>
	{
		var resolve = container.Resolve<T>(argumentsAsAnonymousType);
		try
		{
			await resolve.InvokeAsync(context, async (ctx) => await next());
		}
		finally
		{
			container.Release(resolve);
		}
	});
}

Example of usage:

app.UseMiddlewareFromWindsor<TransactionScopeUnitOfWorkMiddleware>(_windsorContainer, new { isolationLevel = IsolationLevel.ReadCommitted });

In this example it passes read committed isolation level into the TransactionScopeUnitOfWorkMiddleware.

Should I create a pull request? Or do you think this can be added by you? Cheers.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 26 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Awesome. We did not notice any memory spikes either. Closing for now. Please raise new issues when you find them.

@xhafan Can you please take a look at the latest cut? You also should not need to cross wire anything.

I am going to drop this into production tomorrow. If I find anything I will let you know.

@xhafan check 😃

@xhafan I am saying that a factory is resolvable from the container. And a factory can create uow using the container, without exposing the container.

So you ctor injects a dbfactory this can create a session/context and transactions for you. This is one of the awsom things about Castle. The type factory reimplementation:

https://github.com/castleproject/Windsor/blob/master/docs/typed-factory-facility-interface-based.md

Or look at https://github.com/generik0/Smooth.IoC.Dapper.Repository.UnitOfWork

@generik0, ok I think I get it. So the point is to inject the unit of work factory into my middleware, and use it to create unit of work instances, instead of using the container instance directly. Right?