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 8a66871
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 29 deletions.
12 changes: 12 additions & 0 deletions src/Microsoft.AspNet.Hosting/HostingEnvironment.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
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(Path.Combine(applicationBasePath, "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; }
}
}
26 changes: 13 additions & 13 deletions src/Microsoft.AspNet.Hosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -47,33 +49,31 @@ public void Main(string[] args)
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);
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
29 changes: 17 additions & 12 deletions src/Microsoft.AspNet.TestHost/TestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,20 @@ 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;
private bool _disposed = false;

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,
Expand All @@ -51,14 +48,22 @@ public static TestServer Create(Action<IApplicationBuilder> app)

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 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
17 changes: 17 additions & 0 deletions test/Microsoft.AspNet.Hosting.Tests/HostingUtilitiesTests.cs
Original file line number Diff line number Diff line change
@@ -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"));
}
}
}
3 changes: 2 additions & 1 deletion test/Microsoft.AspNet.Hosting.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
},
"commands": {
"test": "Xunit.KRunner"
}
},
"webroot" : "testroot"
}
12 changes: 12 additions & 0 deletions test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +56,16 @@ public async Task CreateInvokesApp()
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()
{
Expand Down
3 changes: 2 additions & 1 deletion test/Microsoft.AspNet.TestHost.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"System.Runtime": ""
}
}
}
},
"webroot" : "testroot"
}

0 comments on commit 8a66871

Please sign in to comment.