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

Commit

Permalink
#71 - Create IHostingEnvironment.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Sep 11, 2014
1 parent 6445562 commit d5e3bc5
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 14 deletions.
16 changes: 16 additions & 0 deletions src/Microsoft.AspNet.Hosting/HostingEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 HostingEnvironment()
{
}

public string EnvironmentName { get; set; }

public string WebRoot { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -33,5 +35,20 @@ internal static Tuple<string, string> SplitTypeName(string identifier)
}
return new Tuple<string, string>(typeName, assemblyName);
}

public static string GetWebRoot(string applicationBasePath)
{
try
{
var config = new Configuration();
config.AddJsonFile("project.json");
var webroot = config.Get("webroot") ?? string.Empty;
return Path.GetFullPath(Path.Combine(applicationBasePath, webroot));
}
catch (Exception)
{
return applicationBasePath;
}
}
}
}
15 changes: 15 additions & 0 deletions src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
15 changes: 11 additions & 4 deletions src/Microsoft.AspNet.Hosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,27 @@ public void Main(string[] args)
config.AddEnvironmentVariables();
config.AddCommandLine(args);

var appEnv = _serviceProvider.GetService<IApplicationEnvironment>();

var hostingEnv = new HostingEnvironment()
{
EnvironmentName = config.Get("KRE_ENV") ?? "Development",
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>();
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Hosting/Server/ServerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public IServerFactory GetServerFactory(string serverFactoryIdentifier)
throw new ArgumentException(string.Empty, "serverFactoryIdentifier");
}

var nameParts = Utilities.SplitTypeName(serverFactoryIdentifier);
var nameParts = HostingUtilities.SplitTypeName(serverFactoryIdentifier);
string typeName = nameParts.Item1;
string assemblyName = nameParts.Item2;

Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.AspNet.Hosting/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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-*"
Expand Down
28 changes: 20 additions & 8 deletions src/Microsoft.AspNet.TestHost/TestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public class TestServer : IServerFactory, IDisposable

public TestServer(IConfiguration config, IServiceProvider serviceProvider, Action<IApplicationBuilder> appStartup)
{
var env = serviceProvider.GetService<IApplicationEnvironment>();
if (env == null)
var appEnv = serviceProvider.GetService<IApplicationEnvironment>();
if (appEnv == null)
{
throw new ArgumentException("IApplicationEnvironment couldn't be resolved.", "serviceProvider");
}

HostingContext hostContext = new HostingContext()
{
ApplicationName = env.ApplicationName,
ApplicationName = appEnv.ApplicationName,
Configuration = config,
ServerFactory = this,
Services = serviceProvider,
Expand All @@ -51,14 +51,26 @@ public static TestServer Create(Action<IApplicationBuilder> app)

public static TestServer Create(IServiceProvider provider, Action<IApplicationBuilder> app)
{
var appEnv = provider.GetService<IApplicationEnvironment>();
if (appEnv == null)
{
throw new ArgumentException("IApplicationEnvironment couldn't be resolved.", "serviceProvider");
}

var hostingEnv = new HostingEnvironment()
{
EnvironmentName = "Development",
WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath),
};

var collection = new ServiceCollection();
var hostingServices = HostingServices.GetDefaultServices();
collection.Add(HostingServices.GetDefaultServices());
collection.AddInstance<IHostingEnvironment>(hostingEnv);

var config = new Configuration();
collection.Add(hostingServices);
var appServices = collection.BuildServiceProvider(provider);

var serviceProvider = collection.BuildServiceProvider(provider);
return new TestServer(config, serviceProvider, app);
var config = new Configuration();
return new TestServer(config, appServices, app);
}

public HttpMessageHandler CreateHandler()
Expand Down

0 comments on commit d5e3bc5

Please sign in to comment.