Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 1cf3cc8

Browse files
committed
- .net core refactoring
1 parent a28b7cc commit 1cf3cc8

File tree

6 files changed

+131
-43
lines changed

6 files changed

+131
-43
lines changed

Shuttle.Core.ServiceHost/.build/package.nuspec

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
<releaseNotes />
1515
<copyright>Copyright (c) {year}, Eben Roux</copyright>
1616
<tags>shuttle</tags>
17+
<frameworkAssemblies>
18+
<frameworkAssembly assemblyName="System.ServiceProcess" targetFramework="net46" />
19+
<frameworkAssembly assemblyName="System.ServiceProcess" targetFramework="net461" />
20+
<frameworkAssembly assemblyName="System.ServiceProcess" targetFramework="net462" />
21+
<frameworkAssembly assemblyName="System.ServiceProcess" targetFramework="net47" />
22+
<frameworkAssembly assemblyName="System.ServiceProcess" targetFramework="net471" />
23+
</frameworkAssemblies>
1724
<dependencies>
1825
<dependency id="Shuttle.Core.Cli" version="{Shuttle.Core.Cli-version}" />
1926
<dependency id="Shuttle.Core.Logging" version="{Shuttle.Core.Logging-version}" />

Shuttle.Core.ServiceHost/ConsoleService.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.Reflection;
3+
#if (!NETCOREAPP2_0 && !NETSTANDARD2_0)
24
using System.Linq;
35
using System.ServiceProcess;
6+
#endif
47
using System.Threading;
58
using Shuttle.Core.Contract;
69

@@ -9,19 +12,30 @@ namespace Shuttle.Core.ServiceHost
912
public class ConsoleService
1013
{
1114
private readonly IServiceHostStart _service;
15+
16+
#if (!NETCOREAPP2_0 && !NETSTANDARD2_0)
1217
private readonly IServiceConfiguration _configuration;
1318

1419
public ConsoleService(IServiceHostStart service, IServiceConfiguration configuration)
1520
{
1621
Guard.AgainstNull(service, nameof(service));
1722
Guard.AgainstNull(configuration, nameof(configuration));
1823

19-
_service = service;
2024
_configuration = configuration;
25+
_service = service;
26+
}
27+
#else
28+
public ConsoleService(IServiceHostStart service)
29+
{
30+
Guard.AgainstNull(service, nameof(service));
31+
32+
_service = service;
2133
}
34+
#endif
2235

2336
public void Execute()
2437
{
38+
#if (!NETCOREAPP2_0 && !NETSTANDARD2_0)
2539
var serviceController =
2640
ServiceController.GetServices()
2741
.FirstOrDefault(s => s.ServiceName == _configuration.ServiceName);
@@ -32,6 +46,7 @@ public void Execute()
3246
$"WARNING: Windows service '{_configuration.ServiceName}' is running. The display name is '{serviceController.DisplayName}'.");
3347
Console.WriteLine();
3448
}
49+
#endif
3550

3651
var waitHandle = new ManualResetEvent(false);
3752
var waitHandles = new WaitHandle[] { waitHandle };
@@ -55,7 +70,11 @@ public void Execute()
5570
_service.Start();
5671

5772
Console.WriteLine();
73+
#if (!NETCOREAPP2_0 && !NETSTANDARD2_0)
5874
ConsoleExtensions.WriteLine(ConsoleColor.Green, $"[started] : '{_configuration.ServiceName}'.");
75+
#else
76+
ConsoleExtensions.WriteLine(ConsoleColor.Green, $"[started] : '{Assembly.GetEntryAssembly().FullName}'.");
77+
#endif
5978
Console.WriteLine();
6079
ConsoleExtensions.WriteLine(ConsoleColor.DarkYellow, "[press ctrl+c to stop]");
6180
Console.WriteLine();

Shuttle.Core.ServiceHost/ServiceHost.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ private void UnhandledException(object sender, UnhandledExceptionEventArgs e)
3434
{
3535
var ex = (Exception) e.ExceptionObject;
3636

37-
_log.WrinteEntry(ex.Message, EventLogEntryType.Error);
37+
_log.WriteEntry(ex.Message, EventLogEntryType.Error);
3838
}
3939

4040
protected override void OnStart(string[] args)
4141
{
42-
_log.WrinteEntry($"[starting] : service name = '{ServiceName}'");
42+
_log.WriteEntry($"[starting] : service name = '{ServiceName}'");
4343

4444
_service.Start();
4545

46-
_log.WrinteEntry($"[started] : service name = '{ServiceName}'");
46+
_log.WriteEntry($"[started] : service name = '{ServiceName}'");
4747
}
4848

4949
protected override void OnStop()
5050
{
51-
_log.WrinteEntry($"[stopping] : service name = '{ServiceName}'");
51+
_log.WriteEntry($"[stopping] : service name = '{ServiceName}'");
5252

5353
var stoppable = _service as IServiceHostStop;
5454

@@ -58,7 +58,7 @@ protected override void OnStop()
5858

5959
disposable?.Dispose();
6060

61-
_log.WrinteEntry($"[stopped] : service name = '{ServiceName}'");
61+
_log.WriteEntry($"[stopped] : service name = '{ServiceName}'");
6262
}
6363

6464
public static void Run<T>() where T : IServiceHostStart, new()
@@ -115,7 +115,7 @@ public static void Run(IServiceHostStart service, Action<IServiceConfiguration>
115115
}
116116
catch (Exception ex)
117117
{
118-
GetServiceHostEventLog(configuration).WrinteEntry(ex.Message, EventLogEntryType.Error);
118+
GetServiceHostEventLog(configuration).WriteEntry(ex.Message, EventLogEntryType.Error);
119119
throw;
120120
}
121121
}
@@ -129,7 +129,7 @@ public static void Run(IServiceHostStart service, Action<IServiceConfiguration>
129129
}
130130
catch (Exception ex)
131131
{
132-
Log.For(typeof(ServiceHost)).Error(ex.Message);
132+
Log.For(typeof(ServiceHost)).Fatal(ex.Message);
133133
throw;
134134
}
135135
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using Shuttle.Core.Contract;
3+
using Shuttle.Core.Logging;
4+
5+
namespace Shuttle.Core.ServiceHost
6+
{
7+
public sealed class ServiceHost
8+
{
9+
private readonly ILog _log;
10+
11+
private ServiceHost()
12+
{
13+
_log = Log.For(this);
14+
15+
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
16+
}
17+
18+
private void UnhandledException(object sender, UnhandledExceptionEventArgs e)
19+
{
20+
var ex = (Exception) e.ExceptionObject;
21+
22+
_log.Error(ex.Message);
23+
}
24+
25+
public static void Run<T>() where T : IServiceHostStart, new()
26+
{
27+
Run(new T());
28+
}
29+
30+
public static void Run(IServiceHostStart service)
31+
{
32+
Guard.AgainstNull(service, nameof(service));
33+
34+
try
35+
{
36+
Console.CursorVisible = false;
37+
38+
new ConsoleService(service).Execute();
39+
}
40+
catch (Exception ex)
41+
{
42+
Log.For(typeof(ServiceHost)).Fatal(ex.Message);
43+
throw;
44+
}
45+
}
46+
}
47+
}

Shuttle.Core.ServiceHost/ServiceHostEventLog.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public static EventLog GetEventLog(string source)
3939
return result;
4040
}
4141

42-
public void WrinteEntry(string message)
42+
public void WriteEntry(string message)
4343
{
44-
WrinteEntry(message, EventLogEntryType.Information);
44+
WriteEntry(message, EventLogEntryType.Information);
4545
}
4646

47-
public void WrinteEntry(string message, EventLogEntryType type)
47+
public void WriteEntry(string message, EventLogEntryType type)
4848
{
4949
_eventLog?.WriteEntry(message, type);
5050
}
Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,51 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFrameworks>net46;net461;net462;net47;net471</TargetFrameworks>
5-
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
6-
</PropertyGroup>
7-
8-
<ItemGroup>
9-
<None Remove="Content\Help.txt" />
10-
</ItemGroup>
11-
12-
<ItemGroup>
13-
<EmbeddedResource Include="Content\Help.txt" />
14-
</ItemGroup>
15-
16-
<ItemGroup>
17-
<None Include=".build\package.msbuild" />
18-
<None Include=".build\package.nuspec" />
19-
<None Include=".build\Shuttle.MSBuild.dll" />
20-
<None Include=".build\Shuttle.MSBuild.targets" />
21-
</ItemGroup>
22-
23-
<ItemGroup>
24-
<PackageReference Include="Shuttle.Core.Cli" Version="10.0.0" />
25-
<PackageReference Include="Shuttle.Core.Logging" Version="10.0.1" />
26-
<PackageReference Include="Shuttle.Core.Reflection" Version="10.0.6" />
27-
<PackageReference Include="Shuttle.Core.Threading" Version="10.0.0" />
28-
</ItemGroup>
29-
30-
<ItemGroup>
31-
<Reference Include="System.Configuration" />
32-
<Reference Include="System.Configuration.Install" />
33-
<Reference Include="System.ServiceProcess" />
34-
</ItemGroup>
3+
<PropertyGroup>
4+
<TargetFrameworks>net46;net461;net462;net47;net471;netstandard2.0;netcoreapp2.0</TargetFrameworks>
5+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<None Remove="Content\Help.txt" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<EmbeddedResource Include="Content\Help.txt" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<None Include=".build\package.msbuild" />
18+
<None Include=".build\package.nuspec" />
19+
<None Include=".build\Shuttle.MSBuild.dll" />
20+
<None Include=".build\Shuttle.MSBuild.targets" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="Shuttle.Core.Cli" Version="10.0.0" />
25+
<PackageReference Include="Shuttle.Core.Logging" Version="10.0.1" />
26+
<PackageReference Include="Shuttle.Core.Reflection" Version="10.0.6" />
27+
<PackageReference Include="Shuttle.Core.Threading" Version="10.0.0" />
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<Reference Include="System.Configuration" Condition="'$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netcoreapp2.0'" />
32+
<Reference Include="System.Configuration.Install" Condition="'$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netcoreapp2.0'" />
33+
<Reference Include="System.ServiceProcess" Condition="'$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netcoreapp2.0'" />
34+
</ItemGroup>
35+
36+
<ItemGroup>
37+
<Compile Remove="ServiceHostInstaller.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.0'" />
38+
<Compile Remove="ServiceHostController.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.0'" />
39+
<Compile Remove="ServiceInvoker.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.0'" />
40+
<Compile Remove="ServiceHostSection.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.0'" />
41+
<Compile Remove="IServiceConfiguration.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.0'" />
42+
<Compile Remove="ServiceConfiguration.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.0'" />
43+
<Compile Remove="ServiceHostEventLog.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.0'" />
44+
<Compile Remove="ServiceHost.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.0'" />
45+
<Compile Remove="CommandProcessor.cs;WindowsServiceInstaller.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.0'" />
46+
<Compile Remove="WindowsServiceInstaller.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.0'" />
47+
48+
<Compile Remove="ServiceHostCore.cs" Condition="'$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netcoreapp2.0'" />
49+
</ItemGroup>
3550

3651
</Project>

0 commit comments

Comments
 (0)