Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 7c8b37b

Browse files
committed
Do not add IHttpContextAccessor to DI by default
1 parent 2f2ca23 commit 7c8b37b

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

src/Microsoft.AspNet.Hosting/WebHostBuilder.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ private IServiceCollection BuildHostingServices()
7575
services.AddTransient<IServerLoader, ServerLoader>();
7676
services.AddTransient<IApplicationBuilderFactory, ApplicationBuilderFactory>();
7777
services.AddTransient<IHttpContextFactory, HttpContextFactory>();
78-
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
7978
services.AddLogging();
8079

8180
var diagnosticSource = new DiagnosticListener("Microsoft.AspNet");

test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.AspNet.Http;
1414
using Microsoft.AspNet.Http.Features;
1515
using Microsoft.AspNet.Http.Features.Internal;
16+
using Microsoft.AspNet.Http.Internal;
1617
using Microsoft.AspNet.Testing.xunit;
1718
using Microsoft.Extensions.Configuration;
1819
using Microsoft.Extensions.DependencyInjection;
@@ -289,14 +290,18 @@ public async Task CanAccessLogger()
289290
[Fact]
290291
public async Task CanAccessHttpContext()
291292
{
293+
Action<IServiceCollection> configureServices = services =>
294+
{
295+
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
296+
};
292297
TestServer server = TestServer.Create(app =>
293298
{
294299
app.Run(context =>
295300
{
296301
var accessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
297302
return context.Response.WriteAsync("HasContext:"+(accessor.HttpContext != null));
298303
});
299-
});
304+
}, configureServices);
300305

301306
string result = await server.CreateClient().GetStringAsync("/path");
302307
Assert.Equal("HasContext:True", result);
@@ -315,15 +320,19 @@ public ContextHolder(IHttpContextAccessor accessor)
315320
[Fact]
316321
public async Task CanAddNewHostServices()
317322
{
323+
Action<IServiceCollection> configureServices = services =>
324+
{
325+
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
326+
services.AddSingleton<ContextHolder>();
327+
};
318328
TestServer server = TestServer.Create(app =>
319329
{
320330
app.Run(context =>
321331
{
322332
var accessor = app.ApplicationServices.GetRequiredService<ContextHolder>();
323333
return context.Response.WriteAsync("HasContext:" + (accessor.Accessor.HttpContext != null));
324334
});
325-
},
326-
services => services.AddSingleton<ContextHolder>());
335+
}, configureServices);
327336

328337
string result = await server.CreateClient().GetStringAsync("/path");
329338
Assert.Equal("HasContext:True", result);

0 commit comments

Comments
 (0)