Skip to content

Commit 1d0aab2

Browse files
New sample projects for contracts 5 (#6821)
* New sample projects for contracts 5 * Add prerelease * Final version * Revert "Add prerelease" This reverts commit f6ce730. * Moved snippet forward to latest version Co-authored-by: Christian <[email protected]> --------- Co-authored-by: danielmarbach <[email protected]> Co-authored-by: Christian <[email protected]>
1 parent aa1de3d commit 1d0aab2

28 files changed

+598
-4
lines changed

Snippets/ServiceControlContracts/ServiceControlContracts.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.29728.190
55
MinimumVisualStudioVersion = 15.0.26730.12
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceControlContracts_1", "ServiceControlContracts_1\ServiceControlContracts_1.csproj", "{93F53614-102A-48EA-B42F-334125DD8046}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceControlContracts_5", "ServiceControlContracts_5\ServiceControlContracts_5.csproj", "{93F53614-102A-48EA-B42F-334125DD8046}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
File renamed without changes.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
<TargetFramework>net48</TargetFramework>
44
</PropertyGroup>
55
<ItemGroup>
6-
<PackageReference Include="NServiceBus" Version="6.*" />
7-
<PackageReference Include="ServiceControl.Contracts" Version="1.*" />
6+
<PackageReference Include="NServiceBus" Version="8.*" />
7+
<PackageReference Include="NServiceBus.Transport.Msmq" Version="2.*" />
8+
<PackageReference Include="ServiceControl.Contracts" Version="5.*" />
89
</ItemGroup>
910
</Project>
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ServiceControlEventsConfig
88
{
99
#region ServiceControlEventsConfig
1010

11-
endpointConfiguration.UseSerialization<JsonSerializer>();
11+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
1212
var conventions = endpointConfiguration.Conventions();
1313
conventions.DefiningEventsAs(
1414
type =>
File renamed without changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>net8.0;net6.0;net48</TargetFrameworks>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>10.0</LangVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
9+
<PackageReference Include="NServiceBus" Version="8.*" />
10+
<PackageReference Include="NServiceBus.Newtonsoft.Json" Version="3.*" />
11+
<PackageReference Include="ServiceControl.Contracts" Version="5.*" />
12+
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.*" />
13+
<PackageReference Include="NServiceBus.Persistence.NonDurable" Version="1.*" />
14+
</ItemGroup>
15+
</Project>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Microsoft.ApplicationInsights;
5+
using Microsoft.ApplicationInsights.DataContracts;
6+
using NServiceBus;
7+
using NServiceBus.Logging;
8+
using ServiceControl.Contracts;
9+
10+
#region AzureMonitorConnectorEventsHandler
11+
12+
public class MessageFailedHandler :
13+
IHandleMessages<MessageFailed>
14+
{
15+
readonly TelemetryClient telemetryClient;
16+
static ILog log = LogManager.GetLogger<CustomEventsHandler>();
17+
18+
public MessageFailedHandler(TelemetryClient telemetryClient)
19+
{
20+
this.telemetryClient = telemetryClient;
21+
}
22+
23+
public Task Handle(MessageFailed message, IMessageHandlerContext context)
24+
{
25+
26+
telemetryClient.TrackEvent("Message Failed", new Dictionary<string, string>
27+
{
28+
{"MessageId", message.FailedMessageId},
29+
});
30+
31+
log.Error($"Received ServiceControl 'MessageFailed' event for a {message.MessageType} with ID {message.FailedMessageId}.");
32+
return Task.CompletedTask;
33+
}
34+
}
35+
36+
#endregion
37+
38+
public class CustomEventsHandler :
39+
IHandleMessages<HeartbeatStopped>,
40+
IHandleMessages<HeartbeatRestored>,
41+
IHandleMessages<FailedMessagesArchived>,
42+
IHandleMessages<FailedMessagesUnArchived>,
43+
IHandleMessages<MessageFailureResolvedByRetry>,
44+
IHandleMessages<MessageFailureResolvedManually>
45+
{
46+
readonly TelemetryClient telemetryClient;
47+
static ILog log = LogManager.GetLogger<CustomEventsHandler>();
48+
49+
public CustomEventsHandler(TelemetryClient telemetryClient)
50+
{
51+
this.telemetryClient = telemetryClient;
52+
}
53+
54+
public Task Handle(HeartbeatStopped message, IMessageHandlerContext context)
55+
{
56+
telemetryClient.TrackEvent("Heartbeat Stopped", new Dictionary<string, string>
57+
{
58+
{"EndpointName", message.EndpointName},
59+
});
60+
61+
log.Warn($"Heartbeats from {message.EndpointName} have stopped.");
62+
return Task.CompletedTask;
63+
}
64+
65+
public Task Handle(HeartbeatRestored message, IMessageHandlerContext context)
66+
{
67+
telemetryClient.TrackEvent("Heartbeat Restored", new Dictionary<string, string>
68+
{
69+
{"EndpointName", message.EndpointName},
70+
});
71+
72+
log.Info($"Heartbeats from {message.EndpointName} have been restored.");
73+
return Task.CompletedTask;
74+
}
75+
76+
public Task Handle(FailedMessagesArchived message, IMessageHandlerContext context)
77+
{
78+
telemetryClient.TrackEvent("Failed Messages Archived", new Dictionary<string, string>
79+
{
80+
{"MessagesIds", string.Join(",", message.FailedMessagesIds)},
81+
});
82+
83+
log.Error($"Received ServiceControl 'FailedMessageArchived' with ID {message.FailedMessagesIds.FirstOrDefault()}.");
84+
return Task.CompletedTask;
85+
}
86+
87+
public Task Handle(FailedMessagesUnArchived message, IMessageHandlerContext context)
88+
{
89+
telemetryClient.TrackEvent("Failed Messages Unarchived", new Dictionary<string, string>
90+
{
91+
{"MessagesIds", string.Join(",", message.FailedMessagesIds)},
92+
});
93+
94+
log.Error($"Received ServiceControl 'FailedMessagesUnArchived' MessagesCount: {message.FailedMessagesIds.Length}.");
95+
return Task.CompletedTask;
96+
}
97+
98+
public Task Handle(MessageFailureResolvedByRetry message, IMessageHandlerContext context)
99+
{
100+
telemetryClient.TrackEvent("Message Failure Resolved By Retry", new Dictionary<string, string>
101+
{
102+
{"MessageId", message.FailedMessageId},
103+
});
104+
105+
log.Error($"Received ServiceControl 'MessageFailureResolvedByRetry' with ID {message.FailedMessageId}.");
106+
return Task.CompletedTask;
107+
}
108+
109+
public Task Handle(MessageFailureResolvedManually message, IMessageHandlerContext context)
110+
{
111+
telemetryClient.TrackEvent("Message Failure Resolved Manually", new Dictionary<string, string>
112+
{
113+
{"MessageId", message.FailedMessageId},
114+
});
115+
116+
log.Error($"Received ServiceControl 'MessageFailureResolvedManually' with ID {message.FailedMessageId}.");
117+
return Task.CompletedTask;
118+
}
119+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.ApplicationInsights;
4+
using Microsoft.ApplicationInsights.Extensibility;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using NServiceBus;
7+
8+
class Program
9+
{
10+
static async Task Main()
11+
{
12+
Console.Title = "AzureMonitorConnector";
13+
var endpointConfiguration = new EndpointConfiguration("AzureMonitorConnector");
14+
endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>();
15+
endpointConfiguration.EnableInstallers();
16+
endpointConfiguration.UsePersistence<NonDurablePersistence>();
17+
endpointConfiguration.SendFailedMessagesTo("error");
18+
19+
var transport = endpointConfiguration.UseTransport<LearningTransport>();
20+
21+
var conventions = endpointConfiguration.Conventions();
22+
var appInsightsConnectionString = "<YOUR KEY HERE>";
23+
conventions.DefiningEventsAs(
24+
type =>
25+
{
26+
return typeof(IEvent).IsAssignableFrom(type) ||
27+
// include ServiceControl events
28+
type.Namespace != null &&
29+
type.Namespace.StartsWith("ServiceControl.Contracts");
30+
});
31+
32+
#region AppInsightsSdkSetup
33+
34+
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
35+
telemetryConfiguration.ConnectionString = appInsightsConnectionString;
36+
var telemetryClient = new TelemetryClient(telemetryConfiguration);
37+
38+
#endregion
39+
40+
endpointConfiguration.RegisterComponents(cc => cc.AddSingleton(telemetryClient));
41+
42+
var endpointInstance = await Endpoint.Start(endpointConfiguration);
43+
Console.WriteLine("Press any key to finish.");
44+
Console.ReadKey();
45+
await endpointInstance.Stop();
46+
}
47+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
EndpointsMonitor\AzureMonitorConnector.csproj
2+
NServiceBusEndpoint\NServiceBusEndpoint.csproj
3+
PlatformLauncher\PlatformLauncher.csproj

0 commit comments

Comments
 (0)