Giter VIP home page Giter VIP logo

Comments (5)

OnurGumus avatar OnurGumus commented on July 25, 2024

I am also having the same problem.

from autofac.webapi.

tillig avatar tillig commented on July 25, 2024

Sorry, we've been really working hard on .NET Core support. It's been taking pretty much all of our time.

The way batch handling works internally to Web API I don't believe there's any hook into the start of the child request process. It's a single request at the top level that gets munged and handled in memory as smaller requests, but it doesn't run the whole request pipeline IIRC.

If you have suggestions or pointers to the code where we can hook in, we're totally open to that. Beyond that, I don't have any suggestions.

from autofac.webapi.

tillig avatar tillig commented on July 25, 2024

OK, I looked into this and I don't think there's anything we can do about it.

You'll want to look at the source of the DefaultHttpBatchHandler to understand what's going on when you batch. In a nutshell, this handler:

  1. Parses the inbound multipart message into separate request messages.
  2. Uses an HttpMessageInvoker to run each of the request messages. (This invoker is basically an in-memory wrapper around the current Web API configuration.)
  3. Takes the set of responses from running each of those in-memory requests and aggregates them into a multipart response.

In particular, if you look at the source for DefaultHttpBatchHandler.ParseBatchRequestsAsync (the thing that splits the big request into smaller requests) you see that it calls an extension CopyBatchRequestProperties which is defined in this class. That method copies basically everything (with the exception of some route data and a few other things) from the main parent request into each of the child requests.

One of the things it copies is the request lifetime scope. That's not an Autofac-specific thing - Web API controls when that scope is created and where in the request message it's stored.

For that reason, you will see the same request lifetime scope across all of the child batch requests. There is no workaround that I'm aware of and there's nothing we can do from the Autofac side. This is something you'd have to file an issue with the ASP.NET team and ask them to fix.

That said, you may consider - technically during a batch it is just one inbound request to the system. That's how it appears Web API is defining "a request" - actually a literal call from a client to the server. A batch is an interesting way to make fewer calls, but even though it's doing a lot of things, it's still just one logical request. I raise that because if you do try to get the ASP.NET team to fix the issue, there will probably be some pushback and that is likely to be the explanation.

Anyway, the best we can do is document the issue, which I'll do. Sorry.

from autofac.webapi.

tillig avatar tillig commented on July 25, 2024

Updated documentation.

from autofac.webapi.

pratikf avatar pratikf commented on July 25, 2024

I implemented a custom handler to solve this issue. This handler creates scopes with the same Web request tag for each individual request, and monitors the original scope's End event to dispose the newly created individual scopes.

public class CustomHttpBatchHandler : DefaultHttpBatchHandler
{
    public CustomHttpBatchHandler(HttpServer httpServer)
        : base(httpServer)
    {
    }

    private const string ScopeKey = "MS_DependencyScope";

    public override Task<IList<HttpRequestMessage>> ParseBatchRequestsAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var retVal = base.ParseBatchRequestsAsync(request, cancellationToken);
        IDependencyScope originalScope = null;
        var childScopes = new List<ILifetimeScope>();
        foreach (var result in retVal.Result.Where(r => r.Properties.ContainsKey(ScopeKey)))
        {
            originalScope = result.Properties[ScopeKey] as IDependencyScope;
            var scope =
                ContainerManager.Container.BeginLifetimeScope(
                    Autofac.Core.Lifetime.MatchingScopeLifetimeTags.RequestLifetimeScopeTag);
            childScopes.Add(scope);
            result.Properties[ScopeKey] = new AutofacWebApiDependencyScope(scope);
        }
        if (originalScope != null)
        {
            originalScope.GetRequestLifetimeScope().CurrentScopeEnding +=
                (s, e) => childScopes.ForEach(scope => scope.Dispose());
        }
        return retVal;
    }
}

During Web API initialization:
config.Routes.MapHttpBatchRoute("BatchApi", "api/batch", new CustomHttpBatchHandler(GlobalConfiguration.DefaultServer));

from autofac.webapi.

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.