Skip to content

Commit

Permalink
Fix issue WebApiContrib#176. Old service provider should be restored …
Browse files Browse the repository at this point in the history
…to the HttpContext after the branch scope is finished.
  • Loading branch information
David Jackman committed May 24, 2019
1 parent cfb6dee commit 6bb1e9c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static IApplicationBuilder UseBranchWithServices(this IApplicationBuilder

branchBuilder.Use(async (context, next) =>
{
var oldServiceProvider = context.RequestServices;
using (var scope = factory.CreateScope())
{
context.RequestServices = scope.ServiceProvider;
Expand All @@ -67,6 +68,7 @@ public static IApplicationBuilder UseBranchWithServices(this IApplicationBuilder

await next();
}
context.RequestServices = oldServiceProvider;
});

appBuilderConfiguration(branchBuilder);
Expand Down
17 changes: 17 additions & 0 deletions tests/WebApiContrib.Core.Tests/ParallelPipelinesStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace WebApiContrib.Core.Tests
{
Expand Down Expand Up @@ -87,6 +88,22 @@ public void Configure(IApplicationBuilder app)
await c.Response.WriteAsync(service.Hi());
});
});

// Add middleware before the next branch to verify state of the context coming out of the branch
app.Use(async (c, next) =>
{
// Call into the branch
await next();

// Verify the service provider is working
var service = c.RequestServices.GetService<ILogger>();
});

app.UseBranchWithServices("/baz", s =>
{
}, a =>
{
});
}
}
}
9 changes: 9 additions & 0 deletions tests/WebApiContrib.Core.Tests/ParallelPipelinesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,14 @@ public async Task CanReachBranchWithMultipleEntryPoints(string path)

Assert.Equal($"hi {path.Substring(0, path.Length-1)}", stringResult);
}

[Fact]
public async Task PipelineBeforeTheBranchStillHasWorkingServiceProvider()
{
var client = _server.CreateClient();

var request = new HttpRequestMessage(HttpMethod.Get, $"/baz");
await client.SendAsync(request);
}
}
}

0 comments on commit 6bb1e9c

Please sign in to comment.