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

Commit

Permalink
Reacting to Hosting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkm committed Mar 15, 2016
1 parent 1ae1cdb commit bf1fc7d
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.PlatformAbstractions;

Expand All @@ -23,8 +24,8 @@ public class DefaultAssemblyProvider : IAssemblyProvider
/// <summary>
/// Initializes a new instance of <see cref="DefaultAssemblyProvider"/>.
/// </summary>
/// <param name="environment">The <see cref="IApplicationEnvironment"/>.</param>
public DefaultAssemblyProvider(IApplicationEnvironment environment)
/// <param name="environment">The <see cref="IHostingEnvironment"/>.</param>
public DefaultAssemblyProvider(IHostingEnvironment environment)
: this(
Assembly.Load(new AssemblyName(environment.ApplicationName)),
DependencyContext.Load(Assembly.Load(new AssemblyName(environment.ApplicationName))))
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.AspNetCore.Mvc.Core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"version": "1.0.0-*"
},
"Microsoft.Extensions.FileProviders.Abstractions": "1.0.0-*",
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-*",
"Microsoft.Extensions.DependencyModel": "1.0.0-*",
"Microsoft.Extensions.ClosedGenericMatcher.Sources": {
"version": "1.0.0-*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
using System.Runtime.Loader;
#endif
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Dnx.Compilation.CSharp;
using Microsoft.Extensions.CompilationAbstractions;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;

namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{
Expand All @@ -32,7 +32,7 @@ public class DnxRoslynCompilationService : ICompilationService
private readonly ConcurrentDictionary<string, AssemblyMetadata> _metadataFileCache =
new ConcurrentDictionary<string, AssemblyMetadata>(StringComparer.OrdinalIgnoreCase);

private readonly IApplicationEnvironment _environment;
private readonly IHostingEnvironment _environment;
private readonly ILibraryExporter _libraryExporter;
private readonly RazorViewEngineOptions _options;
private readonly Lazy<List<MetadataReference>> _applicationReferences;
Expand All @@ -48,7 +48,7 @@ public class DnxRoslynCompilationService : ICompilationService
/// <param name="optionsAccessor">Accessor to <see cref="RazorViewEngineOptions"/>.</param>
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
public DnxRoslynCompilationService(
IApplicationEnvironment environment,
IHostingEnvironment environment,
ILibraryExporter libraryExporter,
IOptions<RazorViewEngineOptions> optionsAccessor,
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.AspNetCore.Mvc.Localization/ViewLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.PlatformAbstractions;

namespace Microsoft.AspNetCore.Mvc.Localization
{
Expand All @@ -28,20 +28,20 @@ public class ViewLocalizer : IViewLocalizer, IViewContextAware
/// Creates a new <see cref="ViewLocalizer"/>.
/// </summary>
/// <param name="localizerFactory">The <see cref="IHtmlLocalizerFactory"/>.</param>
/// <param name="applicationEnvironment">The <see cref="IApplicationEnvironment"/>.</param>
public ViewLocalizer(IHtmlLocalizerFactory localizerFactory, IApplicationEnvironment applicationEnvironment)
/// <param name="hostingEnvironment">The <see cref="IHostingEnvironment"/>.</param>
public ViewLocalizer(IHtmlLocalizerFactory localizerFactory, IHostingEnvironment hostingEnvironment)
{
if (localizerFactory == null)
{
throw new ArgumentNullException(nameof(localizerFactory));
}

if (applicationEnvironment == null)
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(applicationEnvironment));
throw new ArgumentNullException(nameof(hostingEnvironment));
}

_applicationName = applicationEnvironment.ApplicationName;
_applicationName = hostingEnvironment.ApplicationName;
_localizerFactory = localizerFactory;
}

Expand Down
7 changes: 1 addition & 6 deletions src/Microsoft.AspNetCore.Mvc.Razor.Host/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,9 @@
}
},
"frameworks": {
"net451": {
"dependencies": {
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-*"
}
},
"net451": { },
"netstandard1.3": {
"dependencies": {
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-*",
"System.Collections.Concurrent": "4.0.12-*",
"System.ComponentModel.TypeConverter": "4.0.1-*",
"System.Reflection.Extensions": "4.0.1-*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#endif
using System.Text;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand All @@ -23,7 +24,6 @@
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;

namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{
Expand All @@ -50,12 +50,12 @@ public class DefaultRoslynCompilationService : ICompilationService
/// <summary>
/// Initalizes a new instance of the <see cref="DefaultRoslynCompilationService"/> class.
/// </summary>
/// <param name="environment">The environment for the executing application.</param>
/// <param name="environment">The <see cref="IHostingEnvironment"/>.</param>
/// <param name="optionsAccessor">Accessor to <see cref="RazorViewEngineOptions"/>.</param>
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public DefaultRoslynCompilationService(
IApplicationEnvironment environment,
IHostingEnvironment environment,
IOptions<RazorViewEngineOptions> optionsAccessor,
IRazorViewEngineFileProviderAccessor fileProviderAccessor,
ILoggerFactory loggerFactory)
Expand Down Expand Up @@ -326,7 +326,7 @@ private static DiagnosticMessage GetDiagnosticMessage(Diagnostic diagnostic)
mappedLineSpan.EndLinePosition.Character + 1);
}

private static DependencyContext GetDependencyContext(IApplicationEnvironment environment)
private static DependencyContext GetDependencyContext(IHostingEnvironment environment)
{
if (environment.ApplicationName != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;

namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{
Expand All @@ -22,14 +22,14 @@ public class DependencyContextRazorViewEngineOptionsSetup : ConfigureOptions<Raz
/// <summary>
/// Initializes a new instance of <see cref="DependencyContextRazorViewEngineOptionsSetup"/>.
/// </summary>
public DependencyContextRazorViewEngineOptionsSetup(IApplicationEnvironment applicationEnvironment)
: base(options => ConfigureRazor(options, applicationEnvironment))
public DependencyContextRazorViewEngineOptionsSetup(IHostingEnvironment hostingEnvironment)
: base(options => ConfigureRazor(options, hostingEnvironment))
{
}

private static void ConfigureRazor(RazorViewEngineOptions options, IApplicationEnvironment applicationEnvironment)
private static void ConfigureRazor(RazorViewEngineOptions options, IHostingEnvironment hostingEnvironment)
{
var applicationAssembly = Assembly.Load(new AssemblyName(applicationEnvironment.ApplicationName));
var applicationAssembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));
var dependencyContext = DependencyContext.Load(applicationAssembly);
var compilationOptions = dependencyContext?.CompilationOptions ?? Extensions.DependencyModel.CompilationOptions.Default;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;

namespace Microsoft.AspNetCore.Mvc
{
Expand All @@ -20,21 +17,21 @@ public class RazorViewEngineOptionsSetup : ConfigureOptions<RazorViewEngineOptio
/// <summary>
/// Initializes a new instance of <see cref="RazorViewEngineOptions"/>.
/// </summary>
/// <param name="applicationEnvironment"><see cref="IApplicationEnvironment"/> for the application.</param>
/// <param name="hostingEnvironment"><see cref="IHostingEnvironment"/> for the application.</param>
public RazorViewEngineOptionsSetup(
IApplicationEnvironment applicationEnvironment,
IHostingEnvironment hostingEnvironment)
: base(options => ConfigureRazor(options, applicationEnvironment, hostingEnvironment))
: base(options => ConfigureRazor(options, hostingEnvironment))
{
}

private static void ConfigureRazor(
RazorViewEngineOptions razorOptions,
IApplicationEnvironment applicationEnvironment,
IHostingEnvironment hostingEnvironment)
{
razorOptions.FileProviders.Add(new PhysicalFileProvider(applicationEnvironment.ApplicationBasePath));
if (hostingEnvironment.ContentRootFileProvider != null)
{
razorOptions.FileProviders.Add(hostingEnvironment.ContentRootFileProvider);
}

var compilationOptions = razorOptions.CompilationOptions;
string configurationSymbol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
{
public class MvcEncodedTestFixture<TStartup> : MvcTestFixture<TStartup>
{
protected override void InitializeServices(IServiceCollection services, string relativePath)
protected override void InitializeServices(IServiceCollection services)
{
base.InitializeServices(services, relativePath);
base.InitializeServices(services);
services.AddTransient<HtmlEncoder, HtmlTestEncoder>();
services.AddTransient<JavaScriptEncoder, JavaScriptTestEncoder>();
services.AddTransient<UrlEncoder, UrlTestEncoder>();
Expand Down
38 changes: 17 additions & 21 deletions test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ protected MvcTestFixture(string relativePath)
using (new CultureReplacer())
{
var builder = new WebHostBuilder()
.ConfigureServices(serviceCollection => InitializeServices(serviceCollection, relativePath))
.UseContentRoot(GetApplicationPath(relativePath))
.ConfigureServices(InitializeServices)
.UseStartup(typeof(TStartup));

_server = new TestServer(builder);
Expand All @@ -54,35 +55,30 @@ public void Dispose()
_server.Dispose();
}

protected virtual void InitializeServices(IServiceCollection services, string relativePath)
private static string GetApplicationPath(string relativePath)
{
// When an application executes in a regular context, the application base path points to the root
// directory where the application is located, for example .../samples/MvcSample.Web. However, when
// executing an application as part of a test, the ApplicationBasePath of the IApplicationEnvironment
// points to the root folder of the test project.
// To compensate, we need to calculate the correct project path and override the application
// environment value so that components like the view engine work properly in the context of the test.
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
var applicationName = startupAssembly.GetName().Name;

var applicationEnvironment = PlatformServices.Default.Application;
#if DNX451
services.AddSingleton(CompilationServices.Default.LibraryExporter);
services.AddSingleton<ICompilationService, DnxRoslynCompilationService>();

var libraryManager = DnxPlatformServices.Default.LibraryManager;
var library = libraryManager.GetLibrary(applicationName);
var applicationRoot = Path.GetDirectoryName(library.Path);
return Path.GetDirectoryName(library.Path);
#else
var applicationRoot = Path.GetFullPath(Path.Combine(
applicationEnvironment.ApplicationBasePath,
relativePath,
applicationName
));
var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath;
return Path.GetFullPath(Path.Combine(applicationBasePath, relativePath, applicationName));
#endif
}

services.AddSingleton<IApplicationEnvironment>(
new TestApplicationEnvironment(applicationEnvironment, applicationName, applicationRoot));
protected virtual void InitializeServices(IServiceCollection services)
{
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
var applicationName = startupAssembly.GetName().Name;

var applicationEnvironment = PlatformServices.Default.Application;
#if DNX451
services.AddSingleton(CompilationServices.Default.LibraryExporter);
services.AddSingleton<ICompilationService, DnxRoslynCompilationService>();
#endif

// Inject a custom assembly provider. Overrides AddMvc() because that uses TryAdd().
var assemblyProvider = new StaticAssemblyProvider();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Globalization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.PlatformAbstractions;
Expand Down Expand Up @@ -39,9 +40,6 @@ public void HtmlLocalizerOfTTest_UseIndexer_ReturnsLocalizedHtmlString()
public void HtmlLocalizerOfTTest_UseIndexerWithArguments_ReturnsLocalizedHtmlString()
{
// Arrange
var applicationEnvironment = new Mock<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");

var localizedString = new LocalizedHtmlString("Hello", "Bonjour test");

var htmlLocalizer = new Mock<IHtmlLocalizer>();
Expand Down
Loading

0 comments on commit bf1fc7d

Please sign in to comment.