-
Notifications
You must be signed in to change notification settings - Fork 150
/
HostConfigurator.cs
69 lines (62 loc) · 2.11 KB
/
HostConfigurator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PeterKottas.DotNetCore.WindowsService.Configurators.Service;
using PeterKottas.DotNetCore.WindowsService.Interfaces;
namespace PeterKottas.DotNetCore.WindowsService
{
public class HostConfigurator<SERVICE> where SERVICE : IMicroService
{
HostConfiguration<SERVICE> innerConfig;
public HostConfigurator(HostConfiguration<SERVICE> innerConfig)
{
this.innerConfig = innerConfig;
}
public void SetName(string serviceName)
{
if (string.IsNullOrEmpty(innerConfig.Name))
{
innerConfig.Name = serviceName;
}
}
public void SetDisplayName(string displayName)
{
if (string.IsNullOrEmpty(innerConfig.DisplayName))
{
innerConfig.DisplayName = displayName;
}
}
public void SetDescription(string description)
{
if (string.IsNullOrEmpty(innerConfig.Description))
{
innerConfig.Description = description;
}
}
public string GetDefaultName()
{
return innerConfig.Name;
}
public void Service(Action<ServiceConfigurator<SERVICE>> serviceConfigAction)
{
try
{
var serviceConfig = new ServiceConfigurator<SERVICE>(innerConfig);
serviceConfigAction(serviceConfig);
if(innerConfig.ServiceFactory==null)
{
throw new ArgumentException("It's necesarry to configure action that creates the service (ServiceFactory)");
}
if (innerConfig.OnServiceStart == null)
{
throw new ArgumentException("It's necesarry to configure action that is called when the service starts");
}
}
catch (Exception e)
{
throw new ArgumentException("Configuring the service thrown an exception", e);
}
}
}
}