|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Linq; |
4 | 4 | using System.Threading; |
| 5 | +using System.Threading.Tasks; |
5 | 6 | using Autofac.Core.Lifetime; |
6 | 7 | using Autofac.Core.Registration; |
7 | 8 | using Autofac.Features.ResolveAnything; |
@@ -328,6 +329,51 @@ public void UseMiddlewareFromContainerRequiresInjectorRegistrationFirst() |
328 | 329 | Assert.Throws<InvalidOperationException>(() => app.Object.UseMiddlewareFromContainer<TestMiddleware>()); |
329 | 330 | } |
330 | 331 |
|
| 332 | + [Fact] |
| 333 | + public async void UseAutofacLifetimeScopeInjectorWithContainerRegistersOwinContextInTheScope() |
| 334 | + { |
| 335 | + using (var server = TestServer.Create(app => |
| 336 | + { |
| 337 | + app.UseAutofacLifetimeScopeInjector(new ContainerBuilder().Build()); |
| 338 | + app.Run(context => |
| 339 | + { |
| 340 | + //we can't directly compare contexts because they are recreated at each step in UseHandlerMidleware |
| 341 | + Assert.Same( |
| 342 | + context.Environment, |
| 343 | + context |
| 344 | + .GetAutofacLifetimeScope() |
| 345 | + .Resolve<IOwinContext>() |
| 346 | + .Environment |
| 347 | + ); |
| 348 | + return Task.FromResult(0); |
| 349 | + }); |
| 350 | + })) |
| 351 | + { |
| 352 | + await server.HttpClient.GetAsync("/"); |
| 353 | + } |
| 354 | + } |
| 355 | + |
| 356 | + [Fact] |
| 357 | + public async void UseAutofacLifetimeScopeInjectorWithExternalScopeDoesNotRegisterOwinContextInTheScope() |
| 358 | + { |
| 359 | + using (var server = TestServer.Create(app => |
| 360 | + { |
| 361 | + app.UseAutofacLifetimeScopeInjector(context => new ContainerBuilder().Build()); |
| 362 | + app.Run(context => |
| 363 | + { |
| 364 | + Assert.Null( |
| 365 | + context |
| 366 | + .GetAutofacLifetimeScope() |
| 367 | + .ResolveOptional<IOwinContext>() |
| 368 | + ); |
| 369 | + return Task.FromResult(0); |
| 370 | + }); |
| 371 | + })) |
| 372 | + { |
| 373 | + await server.HttpClient.GetAsync("/"); |
| 374 | + } |
| 375 | + } |
| 376 | + |
331 | 377 | class TestableLifetimeScope : LifetimeScope |
332 | 378 | { |
333 | 379 | public bool ScopeIsDisposed { get; set; } |
|
0 commit comments