Skip to content

Unobtrusive sample for .NET 10 #7496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions samples/unobtrusive/Core_10/Client/Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>preview</LangVersion>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\ConventionExtensions.cs" />
</ItemGroup>

</Project>
86 changes: 86 additions & 0 deletions samples/unobtrusive/Core_10/Client/CommandSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Commands;
using Messages;

public class CommandSender
{
public static async Task Start(IMessageSession messageSession)
{
Console.WriteLine("Press 'C' to send a command");
Console.WriteLine("Press 'R' to send a request");
Console.WriteLine("Press 'D' to send a large message that is marked to be sent using data bus");
Console.WriteLine("Press 'X' to send a message that is marked with expiration time.");
Console.WriteLine("Press any other key to exit");

while (true)
{
var key = Console.ReadKey();
Console.WriteLine();

switch (key.Key)
{
case ConsoleKey.C:
await SendCommand(messageSession);
continue;
case ConsoleKey.R:
await SendRequest(messageSession);
continue;
case ConsoleKey.D:
await Data(messageSession);
continue;
case ConsoleKey.X:
await Expiration(messageSession);
continue;
}
return;

}
}

// Shut down server before sending this message, after 30 seconds, the message will be moved to Transactional dead-letter messages queue.
static Task Expiration(IMessageSession messageSession)
{
var messageThatExpires = new MessageThatExpires
{
RequestId = Guid.NewGuid()
};
Console.WriteLine("message with expiration was sent");
return messageSession.Send("Samples.Unobtrusive.Server", messageThatExpires);
}

static Task Data(IMessageSession messageSession)
{
var requestId = Guid.NewGuid();

var largeMessage = new LargeMessage
{
RequestId = requestId,
LargeDataBus = new byte[1024 * 1024 * 5]
};
Console.WriteLine($"Request sent id: {requestId}");
return messageSession.Send("Samples.Unobtrusive.Server", largeMessage);
}

static Task SendRequest(IMessageSession messageSession)
{
var requestId = Guid.NewGuid();

var request = new Request
{
RequestId = requestId
};
Console.WriteLine($"Request sent id: {requestId}");
return messageSession.Send("Samples.Unobtrusive.Server", request);
}

static Task SendCommand(IMessageSession messageSession)
{
var commandId = Guid.NewGuid();

var myCommand = new MyCommand
{
CommandId = commandId,
};
Console.WriteLine($"Command sent id: {commandId}");
return messageSession.Send("Samples.Unobtrusive.Server", myCommand);
}
}
19 changes: 19 additions & 0 deletions samples/unobtrusive/Core_10/Client/InputLoopService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using NServiceBus.Routing;

namespace Client
{
public class InputLoopService(IMessageSession messageSession) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await CommandSender.Start(messageSession);

}
}
}
11 changes: 11 additions & 0 deletions samples/unobtrusive/Core_10/Client/MyEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Events;
using Microsoft.Extensions.Logging;

public class MyEventHandler(ILogger<MyEventHandler> logger) : IHandleMessages<MyEvent>
{
public Task Handle(MyEvent message, IMessageHandlerContext context)
{
logger.LogInformation("MyEvent received from server with id:{EventId}", message.EventId);
return Task.CompletedTask;
}
}
20 changes: 20 additions & 0 deletions samples/unobtrusive/Core_10/Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Console.Title = "Client";
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<InputLoopService>();
var endpointConfiguration = new EndpointConfiguration("Samples.Unobtrusive.Client");
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.UseTransport(new LearningTransport());
#pragma warning disable CS0618 // Type or member is obsolete
var dataBus = endpointConfiguration.UseDataBus<FileShareDataBus, SystemJsonDataBusSerializer>();

Check failure on line 12 in samples/unobtrusive/Core_10/Client/Program.cs

View workflow job for this annotation

GitHub Actions / Build samples & snippets

'UseDataBusExtensions.UseDataBus<TDataBusDefinition, TDataBusSerializer>(EndpointConfiguration)' is obsolete: 'The DataBus feature has been released as a dedicated package, 'NServiceBus.ClaimCheck'. The member currently throws a NotImplementedException. Will be removed in version 11.0.0.'

Check failure on line 12 in samples/unobtrusive/Core_10/Client/Program.cs

View workflow job for this annotation

GitHub Actions / Build samples & snippets

'SystemJsonDataBusSerializer' is obsolete: 'The DataBus feature has been released as a dedicated package, 'NServiceBus.ClaimCheck'. Will be removed in version 11.0.0.'

Check failure on line 12 in samples/unobtrusive/Core_10/Client/Program.cs

View workflow job for this annotation

GitHub Actions / Build samples & snippets

'FileShareDataBus' is obsolete: 'The DataBus feature has been released as a dedicated package, 'NServiceBus.ClaimCheck'. Will be removed in version 11.0.0.'
dataBus.BasePath(@"..\..\..\..\DataBusShare\");

Check failure on line 13 in samples/unobtrusive/Core_10/Client/Program.cs

View workflow job for this annotation

GitHub Actions / Build samples & snippets

'ConfigureFileShareDataBus.BasePath(DataBusExtensions<FileShareDataBus>, string)' is obsolete: 'The DataBus feature has been released as a dedicated package, 'NServiceBus.ClaimCheck'. The member currently throws a NotImplementedException. Will be removed in version 11.0.0.'
#pragma warning restore CS0618 // Type or member is obsolete

endpointConfiguration.ApplyCustomConventions();

builder.UseNServiceBus(endpointConfiguration);

await builder.Build().RunAsync();
11 changes: 11 additions & 0 deletions samples/unobtrusive/Core_10/Client/ResponseHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Messages;
using Microsoft.Extensions.Logging;

public class ResponseHandler(ILogger<ResponseHandler> logger) : IHandleMessages<Response>
{
public Task Handle(Response message, IMessageHandlerContext context)
{
logger.LogInformation("Response received from server for request with id:{ResponseId}", message.ResponseId);
return Task.CompletedTask;
}
}
38 changes: 38 additions & 0 deletions samples/unobtrusive/Core_10/ConventionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
public static class ConventionExtensions
{
#region CustomConvention

public static void ApplyCustomConventions(this EndpointConfiguration endpointConfiguration)
{
var conventions = endpointConfiguration.Conventions();

conventions.DefiningCommandsAs(
type =>
type.Namespace != null &&
type.Namespace.EndsWith("Commands"));

conventions.DefiningEventsAs(
type =>
type.Namespace != null &&
type.Namespace.EndsWith("Events"));

conventions.DefiningMessagesAs(
type => type.Namespace == "Messages");
#pragma warning disable CS0618 // Type or member is obsolete
conventions.DefiningDataBusPropertiesAs(

Check failure on line 22 in samples/unobtrusive/Core_10/ConventionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build samples & snippets

'ConventionsBuilder.DefiningDataBusPropertiesAs(Func<PropertyInfo, bool>)' is obsolete: 'The DataBus feature has been released as a dedicated package, 'NServiceBus.ClaimCheck'. The member currently throws a NotImplementedException. Will be removed in version 11.0.0.'

Check failure on line 22 in samples/unobtrusive/Core_10/ConventionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build samples & snippets

'ConventionsBuilder.DefiningDataBusPropertiesAs(Func<PropertyInfo, bool>)' is obsolete: 'The DataBus feature has been released as a dedicated package, 'NServiceBus.ClaimCheck'. The member currently throws a NotImplementedException. Will be removed in version 11.0.0.'
property => property.Name.EndsWith("DataBus"));
#pragma warning restore CS0618 // Type or member is obsolete

conventions.DefiningTimeToBeReceivedAs(
type =>
{
if (type.Name.EndsWith("Expires"))
{
return TimeSpan.FromSeconds(30);
}
return TimeSpan.MaxValue;
});
}

#endregion
}
36 changes: 36 additions & 0 deletions samples/unobtrusive/Core_10/Server/CommandSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Events;

class CommandSender
{
public static async Task Start(IMessageSession messageSession)
{
Console.WriteLine("Press 'E' to publish an event");
Console.WriteLine("Press any other key to exit");

while (true)
{
var key = Console.ReadKey();
Console.WriteLine();

switch (key.Key)
{
case ConsoleKey.E:
await PublishEvent(messageSession);
continue;
}
return;
}
}

static Task PublishEvent(IMessageSession messageSession)
{
var eventId = Guid.NewGuid();

Console.WriteLine($"Event published, id: {eventId}");
var myEvent = new MyEvent
{
EventId = eventId
};
return messageSession.Publish(myEvent);
}
}
17 changes: 17 additions & 0 deletions samples/unobtrusive/Core_10/Server/InputLoopService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;

namespace Server
{
public class InputLoopService(IMessageSession messageSession) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await CommandSender.Start(messageSession);
}
}
}
18 changes: 18 additions & 0 deletions samples/unobtrusive/Core_10/Server/LargeMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Messages;
using Microsoft.Extensions.Logging;

public class LargeMessageHandler(ILogger<LargeMessageHandler> logger) : IHandleMessages<LargeMessage>
{
public Task Handle(LargeMessage message, IMessageHandlerContext context)
{
if (message.LargeDataBus == null)
{
logger.LogInformation("Message [{MessageType}] received, id:{RequestId}", message.GetType(), message.RequestId);
}
else
{
logger.LogInformation("Message [{MessageType}] received, id:{RequestId} and payload {PayloadLength} bytes", message.GetType(), message.RequestId, message.LargeDataBus.Length);
}
return Task.CompletedTask;
}
}
12 changes: 12 additions & 0 deletions samples/unobtrusive/Core_10/Server/MessageThatExpiresHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Messages;
using Microsoft.Extensions.Logging;

public class MessageThatExpiresHandler(ILogger<MessageThatExpiresHandler> logger) : IHandleMessages<MessageThatExpires>
{

public Task Handle(MessageThatExpires message, IMessageHandlerContext context)
{
logger.LogInformation("Message [{MessageType}] received, id: [{RequestId}]", message.GetType(), message.RequestId);
return Task.CompletedTask;
}
}
11 changes: 11 additions & 0 deletions samples/unobtrusive/Core_10/Server/MyCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Commands;
using Microsoft.Extensions.Logging;

public class MyCommandHandler(ILogger<MyCommandHandler> logger) : IHandleMessages<MyCommand>
{
public Task Handle(MyCommand message, IMessageHandlerContext context)
{
logger.LogInformation("Command received, id:{CommandId}", message.CommandId);
return Task.CompletedTask;
}
}
21 changes: 21 additions & 0 deletions samples/unobtrusive/Core_10/Server/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Server;

Console.Title = "Server";
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<InputLoopService>();

var endpointConfiguration = new EndpointConfiguration("Samples.Unobtrusive.Server");
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.UseTransport(new LearningTransport());
#pragma warning disable CS0618 // Type or member is obsolete
endpointConfiguration.UseDataBus<FileShareDataBus, SystemJsonDataBusSerializer>()

Check failure on line 13 in samples/unobtrusive/Core_10/Server/Program.cs

View workflow job for this annotation

GitHub Actions / Build samples & snippets

'ConfigureFileShareDataBus.BasePath(DataBusExtensions<FileShareDataBus>, string)' is obsolete: 'The DataBus feature has been released as a dedicated package, 'NServiceBus.ClaimCheck'. The member currently throws a NotImplementedException. Will be removed in version 11.0.0.'

Check failure on line 13 in samples/unobtrusive/Core_10/Server/Program.cs

View workflow job for this annotation

GitHub Actions / Build samples & snippets

'UseDataBusExtensions.UseDataBus<TDataBusDefinition, TDataBusSerializer>(EndpointConfiguration)' is obsolete: 'The DataBus feature has been released as a dedicated package, 'NServiceBus.ClaimCheck'. The member currently throws a NotImplementedException. Will be removed in version 11.0.0.'

Check failure on line 13 in samples/unobtrusive/Core_10/Server/Program.cs

View workflow job for this annotation

GitHub Actions / Build samples & snippets

'SystemJsonDataBusSerializer' is obsolete: 'The DataBus feature has been released as a dedicated package, 'NServiceBus.ClaimCheck'. Will be removed in version 11.0.0.'

Check failure on line 13 in samples/unobtrusive/Core_10/Server/Program.cs

View workflow job for this annotation

GitHub Actions / Build samples & snippets

'FileShareDataBus' is obsolete: 'The DataBus feature has been released as a dedicated package, 'NServiceBus.ClaimCheck'. Will be removed in version 11.0.0.'
.BasePath(@"..\..\..\..\DataBusShare\");
#pragma warning restore CS0618 // Type or member is obsolete

endpointConfiguration.ApplyCustomConventions();

builder.UseNServiceBus(endpointConfiguration);

await builder.Build().RunAsync();
15 changes: 15 additions & 0 deletions samples/unobtrusive/Core_10/Server/RequestMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Messages;
using Microsoft.Extensions.Logging;
public class RequestMessageHandler(ILogger<RequestMessageHandler> logger) : IHandleMessages<Request>
{
public Task Handle(Request message, IMessageHandlerContext context)
{
logger.LogInformation("Request received with id:{RequestId}", message.RequestId);

var response = new Response
{
ResponseId = message.RequestId
};
return context.Reply(response);
}
}
24 changes: 24 additions & 0 deletions samples/unobtrusive/Core_10/Server/Server.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>preview</LangVersion>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\ConventionExtensions.cs" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions samples/unobtrusive/Core_10/Shared/LargeMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Messages;

using System;

public class LargeMessage
{
public Guid RequestId { get; set; }

public byte[]? LargeDataBus { get; set; }
}
Loading