-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Middleware registration order is inverted during resolve #27
Comments
The Looking in to the actual implementation of how the set of middleware is determined indicates that the library depends on the order of the internal static IEnumerable<Type> GenerateAllAutofacMiddleware(IComponentContext container)
{
return container.ComponentRegistry.Registrations.SelectMany(r => r.Services)
.OfType<TypedService>()
.Where(s => IsMiddlewareButNotAutofac(s.ServiceType))
.Select(service => typeof(AutofacMiddleware<>).MakeGenericType(service.ServiceType))
.ToArray();
} It doesn't even attempt to use our Other code across the Autofac extensions expressly avoids depending on this order, preferring instead the predictable and documented order of components registered for a given service, when resolving via The simplest fix here may be to apply an internal static IEnumerable<Type> GenerateAllAutofacMiddleware(IComponentContext container)
{
return container.ComponentRegistry.Registrations.SelectMany(r => r.Services)
.OfType<TypedService>()
.Where(s => IsMiddlewareButNotAutofac(s.ServiceType))
.OrderBy(s => container.ComponentRegistry.TryGetServiceRegistration(s, out var reg) ? reg.GetRegistrationOrder() : 0)
.Select(service => typeof(AutofacMiddleware<>).MakeGenericType(service.ServiceType))
.ToArray();
} It's not a great fix, and if this was a more 'current' integration library, I'd look to do a cleaner fix by switching to registering middleware differently under a single |
I think the |
Forgot about this one. Sigh. I'll add a PR for this after #30 goes through. |
I'll put this in, just approved #30, so will merge that and apply this fix. |
Describe the Bug
If you look at the OWIN self hosting example you can see the expected output to respect the registration order of the middleware. However, when you execute the example, the middleware is executed in the opposite order:
(Issue based on this StackOverflow question.)
Steps to Reproduce
Examples
repo.cd src/WebApiExample.OwinSelfHost
dotnet run
Expected Behavior
As documented, the middleware should execute in the order registered.
Dependency Versions
Additional Info
The logic for resolving the set of middleware items looks like it's been stable for six years. However, I recall we had to change some internals in Autofac to handle resolving
IEnumerable<T>
in registration order and it involved doing things like this which reverse the order in which registrations are returned when enumerating them manually.I'm guessing that due to that reversal in core Autofac, we also need to reverse the order in which the manual enumeration of registered middleware is handled. We probably should also add a unit test to make sure we don't regress this.
The text was updated successfully, but these errors were encountered: