A small .NET helper library that makes it easy to send and receive Protocol Buffers messages over UDP.
It wraps UdpClient and Google.Protobuf behind a simple API, so you can focus on your message contracts instead of byte arrays, ports and parsing.
Common package includes UdpService which is necessary to Send protobuf messages over UDP.
NuGet link: ProtobufOverUdp.Common
dotnet add package ProtobufOverUdp.CommonListener package includes UdpListener and interface IUdpMessageHandler which is necessary to Receive protobuf messages over UDP and handle them properly.
NuGet link: ProtobufOverUdp.Listener
dotnet add package ProtobufOverUdp.ListenerTo send protobuf messages over UDP, download ProtobufOverUdp.Common NuGet package, then add necessary services in your Program.cs
builder.Services.AddUdpService(builder.Configuration);Now, you can inject IUdpService and send protobuf messages via UDP this way:
await udpService.SendMessageAsync(message, request.IpAddress, request.Port);If you have the same target IP address and port everywhere, you can specify them this way:
builder.Services.AddUdpService(builder.Configuration, opt =>
{
opt.DestinationIpAddress = "127.0.0.1";
opt.DestinationPort = 5123;
});Now, you can simply use the method this way:
await udpService.SendMessageAsync(message);To receive protobuf messages over UDP, download ProtobufOverUdp.Listener NuGet package, then add necessary services in your Program.cs
builder.Services.AddUdpService(context.Configuration);
builder.Services.AddUdpListener();Now, for each protobuf type you should have its own UdpMessageHandler. Implement them in a similar way (Notification here is your message):
public sealed class NotificationUdpMessageHandler(ILogger<StatusUdpMessageHandler> logger) : IUdpMessageHandler<Notification>
{
public Task HandleAsync(Notification message, CancellationToken token)
{
if (logger.IsEnabled(LogLevel.Information))
{
logger.LogInformation("Received notification {Id} {Text}; Current Time: {CurrentTime}.", message.Id,
message.Text, DateTimeOffset.UtcNow);
}
return Task.CompletedTask;
}
}Finally, register them in DI:
builder.Services.AddUdpMessageHandler<Notification, NotificationUdpMessageHandler>();Now, your application will listen to coming UDP messages and handle them properly.
There are ListenerDemo and PublisherDemo projects. You can run them to see how this application works.
dotnet run --project .\src\ListenerDemo\ListenerDemo.csproj --launch-profile "ListenerDemo"dotnet run --project .\src\PublisherDemo\PublisherDemo.csproj --launch-profile "http"Now, you can use Postman collection (download it from PostmanCollections folder or from this link)