Design review: Always instantiate via DI

This commit is contained in:
SteveSandersonMS
2016-09-01 15:51:53 +01:00
parent 61fd900974
commit f04fb8c421
9 changed files with 97 additions and 91 deletions

View File

@@ -3,6 +3,7 @@ using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.NodeServices;
using Microsoft.Extensions.DependencyInjection;
namespace ConsoleApplication
{
@@ -12,7 +13,17 @@ namespace ConsoleApplication
public class Program
{
public static void Main(string[] args) {
using (var nodeServices = CreateNodeServices(NodeServicesOptions.DefaultNodeHostingModel)) {
// Set up the DI system
var services = new ServiceCollection();
services.AddNodeServices(new NodeServicesOptions {
HostingModel = NodeServicesOptions.DefaultNodeHostingModel,
ProjectPath = Directory.GetCurrentDirectory(),
WatchFileExtensions = new string[] {} // Don't watch anything
});
var serviceProvider = services.BuildServiceProvider();
// Now instantiate an INodeServices and use it
using (var nodeServices = serviceProvider.GetRequiredService<INodeServices>()) {
MeasureLatency(nodeServices).Wait();
}
}
@@ -34,13 +45,5 @@ namespace ConsoleApplication
Console.WriteLine("\nTotal time: {0:F2} milliseconds", 1000 * elapsedSeconds);
Console.WriteLine("\nTime per invocation: {0:F2} milliseconds", 1000 * elapsedSeconds / requestCount);
}
private static INodeServices CreateNodeServices(NodeHostingModel hostingModel) {
return Configuration.CreateNodeServices(new NodeServicesOptions {
HostingModel = hostingModel,
ProjectPath = Directory.GetCurrentDirectory(),
WatchFileExtensions = new string[] {} // Don't watch anything
});
}
}
}

View File

@@ -8,7 +8,8 @@
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.NodeServices": "1.0.0-*"
"Microsoft.AspNetCore.NodeServices": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection": "1.0.0"
},
"frameworks": {
"netcoreapp1.0": {

View File

@@ -10,7 +10,7 @@ module.exports = merge({
},
module: {
loaders: [
{ test: /\.ts(x?)$/, exclude: /node_modules/, loader: 'ts-loader' }
{ test: /\.ts(x?)$/, exclude: /node_modules/, loader: 'ts-loader?silent' }
],
},
entry: {