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

Register IApplicationDiscriminator service #1064

Merged
merged 1 commit into from
May 6, 2017
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
@@ -0,0 +1,25 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.DataProtection.Infrastructure;

namespace Microsoft.AspNetCore.Hosting.Internal
{
public class ApplicationDiscriminator : IApplicationDiscriminator
{
private readonly IHostingEnvironment _hostingEnvironment;

public ApplicationDiscriminator(IHostingEnvironment hostingEnvironment)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}

_hostingEnvironment = hostingEnvironment;
}

public string Discriminator => _hostingEnvironment.ContentRootPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(AspNetCoreVersion)" />
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Reflection;
using System.Runtime.ExceptionServices;
using Microsoft.AspNetCore.DataProtection.Infrastructure;
using Microsoft.AspNetCore.Hosting.Builder;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -303,6 +304,7 @@ private IServiceCollection BuildCommonServices(out AggregateException hostingSta
var services = new ServiceCollection();
services.AddSingleton(_hostingEnvironment);
services.AddSingleton(_context);
services.AddTransient<IApplicationDiscriminator, ApplicationDiscriminator>();

var builder = new ConfigurationBuilder()
.SetBasePath(_hostingEnvironment.ContentRootPath)
Expand Down
14 changes: 14 additions & 0 deletions test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection.Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Fakes;
using Microsoft.AspNetCore.Hosting.Internal;
Expand Down Expand Up @@ -987,6 +988,19 @@ public void Build_ThrowsIfUnloadableAssemblyNameInHostingStartupAssemblies()
Assert.IsType<FileNotFoundException>(ex.InnerExceptions[0].InnerException);
}

[Fact]
public void Build_SetsAppDescriminatorFromContentRoot()
{
var builder = CreateWebHostBuilder()
.UseContentRoot(Environment.CurrentDirectory)
.Configure(app => { })
.UseServer(new TestServer());

var host = builder.Build();
var applicationDiscriminator = host.Services.GetRequiredService<IApplicationDiscriminator>();
Assert.Equal(Environment.CurrentDirectory, applicationDiscriminator.Discriminator);
}

[Fact]
public async Task Build_DoesNotThrowIfUnloadableAssemblyNameInHostingStartupAssembliesAndCaptureStartupErrorsTrue()
{
Expand Down