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

@@ -33,10 +33,9 @@ namespace Microsoft.AspNetCore.SpaServices.Prerendering
// in your startup file, but then again it might be confusing that you don't need to.
if (_nodeServices == null)
{
_nodeServices = _fallbackNodeServices = Configuration.CreateNodeServices(new NodeServicesOptions
{
ProjectPath = _applicationBasePath
}.AddDefaultEnvironmentVariables(hostEnv.IsDevelopment()));
_nodeServices = _fallbackNodeServices = Configuration.CreateNodeServices(
serviceProvider,
new NodeServicesOptions());
}
}

View File

@@ -35,19 +35,17 @@ namespace Microsoft.AspNetCore.Builder
"To enable ReactHotModuleReplacement, you must also enable HotModuleReplacement.");
}
var hostEnv = (IHostingEnvironment)appBuilder.ApplicationServices.GetService(typeof(IHostingEnvironment));
var projectPath = options.ProjectPath ?? hostEnv.ContentRootPath;
// Unlike other consumers of NodeServices, WebpackDevMiddleware dosen't share Node instances, nor does it
// use your DI configuration. It's important for WebpackDevMiddleware to have its own private Node instance
// because it must *not* restart when files change (if it did, you'd lose all the benefits of Webpack
// middleware). And since this is a dev-time-only feature, it doesn't matter if the default transport isn't
// as fast as some theoretical future alternative.
var nodeServices = Configuration.CreateNodeServices(new NodeServicesOptions
{
ProjectPath = projectPath,
WatchFileExtensions = new string[] { } // Don't watch anything
}.AddDefaultEnvironmentVariables(hostEnv.IsDevelopment()));
var nodeServices = Configuration.CreateNodeServices(
appBuilder.ApplicationServices,
new NodeServicesOptions
{
WatchFileExtensions = new string[] { } // Don't watch anything
});
// Get a filename matching the middleware Node script
var script = EmbeddedResourceReader.Read(typeof(WebpackDevMiddleware),
@@ -55,6 +53,8 @@ namespace Microsoft.AspNetCore.Builder
var nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
// Tell Node to start the server hosting webpack-dev-middleware
var hostEnv = (IHostingEnvironment)appBuilder.ApplicationServices.GetService(typeof(IHostingEnvironment));
var projectPath = options.ProjectPath ?? hostEnv.ContentRootPath;
var devServerOptions = new
{
webpackConfigPath = Path.Combine(projectPath, options.ConfigFile ?? DefaultConfigFile),