Skip to content

Commit

Permalink
Register NodeRunner as singletone in Dependency Injection
Browse files Browse the repository at this point in the history
  • Loading branch information
kolesnikovav committed Jul 15, 2020
1 parent 26aa062 commit 5155ece
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Change your Startup.cs configuration file as follows:
using AspSpaService;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// ---- Your code -----------//
services.AddNodeRunner();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ---- Your code -----------//
Expand Down
1 change: 1 addition & 0 deletions samples/webapi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Startup
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddNodeRunner();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
2 changes: 1 addition & 1 deletion src/AspSpaService/AspSpaService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PackageId>AspSpaService</PackageId>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Aleksandr Kolesnikov</Authors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>AspSpaService</AssemblyName>
Expand Down
20 changes: 19 additions & 1 deletion src/AspSpaService/AspSpaServiceMiddlewareExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ public static class AspSpaServiceMiddlewareExtensions
{
private const string LogCategoryName = "AspSpaService";
/// <summary>
/// Adds NodeRunner as singletone service and register it in Dependency Injection
/// This dispose node js process when application is shutdown
///
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
public static void AddNodeRunner(this IServiceCollection services)
{
services.AddSingleton(typeof(NodeRunner));
}
private static NodeRunner GetNodeRunner(IApplicationBuilder builder)
{
return (NodeRunner)builder.ApplicationServices.GetService(typeof(NodeRunner));
}
/// <summary>
/// Handles requests by passing them through to an instance of the node dev server.
/// This means you don't need to start node dev server manually.
///
Expand All @@ -40,7 +54,11 @@ public static void UseAspSpaDevelopmentServer(
throw new ArgumentNullException(nameof(spaBuilder));
}
var logger = GetOrCreateLogger(spaBuilder.ApplicationBuilder, LogCategoryName);
NodeRunner runner = new NodeRunner();
NodeRunner runner = GetNodeRunner(spaBuilder.ApplicationBuilder);
if (runner == null)
{
runner = new NodeRunner();
}
runner.Command = command;
runner.Arguments = arguments;
runner.WorkingDirectory = workingDirectory;
Expand Down

0 comments on commit 5155ece

Please sign in to comment.