-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathLearningTransportCustomization.cs
More file actions
64 lines (51 loc) · 3.07 KB
/
LearningTransportCustomization.cs
File metadata and controls
64 lines (51 loc) · 3.07 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
namespace ServiceControl.Transports.Learning
{
using System;
using System.IO;
using System.Linq;
using LearningTransport;
using Microsoft.Extensions.DependencyInjection;
using NServiceBus;
public class LearningTransportCustomization : TransportCustomization<LearningTransport>
{
protected override void CustomizeTransportForPrimaryEndpoint(EndpointConfiguration endpointConfiguration, LearningTransport transportDefinition, TransportSettings transportSettings) =>
transportDefinition.TransportTransactionMode = TransportTransactionMode.SendsAtomicWithReceive;
protected override void CustomizeTransportForAuditEndpoint(EndpointConfiguration endpointConfiguration, LearningTransport transportDefinition, TransportSettings transportSettings) =>
transportDefinition.TransportTransactionMode = TransportTransactionMode.ReceiveOnly;
protected override void CustomizeTransportForMonitoringEndpoint(EndpointConfiguration endpointConfiguration, LearningTransport transportDefinition, TransportSettings transportSettings) =>
transportDefinition.TransportTransactionMode = TransportTransactionMode.ReceiveOnly;
protected override LearningTransport CreateTransport(TransportSettings transportSettings, TransportTransactionMode preferredTransactionMode = TransportTransactionMode.ReceiveOnly)
{
var transport = new LearningTransport
{
StorageDirectory = FindStoragePath(transportSettings.ConnectionString),
RestrictPayloadSize = false,
};
transport.TransportTransactionMode = transport.GetSupportedTransactionModes().Contains(preferredTransactionMode) ? preferredTransactionMode : TransportTransactionMode.ReceiveOnly;
return transport;
}
protected override void AddTransportForMonitoringCore(IServiceCollection services, TransportSettings transportSettings)
{
services.AddSingleton<IProvideQueueLength, QueueLengthProvider>();
services.AddHostedService(provider => provider.GetRequiredService<IProvideQueueLength>());
}
internal static string FindStoragePath(string connectionString)
{
var storagePath = Environment.ExpandEnvironmentVariables(connectionString);
if (!string.IsNullOrWhiteSpace(storagePath))
{
return storagePath;
}
var directory = AppDomain.CurrentDomain.BaseDirectory;
while (true)
{
if (Directory.EnumerateFiles(directory).Any(file => file.EndsWith(".sln") || file.EndsWith(".slnx")))
{
return Path.Combine(directory, ".learningtransport");
}
var parent = Directory.GetParent(directory) ?? throw new Exception($"Unable to determine the storage directory path for the learning transport due to the absence of a solution file. Specify the path explicitly in the app.config connection string.");
directory = parent.FullName;
}
}
}
}