-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Nybus.Extensions.Hosting.TopShelf package (#70)
- TopShelf helper classes in new package - Sample NetFxWindowsService modified
- Loading branch information
Showing
7 changed files
with
510 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 6 additions & 2 deletions
8
samples/InMemory/NetFxWindowsService/NetFxWindowsService.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,72 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Nybus; | ||
using Topshelf; | ||
|
||
namespace NetFxWindowsService | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Hello World!"); | ||
var host = HostFactory.New(configure => | ||
{ | ||
configure.UseStartup<NybusStartup>(); | ||
configure.SetDisplayName("Nybus Windows Service"); | ||
configure.SetServiceName("NybusWindowsService"); | ||
configure.SetDescription("A Windows service running Nybus"); | ||
configure.EnableServiceRecovery(rc => rc.RestartService(TimeSpan.FromMinutes(1)) | ||
.RestartService(TimeSpan.FromMinutes(5)) | ||
.RestartService(TimeSpan.FromMinutes(10)) | ||
.SetResetPeriod(1)); | ||
configure.RunAsLocalService(); | ||
configure.StartAutomaticallyDelayed(); | ||
configure.SetStopTimeout(TimeSpan.FromMinutes(5)); | ||
}); | ||
|
||
host.Run(); | ||
} | ||
} | ||
|
||
public class NybusStartup : Startup | ||
{ | ||
public override IBusHost ConstructService(StartupContext context, IServiceProvider serviceProvider) | ||
{ | ||
var host = serviceProvider.GetRequiredService<IBusHost>(); | ||
|
||
return host; | ||
} | ||
|
||
public override void ConfigureServices(StartupContext context, IServiceCollection services) | ||
{ | ||
services.AddNybus(nybus => | ||
{ | ||
nybus.UseConfiguration(context.Configuration); | ||
nybus.UseInMemoryBusEngine(); | ||
}); | ||
} | ||
|
||
public override void ConfigureAppConfiguration(IConfigurationBuilder configurationBuilder) | ||
{ | ||
configurationBuilder.AddEnvironmentVariables(); | ||
} | ||
|
||
public override void ConfigureLogging(StartupContext context, ILoggingBuilder logging) | ||
{ | ||
logging.AddConfiguration(context.Configuration.GetSection("Logging")); | ||
|
||
logging.AddConsole(); | ||
} | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/extensions/hosting/Nybus.Extensions.TopShelf/HostConfiguratorExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Topshelf; | ||
using Topshelf.HostConfigurators; | ||
using Topshelf.ServiceConfigurators; | ||
|
||
namespace Nybus | ||
{ | ||
public static class HostConfiguratorExtensions | ||
{ | ||
public static HostConfigurator UseStartup<TStartup>(this HostConfigurator configurator) | ||
where TStartup : Startup, new() | ||
{ | ||
return configurator.Service<IBusHost>(UseStartup<TStartup>); | ||
} | ||
|
||
private static void UseStartup<TStartup>(ServiceConfigurator<IBusHost> configurator) | ||
where TStartup : Startup, new() | ||
{ | ||
var startup = new TStartup(); | ||
|
||
configurator.ConstructUsing(settings => | ||
{ | ||
var configurationBuilder = new ConfigurationBuilder(); | ||
startup.ConfigureAppConfiguration(configurationBuilder); | ||
var configuration = configurationBuilder.Build(); | ||
var context = new StartupContext(settings, configuration); | ||
var services = new ServiceCollection(); | ||
services.AddSingleton(configuration); | ||
startup.ConfigureServices(context, services); | ||
services.AddLogging(builder => startup.ConfigureLogging(context, builder)); | ||
var serviceProvider = services.BuildServiceProvider(); | ||
return startup.ConstructService(context, serviceProvider); | ||
}); | ||
|
||
configurator.WhenStarted(startup.OnStart); | ||
|
||
configurator.WhenStopped(startup.OnStop); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/extensions/hosting/Nybus.Extensions.TopShelf/Nybus.Extensions.Hosting.TopShelf.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<RootNamespace>Nybus</RootNamespace> | ||
<AssemblyName>Nybus.Extensions.Hosting.TopShelf</AssemblyName> | ||
<Description>TopShelf helper classes for Nybus</Description> | ||
<PackageTags>dotnet-standard;framework;queue;rx;nybus;topshelf</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="TopShelf" Version="4.2.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Nybus.Abstractions\Nybus.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
47 changes: 47 additions & 0 deletions
47
src/extensions/hosting/Nybus.Extensions.TopShelf/Startup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Topshelf; | ||
using Topshelf.Runtime; | ||
|
||
namespace Nybus | ||
{ | ||
public abstract class Startup | ||
{ | ||
public virtual void ConfigureAppConfiguration(IConfigurationBuilder configurationBuilder) { } | ||
|
||
public virtual void ConfigureServices(StartupContext context, IServiceCollection services) { } | ||
|
||
public virtual void ConfigureLogging(StartupContext context, ILoggingBuilder logging) { } | ||
|
||
public abstract IBusHost ConstructService(StartupContext context, IServiceProvider serviceProvider); | ||
|
||
public virtual bool OnStart(IBusHost host, HostControl control) | ||
{ | ||
host.StartAsync().GetAwaiter().GetResult(); | ||
|
||
return true; | ||
} | ||
|
||
public virtual bool OnStop(IBusHost host, HostControl control) | ||
{ | ||
host.StopAsync().GetAwaiter().GetResult(); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
public class StartupContext | ||
{ | ||
public StartupContext(HostSettings settings, IConfigurationRoot configuration) | ||
{ | ||
Settings = settings ?? throw new ArgumentNullException(nameof(settings)); | ||
Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); | ||
} | ||
|
||
public HostSettings Settings { get; } | ||
|
||
public IConfigurationRoot Configuration { get; } | ||
} | ||
} |