-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathSettings.cs
More file actions
109 lines (83 loc) · 4.35 KB
/
Settings.cs
File metadata and controls
109 lines (83 loc) · 4.35 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
namespace ServiceControl.Monitoring
{
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Runtime.Loader;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Configuration;
using ServiceControl.Infrastructure;
using Transports;
public class Settings
{
public Settings(LoggingSettings loggingSettings = null, string transportType = null)
{
LoggingSettings = loggingSettings ?? new(SettingsRootNamespace);
// Overwrite the instance name if it is specified in ENVVAR, reg, or config file
InstanceName = SettingsReader.Read(SettingsRootNamespace, "InstanceName", InstanceName);
TransportType = SettingsReader.Read(SettingsRootNamespace, "TransportType", transportType);
ConnectionString = GetConnectionString();
ErrorQueue = SettingsReader.Read(SettingsRootNamespace, "ErrorQueue", "error");
if (AppEnvironment.RunningInContainer)
{
HttpHostName = "*";
HttpPort = "33633";
}
else
{
HttpHostName = SettingsReader.Read<string>(SettingsRootNamespace, "HttpHostname");
HttpPort = SettingsReader.Read<string>(SettingsRootNamespace, "HttpPort");
}
EndpointUptimeGracePeriod = TimeSpan.Parse(SettingsReader.Read(SettingsRootNamespace, "EndpointUptimeGracePeriod", "00:00:40"));
MaximumConcurrencyLevel = SettingsReader.Read<int?>(SettingsRootNamespace, "MaximumConcurrencyLevel");
ServiceControlThroughputDataQueue = SettingsReader.Read(SettingsRootNamespace, "ServiceControlThroughputDataQueue", "ServiceControl.ThroughputData");
ShutdownTimeout = SettingsReader.Read(SettingsRootNamespace, "ShutdownTimeout", ShutdownTimeout);
AssemblyLoadContextResolver = static assemblyPath => new PluginAssemblyLoadContext(assemblyPath);
}
[JsonIgnore]
public Func<string, AssemblyLoadContext> AssemblyLoadContextResolver { get; set; }
public LoggingSettings LoggingSettings { get; }
public string InstanceName { get; init; } = DEFAULT_INSTANCE_NAME;
public string TransportType { get; set; }
public string ConnectionString { get; set; }
public string ErrorQueue { get; set; }
public string HttpHostName { get; set; }
public string HttpPort { get; set; }
public TimeSpan EndpointUptimeGracePeriod { get; set; }
public string RootUrl => $"http://{HttpHostName}:{HttpPort}/";
public int? MaximumConcurrencyLevel { get; set; }
public string ServiceControlThroughputDataQueue { get; set; }
// The default value is set to the maximum allowed time by the most
// restrictive hosting platform, which is Linux containers. Linux
// containers allow for a maximum of 10 seconds. We set it to 5 to
// allow for cancellation and logging to take place
public TimeSpan ShutdownTimeout { get; set; } = TimeSpan.FromSeconds(5);
public TransportSettings ToTransportSettings()
{
var transportSettings = new TransportSettings
{
ConnectionString = ConnectionString,
EndpointName = InstanceName,
ErrorQueue = ErrorQueue,
MaxConcurrency = MaximumConcurrencyLevel,
AssemblyLoadContextResolver = AssemblyLoadContextResolver,
TransportType = TransportType
};
return transportSettings;
}
static string GetConnectionString()
{
var settingsValue = SettingsReader.Read<string>(SettingsRootNamespace, "ConnectionString");
if (settingsValue != null)
{
return settingsValue;
}
var connectionStringSettings = ConfigurationManager.ConnectionStrings["NServiceBus/Transport"];
return connectionStringSettings?.ConnectionString;
}
internal Func<string, Dictionary<string, string>, byte[], Func<Task>, Task> OnMessage { get; set; } = (messageId, headers, body, next) => next();
public const string DEFAULT_INSTANCE_NAME = "Particular.Monitoring";
public static readonly SettingsRootNamespace SettingsRootNamespace = new("Monitoring");
}
}