-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a052a1a
commit 780d90c
Showing
1 changed file
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string> GetSomeString(int a, string b); | ||
Task CreateSomeObject(); | ||
Task<T> GenericAdd<T>(T a, T b) where T : INumber<T>; | ||
}; | ||
``` | ||
|
||
## Service Implementation | ||
|
||
```csharp | ||
[ServiceMesh("someservice", "someservice")] | ||
public class SomeService(ILogger<SomeService> logger) : ISomeService | ||
{ | ||
public async Task<string> 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<T> GenericAdd<T>(T a, T b) where T : INumber<T> | ||
{ | ||
await Task.Delay(100); | ||
return a + b; | ||
} | ||
} | ||
``` | ||
|
||
## Service Invocation | ||
|
||
```csharp | ||
public class DevController(ISomeService someService) : ControllerBase | ||
{ | ||
[HttpPost("add-ints")] | ||
public async Task<ActionResult<int>> CreateIntObject([FromQuery] int a = 3, [FromQuery] int b = 5) | ||
{ | ||
return await someService.GenericAdd(a, b); | ||
} | ||
|
||
[HttpPost("add-doubles")] | ||
public async Task<ActionResult<double>> 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<SomeCommandHandler> logger) : IConsumer<SomeCommand> | ||
{ | ||
public ValueTask ConsumeAsync(SomeCommand message, CancellationToken token) | ||
{ | ||
// do stuff | ||
return ValueTask.CompletedTask; | ||
} | ||
} | ||
|
||
public class DevController(IServiceMesh mesh) : ControllerBase | ||
{ | ||
[HttpPost("publish")] | ||
public async Task<IActionResult> Publish([FromQuery] string message) | ||
{ | ||
await mesh.PublishAsync(new SomeCommand(message)); | ||
return Ok(); | ||
} | ||
} | ||
``` |