Code Monkey home page Code Monkey logo

Comments (4)

alistairjevans avatar alistairjevans commented on July 19, 2024

The Implementations property returns the set of implementations of a service in the same order as v4; the reversal you link to in core Autofac was actually just a re-implementation of a reversal which existed in v4 of Autofac.

Looking in to the actual implementation of how the set of middleware is determined indicates that the library depends on the order of the Registrations property, not Implementations:

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 IEnumerable support. Prior to v5, the Registrations property was a List<T> of component registrations, hence a dependable order. In v5, the backing type for that set was changed to a ConcurrentBag to reduce locking, but the order of that set is then basically "undefined". Even stating "Reverse the order of your registrations" isn't perfect advice, because they could come out in any order.

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 IEnumerable<T>.

The simplest fix here may be to apply an OrderBy line that retrieves the ServiceRegistration details, and through that the RegistrationOrder value to sort by. That does give a predictable order at least.

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 Service or something. But for Owin this may be sufficient.

from autofac.owin.

tillig avatar tillig commented on July 19, 2024

I think the OrderBy line is probably a "good enough" fix. I'm not super interested in spending a lot of time here given it appears no one even noticed this for almost two years (the length of time it's been since we updated Autofac internals to support .NET core ordering, around v5).

from autofac.owin.

tillig avatar tillig commented on July 19, 2024

Forgot about this one. Sigh. I'll add a PR for this after #30 goes through.

from autofac.owin.

alistairjevans avatar alistairjevans commented on July 19, 2024

I'll put this in, just approved #30, so will merge that and apply this fix.

from autofac.owin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.