Skip to content

Commit 3ff7efe

Browse files
tmasternakbording
andauthored
ServiceControl EventsSubscription sample (#7478)
* fixing existing samples * core 10 version of the sample * Revert "core 10 version of the sample" This reverts commit 15849fa. * Clean up samples --------- Co-authored-by: Brandon Ording <[email protected]>
1 parent beaf3e2 commit 3ff7efe

File tree

23 files changed

+235
-315
lines changed

23 files changed

+235
-315
lines changed

samples/servicecontrol/events-subscription/ServiceControlContracts_3/NServiceBusEndpoint/Program.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ static async Task Main()
1313
endpointConfiguration.EnableInstallers();
1414
endpointConfiguration.SendFailedMessagesTo("error");
1515

16+
endpointConfiguration.SendHeartbeatTo(
17+
serviceControlQueue: "Particular.ServiceControl",
18+
frequency: TimeSpan.FromSeconds(10),
19+
timeToLive: TimeSpan.FromSeconds(30));
20+
1621
#region DisableRetries
1722

1823
var recoverability = endpointConfiguration.Recoverability();
@@ -31,9 +36,11 @@ static async Task Main()
3136
#endregion
3237

3338
var endpointInstance = await Endpoint.Start(endpointConfiguration);
34-
Console.WriteLine("Press 'Enter' to send a new message. Press any other key to finish.");
39+
3540
while (true)
3641
{
42+
Console.WriteLine("Press 'Enter' to send a new message. Press any other key to finish.");
43+
3744
var key = Console.ReadKey();
3845

3946
if (key.Key != ConsoleKey.Enter)
@@ -47,10 +54,10 @@ static async Task Main()
4754
{
4855
Id = guid
4956
};
57+
5058
await endpointInstance.Send("NServiceBusEndpoint", simpleMessage);
51-
Console.WriteLine($"Sent a new message with Id = {guid}.");
5259

53-
Console.WriteLine("Press 'Enter' to send a new message. Press any other key to finish.");
60+
Console.WriteLine($"Sent a new message with Id = {guid}.");
5461
}
5562
await endpointInstance.Stop();
5663
}

samples/servicecontrol/events-subscription/ServiceControlContracts_3/PlatformLauncher/PlatformLauncher.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>true</ImplicitUsings>
67
<LangVersion>12.0</LangVersion>
78
</PropertyGroup>
89

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,2 @@
1-
namespace PlatformLauncher
2-
{
3-
using System;
4-
using System.Threading.Tasks;
5-
6-
class Program
7-
{
8-
static async Task Main(string[] args)
9-
{
10-
Console.Title = "PlatformLauncher";
11-
await Particular.PlatformLauncher.Launch();
12-
}
13-
}
14-
}
1+
Console.Title = "PlatformLauncher";
2+
await Particular.PlatformLauncher.Launch();

samples/servicecontrol/events-subscription/ServiceControlContracts_4/EndpointsMonitor/CustomEventsHandler.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
using System.Threading.Tasks;
4-
using Microsoft.Extensions.Logging;
5-
using NServiceBus;
1+
using Microsoft.Extensions.Logging;
62
using ServiceControl.Contracts;
73

84
#region ServiceControlEventsHandlers
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2+
23
<PropertyGroup>
34
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
45
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>true</ImplicitUsings>
57
<LangVersion>12.0</LangVersion>
68
</PropertyGroup>
9+
710
<ItemGroup>
8-
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
911
<PackageReference Include="NServiceBus" Version="8.*" />
10-
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="2.0.1" />
11-
<PackageReference Include="NServiceBus.Persistence.NonDurable" Version="1.*" />
12-
<PackageReference Include="NServiceBus.Newtonsoft.Json" Version="3.*" />
12+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="2.*" />
1313
<PackageReference Include="ServiceControl.Contracts" Version="4.*" />
1414
</ItemGroup>
15+
1516
</Project>
Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
11
using Microsoft.Extensions.Hosting;
2-
using NServiceBus;
3-
using System;
4-
using System.Threading.Tasks;
52

6-
class Program
3+
Console.Title = "EndpointsMonitor";
4+
5+
var builder = Host.CreateDefaultBuilder(args);
6+
7+
builder.UseNServiceBus(x =>
78
{
8-
public static async Task Main(string[] args)
9-
{
10-
await CreateHostBuilder(args).Build().RunAsync();
11-
}
12-
13-
public static IHostBuilder CreateHostBuilder(string[] args) =>
14-
Host.CreateDefaultBuilder(args)
15-
.ConfigureServices((hostContext, services) =>
16-
{
17-
Console.Title = "EndpointsMonitor";
18-
19-
}).UseNServiceBus(x =>
20-
{
21-
var endpointConfiguration = new EndpointConfiguration("EndpointsMonitor");
22-
endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>();
23-
endpointConfiguration.EnableInstallers();
24-
endpointConfiguration.UsePersistence<NonDurablePersistence>();
25-
26-
#region ServiceControlEventsMonitorCustomErrorQueue
27-
endpointConfiguration.SendFailedMessagesTo("error-monitoring");
28-
#endregion
29-
30-
var transport = endpointConfiguration.UseTransport<LearningTransport>();
31-
32-
var conventions = endpointConfiguration.Conventions();
33-
conventions.DefiningEventsAs(
34-
type =>
35-
{
36-
return typeof(IEvent).IsAssignableFrom(type) ||
37-
// include ServiceControl events
38-
type.Namespace != null &&
39-
type.Namespace.StartsWith("ServiceControl.Contracts");
40-
});
41-
42-
return endpointConfiguration;
43-
});
44-
45-
}
9+
var endpointConfiguration = new EndpointConfiguration("EndpointsMonitor");
10+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
11+
endpointConfiguration.EnableInstallers();
12+
13+
#region ServiceControlEventsMonitorCustomErrorQueue
14+
endpointConfiguration.SendFailedMessagesTo("error-monitoring");
15+
#endregion
16+
17+
var transport = endpointConfiguration.UseTransport<LearningTransport>();
18+
19+
var conventions = endpointConfiguration.Conventions();
20+
conventions.DefiningEventsAs(
21+
type =>
22+
{
23+
return typeof(IEvent).IsAssignableFrom(type) ||
24+
// include ServiceControl events
25+
type.Namespace != null &&
26+
type.Namespace.StartsWith("ServiceControl.Contracts");
27+
});
28+
29+
return endpointConfiguration;
30+
});
31+
32+
var host = builder.Build();
33+
34+
await host.RunAsync();

samples/servicecontrol/events-subscription/ServiceControlContracts_4/NServiceBusEndpoint/InputLoopService.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2+
23
<PropertyGroup>
34
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
45
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>true</ImplicitUsings>
57
<LangVersion>12.0</LangVersion>
68
</PropertyGroup>
9+
710
<ItemGroup>
8-
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
911
<PackageReference Include="NServiceBus" Version="8.*" />
10-
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="2.0.1" />
12+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="2.*" />
1113
<PackageReference Include="NServiceBus.Heartbeat" Version="4.*" />
12-
<PackageReference Include="NServiceBus.Newtonsoft.Json" Version="3.*" />
1314
</ItemGroup>
15+
1416
</Project>
Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,68 @@
1-
using System;
2-
using System.Threading.Tasks;
3-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.Extensions.DependencyInjection;
42
using Microsoft.Extensions.Hosting;
5-
using NServiceBus;
63

7-
class Program
4+
Console.Title = "NServiceBusEndpoint";
5+
6+
var host = Host.CreateDefaultBuilder(args)
7+
.UseNServiceBus(x =>
8+
{
9+
var endpointConfiguration = new EndpointConfiguration("NServiceBusEndpoint");
10+
endpointConfiguration.UseTransport<LearningTransport>();
11+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
12+
endpointConfiguration.EnableInstallers();
13+
endpointConfiguration.SendFailedMessagesTo("error");
14+
15+
endpointConfiguration.SendHeartbeatTo(
16+
serviceControlQueue: "Particular.ServiceControl",
17+
frequency: TimeSpan.FromSeconds(10),
18+
timeToLive: TimeSpan.FromSeconds(30));
19+
20+
#region DisableRetries
21+
22+
var recoverability = endpointConfiguration.Recoverability();
23+
24+
recoverability.Delayed(retriesSettings =>
25+
{
26+
retriesSettings.NumberOfRetries(0);
27+
});
28+
29+
recoverability.Immediate(retriesSettings =>
30+
{
31+
retriesSettings.NumberOfRetries(0);
32+
});
33+
34+
#endregion
35+
36+
return endpointConfiguration;
37+
})
38+
.Build();
39+
40+
41+
await host.StartAsync();
42+
43+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
44+
45+
while (true)
846
{
9-
public static async Task Main(string[] args)
47+
Console.WriteLine("Press 'Enter' to send a new message. Press any other key to finish.");
48+
49+
var key = Console.ReadKey();
50+
51+
if (key.Key != ConsoleKey.Enter)
1052
{
11-
await CreateHostBuilder(args).Build().RunAsync();
53+
break;
1254
}
1355

14-
public static IHostBuilder CreateHostBuilder(string[] args) =>
15-
Host.CreateDefaultBuilder(args)
16-
.UseNServiceBus(x =>
17-
{
18-
var endpointConfiguration = new EndpointConfiguration("NServiceBusEndpoint");
19-
endpointConfiguration.UseTransport<LearningTransport>();
20-
endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>();
21-
endpointConfiguration.EnableInstallers();
22-
endpointConfiguration.SendFailedMessagesTo("error");
23-
24-
#region DisableRetries
25-
26-
var recoverability = endpointConfiguration.Recoverability();
27-
28-
recoverability.Delayed(
29-
customizations: retriesSettings =>
30-
{
31-
retriesSettings.NumberOfRetries(0);
32-
});
33-
recoverability.Immediate(
34-
customizations: retriesSettings =>
35-
{
36-
retriesSettings.NumberOfRetries(0);
37-
});
38-
39-
#endregion
40-
41-
return endpointConfiguration;
42-
}).ConfigureServices((hostContext, services) =>
43-
{
44-
Console.Title = "NServiceBusEndpoint";
45-
services.AddHostedService<InputLoopService>();
46-
47-
});
48-
49-
}
56+
var guid = Guid.NewGuid();
57+
58+
var simpleMessage = new SimpleMessage
59+
{
60+
Id = guid
61+
};
62+
63+
await messageSession.Send("NServiceBusEndpoint", simpleMessage);
64+
65+
Console.WriteLine($"Sent a new message with Id = {guid}.");
66+
}
67+
68+
await host.StopAsync();

samples/servicecontrol/events-subscription/ServiceControlContracts_4/NServiceBusEndpoint/SimpleMessage.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using NServiceBus;
3-
4-
public class SimpleMessage :
1+
public class SimpleMessage :
52
IMessage
63
{
74
public Guid Id { get; set; }

0 commit comments

Comments
 (0)