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

Commit 06da302

Browse files
committed
Register IApplicationDiscriminator service
1 parent 86ead9f commit 06da302

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNetCore.DataProtection.Infrastructure;
6+
7+
namespace Microsoft.AspNetCore.Hosting.Internal
8+
{
9+
public class ApplicationDiscriminator : IApplicationDiscriminator
10+
{
11+
private readonly IHostingEnvironment _hostingEnvironment;
12+
13+
public ApplicationDiscriminator(IHostingEnvironment hostingEnv)
14+
{
15+
if (hostingEnv == null)
16+
{
17+
throw new ArgumentNullException(nameof(hostingEnv));
18+
}
19+
20+
_hostingEnvironment = hostingEnv;
21+
}
22+
23+
public string Discriminator => _hostingEnvironment.ContentRootPath;
24+
}
25+
}

src/Microsoft.AspNetCore.Hosting/Microsoft.AspNetCore.Hosting.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20+
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="$(AspNetCoreVersion)" />
2021
<PackageReference Include="Microsoft.AspNetCore.Http" Version="$(AspNetCoreVersion)" />
2122
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="$(AspNetCoreVersion)" />
2223
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(AspNetCoreVersion)" />

src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.IO;
88
using System.Reflection;
99
using System.Runtime.ExceptionServices;
10+
using Microsoft.AspNetCore.DataProtection.Infrastructure;
1011
using Microsoft.AspNetCore.Hosting.Builder;
1112
using Microsoft.AspNetCore.Hosting.Internal;
1213
using Microsoft.AspNetCore.Http;
@@ -303,6 +304,7 @@ private IServiceCollection BuildCommonServices(out AggregateException hostingSta
303304
var services = new ServiceCollection();
304305
services.AddSingleton(_hostingEnvironment);
305306
services.AddSingleton(_context);
307+
services.AddTransient<IApplicationDiscriminator, ApplicationDiscriminator>();
306308

307309
var builder = new ConfigurationBuilder()
308310
.SetBasePath(_hostingEnvironment.ContentRootPath)

test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading;
1010
using System.Threading.Tasks;
1111
using Microsoft.AspNetCore.Builder;
12+
using Microsoft.AspNetCore.DataProtection.Infrastructure;
1213
using Microsoft.AspNetCore.Hosting;
1314
using Microsoft.AspNetCore.Hosting.Fakes;
1415
using Microsoft.AspNetCore.Hosting.Internal;
@@ -987,6 +988,19 @@ public void Build_ThrowsIfUnloadableAssemblyNameInHostingStartupAssemblies()
987988
Assert.IsType<FileNotFoundException>(ex.InnerExceptions[0].InnerException);
988989
}
989990

991+
[Fact]
992+
public void Build_SetsAppDescriminatorFromContentRoot()
993+
{
994+
var builder = CreateWebHostBuilder()
995+
.UseContentRoot(Environment.CurrentDirectory)
996+
.Configure(app => { })
997+
.UseServer(new TestServer());
998+
999+
var host = builder.Build();
1000+
var applicationDiscriminator = host.Services.GetRequiredService<IApplicationDiscriminator>();
1001+
Assert.Equal(Environment.CurrentDirectory, applicationDiscriminator.Discriminator);
1002+
}
1003+
9901004
[Fact]
9911005
public async Task Build_DoesNotThrowIfUnloadableAssemblyNameInHostingStartupAssembliesAndCaptureStartupErrorsTrue()
9921006
{

0 commit comments

Comments
 (0)