InstancePerRequest does not work as expected with http batch requests
kevinsbennett opened this issue · 5 comments
After implementing http batch handling (with non sequential execution) in web api like described here my dependencies setup with InstancePerApiRequest do not act as expected.
Each of the sub-requests of the batch will get the same instance of the class setup with InstancePerApiRequest, which is problematic when dealing with database transactions, because the sub-requests come in on different threads at the same time.
Is there any way to fix this or work around it?
Thanks in advance for any help!
I am also having the same problem.
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.
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.
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));