Skip to content

Commit

Permalink
Working on .NET core version of Topshelf (currently only ConsoleRunHost)
Browse files Browse the repository at this point in the history
  • Loading branch information
phatboyg committed Jan 6, 2018
1 parent a305af6 commit 61211e7
Show file tree
Hide file tree
Showing 16 changed files with 267 additions and 121 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ packages

.fake
.idea
.vs


bin
Expand Down
1 change: 0 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<Project>
<!-- These properties will be shared for all projects -->
<PropertyGroup>
<Title>Topshelf</Title>
<Product>Topshelf</Product>
<Description>Topshelf is an open source project for hosting services without friction. By referencing Topshelf, your console application *becomes* a service installer with a comprehensive set of command-line options for installing, configuring, and running your application as a service.</Description>
<PackageProjectUrl>https://github.com/Topshelf/Topshelf</PackageProjectUrl>
Expand Down
10 changes: 8 additions & 2 deletions src/SampleTopshelfService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace SampleTopshelfService
{
using System;
using Serilog;
using Topshelf;

class Program
Expand All @@ -21,7 +22,11 @@ static int Main()
{
return (int)HostFactory.Run(x =>
{
x.UseLog4Net("log4net.config");
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.ColoredConsole()
.CreateLogger();
x.UseSerilog();

x.UseAssemblyInfoForServiceInfo();

Expand All @@ -38,6 +43,7 @@ static int Main()
x.SetStartTimeout(TimeSpan.FromSeconds(10));
x.SetStopTimeout(TimeSpan.FromSeconds(10));

#if !NETCORE
x.EnableServiceRecovery(r =>
{
r.RestartService(3);
Expand All @@ -47,7 +53,7 @@ static int Main()
r.OnCrashOnly();
r.SetResetPeriod(2);
});

#endif
x.AddCommandLineSwitch("throwonstart", v => throwOnStart = v);
x.AddCommandLineSwitch("throwonstop", v => throwOnStop = v);
x.AddCommandLineSwitch("throwunhandled", v => throwUnhandled = v);
Expand Down
15 changes: 9 additions & 6 deletions src/SampleTopshelfService/SampleTopshelfService.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net452</TargetFrameworks>
<TargetFrameworks>net452;netcoreapp2.0</TargetFrameworks>
<DebugType>portable</DebugType>
<IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.8" />
<PackageReference Include="Serilog" Version="2.6.0" />
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Topshelf.Log4Net\Topshelf.Log4Net.csproj" />
<ProjectReference Include="..\Topshelf.Serilog\Topshelf.Serilog.csproj" />
<ProjectReference Include="..\Topshelf\Topshelf.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="log4net.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
38 changes: 0 additions & 38 deletions src/SampleTopshelfService/log4net.config

This file was deleted.

2 changes: 2 additions & 0 deletions src/Topshelf.Log4Net/Topshelf.Log4Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.8" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
Expand Down
4 changes: 3 additions & 1 deletion src/Topshelf.Serilog/Topshelf.Serilog.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net452</TargetFrameworks>
<TargetFrameworks>net452;netstandard2.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<SignAssembly>true</SignAssembly>
<DebugType>portable</DebugType>
Expand All @@ -12,6 +12,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Serilog" Version="2.6.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/Topshelf.Tests/Topshelf.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="NUnit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
Expand Down
44 changes: 24 additions & 20 deletions src/Topshelf.sln
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2005
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Topshelf", "Topshelf\Topshelf.csproj", "{A52AD64D-6455-4A22-8CCF-581851086578}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Topshelf", "Topshelf\Topshelf.csproj", "{A52AD64D-6455-4A22-8CCF-581851086578}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{94D59671-81F3-44B9-B54D-8E5D616DB0BB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleTopshelfService", "SampleTopshelfService\SampleTopshelfService.csproj", "{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleTopshelfService", "SampleTopshelfService\SampleTopshelfService.csproj", "{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Topshelf.Tests", "Topshelf.Tests\Topshelf.Tests.csproj", "{508C7AAB-03DD-40C6-947D-6A8D22AAA941}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Topshelf.Tests", "Topshelf.Tests\Topshelf.Tests.csproj", "{508C7AAB-03DD-40C6-947D-6A8D22AAA941}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Topshelf.Log4Net", "Topshelf.Log4Net\Topshelf.Log4Net.csproj", "{0EE64D46-5728-45DB-8D0E-044CE2677FD8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Topshelf.Log4Net", "Topshelf.Log4Net\Topshelf.Log4Net.csproj", "{0EE64D46-5728-45DB-8D0E-044CE2677FD8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Topshelf.NLog", "Topshelf.NLog\Topshelf.NLog.csproj", "{55FC6F5C-778F-4E77-8C55-C62216858CE7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Topshelf.NLog", "Topshelf.NLog\Topshelf.NLog.csproj", "{55FC6F5C-778F-4E77-8C55-C62216858CE7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Topshelf.Elmah", "Topshelf.Elmah\Topshelf.Elmah.csproj", "{B6990D65-5B8A-4332-B01F-DE60D7042F6E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Topshelf.Elmah", "Topshelf.Elmah\Topshelf.Elmah.csproj", "{B6990D65-5B8A-4332-B01F-DE60D7042F6E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Topshelf.Serilog", "Topshelf.Serilog\Topshelf.Serilog.csproj", "{20F2B28C-3AD6-4B1D-9371-15DF77443DA8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Topshelf.Serilog", "Topshelf.Serilog\Topshelf.Serilog.csproj", "{20F2B28C-3AD6-4B1D-9371-15DF77443DA8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -48,20 +48,21 @@ Global
{A52AD64D-6455-4A22-8CCF-581851086578}.ReleaseUnsigned|x86.ActiveCfg = Release|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Debug|Mixed Platforms.Build.0 = Debug|x86
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Debug|x86.ActiveCfg = Debug|x86
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Debug|x86.Build.0 = Debug|x86
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Debug|x86.ActiveCfg = Debug|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Debug|x86.Build.0 = Debug|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Release|Any CPU.Build.0 = Release|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Release|Mixed Platforms.ActiveCfg = Release|x86
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Release|Mixed Platforms.Build.0 = Release|x86
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Release|x86.ActiveCfg = Release|x86
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Release|x86.Build.0 = Release|x86
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Release|x86.ActiveCfg = Release|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.Release|x86.Build.0 = Release|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.ReleaseUnsigned|Any CPU.ActiveCfg = Release|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.ReleaseUnsigned|Mixed Platforms.ActiveCfg = Release|x86
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.ReleaseUnsigned|x86.ActiveCfg = Release|x86
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.ReleaseUnsigned|x86.Build.0 = Release|x86
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.ReleaseUnsigned|Mixed Platforms.ActiveCfg = Release|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.ReleaseUnsigned|Mixed Platforms.Build.0 = Release|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.ReleaseUnsigned|x86.ActiveCfg = Release|Any CPU
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3}.ReleaseUnsigned|x86.Build.0 = Release|Any CPU
{508C7AAB-03DD-40C6-947D-6A8D22AAA941}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{508C7AAB-03DD-40C6-947D-6A8D22AAA941}.Debug|Any CPU.Build.0 = Debug|Any CPU
{508C7AAB-03DD-40C6-947D-6A8D22AAA941}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -134,4 +135,7 @@ Global
GlobalSection(NestedProjects) = preSolution
{E5A0AD7B-1806-4F99-84F0-55BD72C1C7A3} = {94D59671-81F3-44B9-B54D-8E5D616DB0BB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DF857ED9-FBAA-438F-881E-A8BB738CA86C}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,37 @@ internal static void AddTopshelfOptions(ICommandLineElementParser<Option> x)
{
int n;

x.Add((from arg in x.Argument("install")
select (Option)new InstallOption())
.Or(from arg in x.Argument("uninstall")
select (Option)new UninstallOption())
.Or(from arg in x.Argument("start")
select (Option)new StartOption())
.Or(from arg in x.Argument("command")
x.Add((from arg in x.Argument("command")
from cmd in x.Argument()
where int.TryParse(cmd.Id, out n)
select (Option)new CommandOption(cmd.Id))
.Or(from arg in x.Argument("help")
select (Option)new HelpOption())
.Or(from arg in x.Argument("run")
select (Option)new RunOption())
.Or(from help in x.Switch("help")
select (Option)new HelpOption())
.Or(from systemHelp in x.Switch("systemonly")
select (Option)new SystemOnlyHelpOption())
.Or(from name in x.Definition("servicename")
select (Option)new ServiceNameOption(name.Value))
.Or(from desc in x.Definition("description")
select (Option)new ServiceDescriptionOption(desc.Value))
.Or(from disp in x.Definition("displayname")
select (Option)new DisplayNameOption(disp.Value))
.Or(from instance in x.Definition("instance")
select (Option)new InstanceOption(instance.Value))
#if !NETCORE
.Or(from arg in x.Argument("stop")
select (Option)new StopOption())
.Or(from arg in x.Argument("install")
select (Option)new InstallOption())
.Or(from arg in x.Argument("uninstall")
select (Option)new UninstallOption())
.Or(from arg in x.Argument("start")
select (Option)new StartOption())
.Or(from arg in x.Switch("sudo")
select (Option)new SudoOption())
.Or(from arg in x.Argument("run")
select (Option)new RunOption())
.Or(from username in x.Definition("username")
from password in x.Definition("password")
select (Option)new ServiceAccountOption(username.Value, password.Value))
Expand All @@ -58,18 +71,8 @@ from password in x.Definition("password")
select (Option)new LocalServiceOption())
.Or(from autostart in x.Switch("networkservice")
select (Option)new NetworkServiceOption())
.Or(from help in x.Switch("help")
select (Option)new HelpOption())
.Or(from systemHelp in x.Switch("systemonly")
select (Option)new SystemOnlyHelpOption())
.Or(from name in x.Definition("servicename")
select (Option)new ServiceNameOption(name.Value))
.Or(from desc in x.Definition("description")
select (Option)new ServiceDescriptionOption(desc.Value))
.Or(from disp in x.Definition("displayname")
select (Option)new DisplayNameOption(disp.Value))
.Or(from instance in x.Definition("instance")
select (Option)new InstanceOption(instance.Value)));
#endif
);
}

internal static void AddUnknownOptions(ICommandLineElementParser<Option> x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Topshelf.HostConfigurators
using Logging;
using Options;
using Runtime;
using Runtime.DotNetCore;
using Runtime.Windows;


Expand Down Expand Up @@ -108,12 +109,12 @@ public void SetInstanceName(string instanceName)

public void SetStartTimeout(TimeSpan startTimeOut)
{
_settings.StartTimeOut = startTimeOut;
_settings.StartTimeOut = startTimeOut;
}

public void SetStopTimeout(TimeSpan stopTimeOut)
{
_settings.StopTimeOut = stopTimeOut;
_settings.StopTimeOut = stopTimeOut;
}

public void EnablePauseAndContinue()
Expand Down Expand Up @@ -191,8 +192,8 @@ public Host CreateHost()
{
Type type = typeof(HostFactory);
HostLogger.Get<HostConfiguratorImpl>()
.InfoFormat("{0} v{1}, .NET Framework v{2}", type.Namespace, type.Assembly.GetName().Version,
Environment.Version);
.InfoFormat("{0} v{1}, .NET Framework v{2}", type.Namespace, type.Assembly.GetName().Version,
Environment.Version);

EnvironmentBuilder environmentBuilder = _environmentBuilderFactory(this);

Expand Down Expand Up @@ -231,7 +232,11 @@ static HostBuilder DefaultHostBuilderFactory(HostEnvironment environment, HostSe

static EnvironmentBuilder DefaultEnvironmentBuilderFactory(HostConfigurator configurator)
{
#if NETCORE
return new DotNetCoreEnvironmentBuilder(configurator);
#else
return new WindowsHostEnvironmentBuilder(configurator);
#endif
}
}
}
Loading

0 comments on commit 61211e7

Please sign in to comment.