Skip to content

Commit

Permalink
Create README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
coronabytes authored Jul 28, 2024
1 parent a052a1a commit 780d90c
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions README.md
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();
}
}
```

0 comments on commit 780d90c

Please sign in to comment.