Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
var dependencyScope = new AutofacWebApiDependencyScope(lifetimeScope);
request.Properties[HttpPropertyKeys.DependencyScope] = dependencyScope;

return base.SendAsync(request, cancellationToken);
try
{
return base.SendAsync(request, cancellationToken);
}
finally
{
request.Properties.Remove(HttpPropertyKeys.DependencyScope);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,48 @@ public async void AddsAutofacDependencyScopeToHttpRequestMessage()
var container = new ContainerBuilder().Build();
context.Set(Constants.OwinLifetimeScopeKey, container);

var fakeHandler = new FakeInnerHandler { Message = new HttpResponseMessage(HttpStatusCode.OK) };
AutofacWebApiDependencyScope scope = null;
var fakeHandler = new FakeInnerHandler(r =>
{
scope = (AutofacWebApiDependencyScope)r.Properties[HttpPropertyKeys.DependencyScope];
return new HttpResponseMessage(HttpStatusCode.OK);
});
var handler = new DependencyScopeHandler { InnerHandler = fakeHandler };
var invoker = new HttpMessageInvoker(handler);
await invoker.SendAsync(request, new CancellationToken());

var scope = (AutofacWebApiDependencyScope)request.Properties[HttpPropertyKeys.DependencyScope];

Assert.NotNull(scope); //checking if the handler was in fact called
Assert.Equal(container, scope.LifetimeScope);
}

[Fact]
public async void RemoveAutofacDependencyScopeFromHttpRequestMessageAfterLeaving()
{
var request = new HttpRequestMessage();
var context = new OwinContext();
request.Properties.Add("MS_OwinContext", context);

var container = new ContainerBuilder().Build();
context.Set(Constants.OwinLifetimeScopeKey, container);

var fakeHandler = new FakeInnerHandler(_ => new HttpResponseMessage(HttpStatusCode.OK));
var handler = new DependencyScopeHandler { InnerHandler = fakeHandler };
var invoker = new HttpMessageInvoker(handler);
await invoker.SendAsync(request, new CancellationToken());

Assert.DoesNotContain(HttpPropertyKeys.DependencyScope, request.Properties);
}

}

public class FakeInnerHandler : DelegatingHandler
public class FakeInnerHandler : HttpMessageHandler
{
public HttpResponseMessage Message { get; set; }
private readonly Func<HttpRequestMessage, HttpResponseMessage> _delegate;

public FakeInnerHandler(Func<HttpRequestMessage, HttpResponseMessage> @delegate)
=> _delegate = @delegate;

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return Message == null ? base.SendAsync(request, cancellationToken) : Task.Run(() => Message, cancellationToken);
}
=> Task.FromResult(_delegate(request));
}
}
}