From 780d90cca28b19dd33e1eda99d673fef373a4313 Mon Sep 17 00:00:00 2001 From: Andreas D Jung <7018968+coronabytes@users.noreply.github.com> Date: Sun, 28 Jul 2024 18:54:02 +0200 Subject: [PATCH] Create README.md --- README.md | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..8d055c5 --- /dev/null +++ b/README.md @@ -0,0 +1,105 @@ +[![Nuget](https://img.shields.io/nuget/v/Core.ServiceMesh)](https://www.nuget.org/packages/Core.ServiceMesh) +[![Nuget](https://img.shields.io/nuget/dt/Core.ServiceMesh)](https://www.nuget.org/packages/Core.ServiceMesh) + +``` +dotnet add package Core.ServiceMesh +``` +# Service Mesh for ASP.NET Core +- based on https://nats.io +- service request reponse + - strongly typed clients out of the box +- event streaming + - durable and transient consumers + +## Initialization in ASP.NET Core + +```csharp +builder.AddServiceMesh(options => +{ + options.Nats = "nats://localhost:4222"; + options.Assemblies = [typeof(ISomeService).Assembly, typeof(SomeService).Assembly]; +}); +``` + +## Service Interface + +```csharp +[ServiceMesh("someservice")] +public interface ISomeService +{ + Task GetSomeString(int a, string b); + Task CreateSomeObject(); + Task GenericAdd(T a, T b) where T : INumber; +}; +``` + +## Service Implementation + +```csharp +[ServiceMesh("someservice", "someservice")] +public class SomeService(ILogger logger) : ISomeService +{ + public async Task GetSomeString(int a, string b) + { + await Task.Delay(100); + return b + " " + a; + } + + public async Task CreateSomeObject() + { + await Task.Delay(100); + logger.LogInformation(nameof(CreateSomeObject)); + } + + public async Task GenericAdd(T a, T b) where T : INumber + { + await Task.Delay(100); + return a + b; + } +} +``` + +## Service Invocation + +```csharp +public class DevController(ISomeService someService) : ControllerBase +{ + [HttpPost("add-ints")] + public async Task> CreateIntObject([FromQuery] int a = 3, [FromQuery] int b = 5) + { + return await someService.GenericAdd(a, b); + } + + [HttpPost("add-doubles")] + public async Task> CreateDoubleObject([FromQuery] double a = 3.1, [FromQuery] double b = 5.1) + { + return await someService.GenericAdd(a, b); + } +} +``` + +## Events, Streams and Consumers + +```csharp +public record SomeCommand(string Name); + +[DurableConsumer("SomeCommandHandler", "default")] +public class SomeCommandHandler(ILogger logger) : IConsumer +{ + public ValueTask ConsumeAsync(SomeCommand message, CancellationToken token) + { + // do stuff + return ValueTask.CompletedTask; + } +} + +public class DevController(IServiceMesh mesh) : ControllerBase +{ + [HttpPost("publish")] + public async Task Publish([FromQuery] string message) + { + await mesh.PublishAsync(new SomeCommand(message)); + return Ok(); + } +} +```