Skip to content

Commit

Permalink
fix handling of error during startup
Browse files Browse the repository at this point in the history
  • Loading branch information
mookid8000 committed Aug 14, 2020
1 parent 0b76a4e commit 31bb9cb
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@
## 3.2.0
* Provide ability to customize Topshelf's `HostConfigurator`, making it possible e.g. to configure a service's description, crash recovery, etc. - thanks [igitur]

## 3.2.1
* Work around bug in Topshelf, which would not trigger Windows Service recovery if startup failed

[igitur]: https://github.com/igitur
[scardetto]: https://github.com/scardetto
8 changes: 7 additions & 1 deletion Topper/ServiceHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ static void RunAsTopShelf(ServiceConfiguration configuration)
service.StartupFailed += exception =>
{
Log.ErrorException("Startup failed", exception);
control.Stop();

// bug in Topshelf: it doesn't seem to trigger recovery, when calling this:
// control.Stop(TopshelfExitCode.AbnormalExit);
// so instead we call this:
Environment.Exit(-1);
// more info here:
// https://github.com/Topshelf/Topshelf/issues/479
};
service.Start();
return true;
Expand Down
2 changes: 1 addition & 1 deletion Topper/Topper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="topshelf" Version="4.2.0" />
<PackageReference Include="topshelf" Version="4.2.1" />
</ItemGroup>

</Project>
20 changes: 12 additions & 8 deletions Toppertest/App.config
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Serilog" publicKeyToken="24c2f752a8e58a10" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0"/>
<assemblyIdentity name="Serilog" publicKeyToken="24c2f752a8e58a10" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Topshelf" publicKeyToken="b800c4cfcdeea87b" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0"/>
<assemblyIdentity name="Topshelf" publicKeyToken="b800c4cfcdeea87b" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0"/>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Serilog.Sinks.Console" publicKeyToken="24c2f752a8e58a10" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.1.1.0" newVersion="3.1.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
36 changes: 30 additions & 6 deletions Toppertest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Serilog;
using System;
using System.Threading.Tasks;
using Serilog;
using Topper;
using Topshelf;

Expand All @@ -10,25 +12,47 @@ static void Main()
{
Log.Logger = new LoggerConfiguration()
.WriteTo.ColoredConsole()
//.MinimumLevel.Warning()
.WriteTo.File(@"C:\logs\toppertest\log.txt", rollOnFileSizeLimit: true, fileSizeLimitBytes: 1024 * 1024)
.MinimumLevel.Verbose()
.CreateLogger();

var configuration = new ServiceConfiguration()
.Configure(c =>
{
c.EnableParallelStartup();
c.EnableParallelShutdown();
//c.EnableParallelStartup();
//c.EnableParallelShutdown();

c.Topshelf(config =>
{
config.SetDescription("Some description");
config.EnableServiceRecovery(r => r.RestartService(5));
});
})
.Add("test1", () => new TestService1())
.Add("test2", () => new TestService2());
.Add("crashtest", async () =>
{
var service = new CrashingTestService();
await service.CrashAsync();
return service;
})
//.Add("test1", () => new TestService1())
//.Add("test2", () => new TestService2())
;

ServiceHost.Run(configuration);
}
}

class CrashingTestService : IDisposable
{
public async Task CrashAsync()
{
await Task.Delay(TimeSpan.FromSeconds(2));

throw new InvalidOperationException("OH NO!");
}

public void Dispose()
{
}
}
}
1 change: 1 addition & 0 deletions Toppertest/TestService1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class TestService1 : IDisposable
public TestService1()
{
}

public void Dispose()
{
}
Expand Down
13 changes: 8 additions & 5 deletions Toppertest/Toppertest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.2.5.0\lib\net46\Serilog.dll</HintPath>
<HintPath>..\packages\Serilog.2.9.0\lib\net46\Serilog.dll</HintPath>
</Reference>
<Reference Include="Serilog.Sinks.ColoredConsole, Version=3.0.1.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.Sinks.ColoredConsole.3.0.1\lib\net45\Serilog.Sinks.ColoredConsole.dll</HintPath>
</Reference>
<Reference Include="Serilog.Sinks.Console, Version=3.0.1.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.Sinks.Console.3.0.1\lib\net45\Serilog.Sinks.Console.dll</HintPath>
<Reference Include="Serilog.Sinks.Console, Version=3.1.1.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.Sinks.Console.3.1.1\lib\net45\Serilog.Sinks.Console.dll</HintPath>
</Reference>
<Reference Include="Serilog.Sinks.File, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.Sinks.File.4.1.0\lib\net45\Serilog.Sinks.File.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
Expand All @@ -52,8 +55,8 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="Topshelf, Version=4.2.0.194, Culture=neutral, PublicKeyToken=b800c4cfcdeea87b, processorArchitecture=MSIL">
<HintPath>..\packages\Topshelf.4.2.0\lib\net452\Topshelf.dll</HintPath>
<Reference Include="Topshelf, Version=4.2.1.215, Culture=neutral, PublicKeyToken=b800c4cfcdeea87b, processorArchitecture=MSIL">
<HintPath>..\packages\Topshelf.4.2.1\lib\net452\Topshelf.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down
7 changes: 4 additions & 3 deletions Toppertest/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Serilog" version="2.5.0" targetFramework="net462" />
<package id="Serilog" version="2.9.0" targetFramework="net462" />
<package id="Serilog.Sinks.ColoredConsole" version="3.0.1" targetFramework="net462" />
<package id="Serilog.Sinks.Console" version="3.0.1" targetFramework="net462" />
<package id="Topshelf" version="4.2.0" targetFramework="net462" />
<package id="Serilog.Sinks.Console" version="3.1.1" targetFramework="net462" />
<package id="Serilog.Sinks.File" version="4.1.0" targetFramework="net462" />
<package id="Topshelf" version="4.2.1" targetFramework="net462" />
</packages>

0 comments on commit 31bb9cb

Please sign in to comment.