Comments (5)
I am also having the same problem.
from autofac.webapi.
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.
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:
- Parses the inbound multipart message into separate request messages.
- Uses an
HttpMessageInvoker
to run each of the request messages. (This invoker is basically an in-memory wrapper around the current Web API configuration.) - 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.
from autofac.webapi.
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)
- Dependency Issue on latest version on nuget HOT 2
- Bad async/await implementation causing loss of execution context. HOT 9
- Help - Autofac.WebApi2 Global Action FIlters Parameters injection failed HOT 1
- Please, consider to release the 4.2.0 version of the Nuget package HOT 2
- Could not load file or assembly 'Autofac, Version=3.0.0.0' when using Autofac.WebApi 3.1.0 HOT 3
- What should I do tp set the value of ControllerContext property after GetService method? HOT 3
- Cannot get Autofac to work with Web API HOT 2
- Performance: Use TryGetValue instead of ContainsKey() + indexer sequence HOT 3
- Registration of filters on base classes no longer work HOT 3
- Test Failures on Autofac 4.0.1 or Later HOT 8
- Autofac filters for specific 'async' action in a controller not working HOT 1
- Injecting a registered named service is not working as expected. HOT 1
- Autofac 5 was pushed to Github a few days ago along with MVC 5 but the current version of WebApi2 is not usable with it HOT 1
- Compatibility with Autofac v6 HOT 1
- Web requests never return when using Autofac, unless pre-resolved HOT 2
- HttpRequestMessage is disposed after the filters are resolved HOT 7
- Override Exception Filters executed last instead of first HOT 3
- Filters at controller scope not ran when action scoped filters exist. HOT 3
- Change CallContext to AsyncLocal HOT 5
- Self-hosted WebApi Custom Authorization Filter/Attribute using IAutofacAuthorizationFilter HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autofac.webapi.