#71 - Create IHostingEnvironment.
This commit is contained in:
parent
64455620a0
commit
8a66871139
|
|
@ -0,0 +1,12 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
namespace Microsoft.AspNet.Hosting
|
||||
{
|
||||
public class HostingEnvironment : IHostingEnvironment
|
||||
{
|
||||
public string EnvironmentName { get; set; }
|
||||
|
||||
public string WebRoot { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -16,10 +16,12 @@
|
|||
// permissions and limitations under the License.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting
|
||||
{
|
||||
internal static class Utilities
|
||||
public static class HostingUtilities
|
||||
{
|
||||
internal static Tuple<string, string> SplitTypeName(string identifier)
|
||||
{
|
||||
|
|
@ -33,5 +35,20 @@ namespace Microsoft.AspNet.Hosting
|
|||
}
|
||||
return new Tuple<string, string>(typeName, assemblyName);
|
||||
}
|
||||
|
||||
public static string GetWebRoot(string applicationBasePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var config = new Configuration();
|
||||
config.AddJsonFile(Path.Combine(applicationBasePath, "project.json"));
|
||||
var webroot = config.Get("webroot") ?? string.Empty;
|
||||
return Path.GetFullPath(Path.Combine(applicationBasePath, webroot));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return applicationBasePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.Framework.Runtime;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting
|
||||
{
|
||||
[AssemblyNeutral]
|
||||
public interface IHostingEnvironment
|
||||
{
|
||||
string EnvironmentName { get; }
|
||||
|
||||
string WebRoot { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -29,6 +29,8 @@ namespace Microsoft.AspNet.Hosting
|
|||
public class Program
|
||||
{
|
||||
private const string HostingIniFile = "Microsoft.AspNet.Hosting.ini";
|
||||
private const string DefaultEnvironmentName = "Development";
|
||||
private const string EnvironmentKey = "KRE_ENV";
|
||||
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
|
|
@ -47,33 +49,31 @@ namespace Microsoft.AspNet.Hosting
|
|||
config.AddEnvironmentVariables();
|
||||
config.AddCommandLine(args);
|
||||
|
||||
var appEnv = _serviceProvider.GetService<IApplicationEnvironment>();
|
||||
|
||||
var hostingEnv = new HostingEnvironment()
|
||||
{
|
||||
EnvironmentName = config.Get(EnvironmentKey) ?? DefaultEnvironmentName,
|
||||
WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath),
|
||||
};
|
||||
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.Add(HostingServices.GetDefaultServices(config));
|
||||
serviceCollection.AddInstance<IHostingEnvironment>(hostingEnv);
|
||||
var services = serviceCollection.BuildServiceProvider(_serviceProvider);
|
||||
|
||||
var appEnvironment = _serviceProvider.GetService<IApplicationEnvironment>();
|
||||
|
||||
var context = new HostingContext()
|
||||
{
|
||||
Services = services,
|
||||
Configuration = config,
|
||||
ServerName = config.Get("server"), // TODO: Key names
|
||||
ApplicationName = config.Get("app") // TODO: Key names
|
||||
?? appEnvironment.ApplicationName,
|
||||
EnvironmentName = config.Get("env") ?? "Development"
|
||||
?? appEnv.ApplicationName,
|
||||
EnvironmentName = hostingEnv.EnvironmentName,
|
||||
};
|
||||
|
||||
var engine = services.GetService<IHostingEngine>();
|
||||
if (engine == null)
|
||||
{
|
||||
throw new Exception("TODO: IHostingEngine service not available exception");
|
||||
}
|
||||
|
||||
var appShutdownService = _serviceProvider.GetService<IApplicationShutdown>();
|
||||
if (appShutdownService == null)
|
||||
{
|
||||
throw new Exception("TODO: IApplicationShutdown service not available");
|
||||
}
|
||||
var shutdownHandle = new ManualResetEvent(false);
|
||||
|
||||
var serverShutdown = engine.Start(context);
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Hosting.Server
|
|||
throw new ArgumentException(string.Empty, "serverFactoryIdentifier");
|
||||
}
|
||||
|
||||
var nameParts = Utilities.SplitTypeName(serverFactoryIdentifier);
|
||||
var nameParts = HostingUtilities.SplitTypeName(serverFactoryIdentifier);
|
||||
string typeName = nameParts.Item1;
|
||||
string assemblyName = nameParts.Item2;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
"Microsoft.AspNet.PipelineCore": "1.0.0-*",
|
||||
"Microsoft.AspNet.Security.DataProtection": "1.0.0-*",
|
||||
"Microsoft.Framework.ConfigurationModel": "1.0.0-*",
|
||||
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*",
|
||||
"Microsoft.Framework.DependencyInjection": "1.0.0-*",
|
||||
"Microsoft.Framework.Logging": "1.0.0-*",
|
||||
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*"
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ namespace Microsoft.AspNet.TestHost
|
|||
{
|
||||
public class TestServer : IServerFactory, IDisposable
|
||||
{
|
||||
private static readonly string ServerName = typeof(TestServer).FullName;
|
||||
private const string DefaultEnvironmentName = "Development";
|
||||
private const string ServerName = nameof(TestServer);
|
||||
private static readonly ServerInformation ServerInfo = new ServerInformation();
|
||||
private Func<object, Task> _appDelegate;
|
||||
private IDisposable _appInstance;
|
||||
|
|
@ -25,15 +26,11 @@ namespace Microsoft.AspNet.TestHost
|
|||
|
||||
public TestServer(IConfiguration config, IServiceProvider serviceProvider, Action<IApplicationBuilder> appStartup)
|
||||
{
|
||||
var env = serviceProvider.GetService<IApplicationEnvironment>();
|
||||
if (env == null)
|
||||
{
|
||||
throw new ArgumentException("IApplicationEnvironment couldn't be resolved.", "serviceProvider");
|
||||
}
|
||||
var appEnv = serviceProvider.GetService<IApplicationEnvironment>();
|
||||
|
||||
HostingContext hostContext = new HostingContext()
|
||||
{
|
||||
ApplicationName = env.ApplicationName,
|
||||
ApplicationName = appEnv.ApplicationName,
|
||||
Configuration = config,
|
||||
ServerFactory = this,
|
||||
Services = serviceProvider,
|
||||
|
|
@ -51,14 +48,22 @@ namespace Microsoft.AspNet.TestHost
|
|||
|
||||
public static TestServer Create(IServiceProvider provider, Action<IApplicationBuilder> app)
|
||||
{
|
||||
var appEnv = provider.GetService<IApplicationEnvironment>();
|
||||
|
||||
var hostingEnv = new HostingEnvironment()
|
||||
{
|
||||
EnvironmentName = DefaultEnvironmentName,
|
||||
WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath),
|
||||
};
|
||||
|
||||
var collection = new ServiceCollection();
|
||||
var hostingServices = HostingServices.GetDefaultServices();
|
||||
collection.Add(HostingServices.GetDefaultServices());
|
||||
collection.AddInstance<IHostingEnvironment>(hostingEnv);
|
||||
|
||||
var appServices = collection.BuildServiceProvider(provider);
|
||||
|
||||
var config = new Configuration();
|
||||
collection.Add(hostingServices);
|
||||
|
||||
var serviceProvider = collection.BuildServiceProvider(provider);
|
||||
return new TestServer(config, serviceProvider, app);
|
||||
return new TestServer(config, appServices, app);
|
||||
}
|
||||
|
||||
public HttpMessageHandler CreateHandler()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Hosting.Tests
|
||||
{
|
||||
public class HostingUtilitiesTests
|
||||
{
|
||||
[Fact]
|
||||
public void ReadWebRootFromProjectJson()
|
||||
{
|
||||
var root = HostingUtilities.GetWebRoot(".");
|
||||
Assert.True(root.EndsWith("testroot"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -13,5 +13,6 @@
|
|||
},
|
||||
"commands": {
|
||||
"test": "Xunit.KRunner"
|
||||
}
|
||||
},
|
||||
"webroot" : "testroot"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
|
|
@ -54,6 +56,16 @@ namespace Microsoft.AspNet.TestHost
|
|||
Assert.Equal("CreateInvokesApp", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebRootCanBeResolvedFromProjectJson()
|
||||
{
|
||||
TestServer server = TestServer.Create(app =>
|
||||
{
|
||||
var env = app.ApplicationServices.GetService<IHostingEnvironment>();
|
||||
Assert.Equal(Path.GetFullPath("testroot"), env.WebRoot);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DisposeStreamIgnored()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -14,5 +14,6 @@
|
|||
"System.Runtime": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"webroot" : "testroot"
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue