-
Notifications
You must be signed in to change notification settings - Fork 526
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
NATS Aspire container #1175
NATS Aspire container #1175
Changes from 1 commit
a4dcc11
135a213
2088f5b
7b9690f
732c8f1
6c2d73c
56126f5
35a8972
35f22fe
7d94f67
27ec961
70987ad
b5c2c37
28b2112
47d5268
4d2ca61
7916e6b
27a8609
3e8fc4f
4e27824
ec581ba
f2884de
f52811d
2eb8acd
607f025
f772de8
cec35c9
54d5946
6e255ae
4d37047
ee10f49
b583065
d138aa9
9b950eb
2f77b55
08e26aa
48d6bd3
f6fd1b0
ff2cb10
bf74eda
a40a832
1405623
8eb4d94
4d488e2
4afdad8
1a62a75
3606984
6eefa74
388bd85
f31d49f
2c10732
f98cfa6
0eab699
1f773f9
15ffa21
7836160
d005020
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
@HostAddress = http://localhost:5156 | ||
|
||
POST {{HostAddress}}/stream/ | ||
Content-Type: application/json | ||
|
||
{ | ||
"name": "EVENTS", | ||
"description": "Events Description", | ||
"subjects": ["events.>"], | ||
"max_msgs": 10 | ||
} | ||
|
||
### | ||
|
||
GET {{HostAddress}}/stream/EVENTS | ||
Accept: application/json | ||
|
||
### | ||
|
||
POST {{HostAddress}}/publish/ | ||
Content-Type: application/json | ||
|
||
{ | ||
"subject": "events.mouse.click", | ||
"name": "Mouse Event", | ||
"description": "Mouse Event Description", | ||
"priority": 0.1 | ||
} | ||
|
||
### | ||
|
||
POST {{HostAddress}}/publish/ | ||
Content-Type: application/json | ||
|
||
{ | ||
"subject": "events.key.a", | ||
"name": "Key Event", | ||
"description": "Key Event Description", | ||
"priority": 0.5 | ||
} | ||
|
||
### | ||
|
||
GET {{HostAddress}}/consume/EVENTS | ||
Accept: application/json | ||
|
||
### |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,82 @@ | ||
using System.Text.Json.Serialization; | ||
using NATS.Client.Core; | ||
using NATS.Client.JetStream; | ||
using NATS.Client.JetStream.Models; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
builder.AddServiceDefaults(); | ||
|
||
builder.AddNats("nats"); | ||
builder.AddNats("nats", opts => | ||
{ | ||
var jsonRegistry = new NatsJsonContextSerializerRegistry(AppJsonContext.Default); | ||
return opts with { SerializerRegistry = jsonRegistry }; | ||
Comment on lines
+17
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this required or optional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's optional. required for json serialization. Would you suggests to make it easier for ad hoc JSON serialization? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so, but I don't know enough about your API design to suggest something. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Challenge is our core NuGet packages are AOT ready and for ad-hoc JSON we have an extra package. I can bring NATS ad-hoc JSON package into Aspite.Nats.Client if AOT is not a concern (yet?). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Aspire component packages are intended to be AOT compatible if the associated client library is AOT compatible. For example, we are using the Configuration Binder source generator, so config binding works in native AOT. We have 2 service apps in https://github.com/dotnet/eShop that support being published to native AOT:
So, yes, AOT is a concern for Aspire. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the comments. I'm going to leave this as it is for now since it looks like we're going to need some changes on NATS Client side as well (nats-io/nats.net#417) |
||
}); | ||
|
||
builder.Services.AddSingleton<INatsJSContext>(static provider => | ||
davidfowl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return new NatsJSContextFactory().CreateContext(provider.GetService<INatsConnection>()!); | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
app.MapGet("/", async (INatsConnection nats) => $""" | ||
Hello NATS! | ||
rtt: {await nats.PingAsync()} | ||
{nats.ServerInfo} | ||
"""); | ||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.MapPost("/stream", async (StreamConfig config, INatsJSContext jetStream) => | ||
{ | ||
var stream = await jetStream.CreateStreamAsync(config); | ||
var name = stream.Info.Config.Name; | ||
return Results.Created($"/stream/{name}", name); | ||
}); | ||
|
||
app.MapGet("/stream/{name}", async (string name, INatsJSContext jetStream) => | ||
{ | ||
var stream = await jetStream.GetStreamAsync(name); | ||
return Results.Ok(stream.Info); | ||
}); | ||
|
||
app.MapPost("/publish/", async (AppEvent @event, INatsJSContext jetStream) => | ||
{ | ||
var ack = await jetStream.PublishAsync(@event.Subject, @event); | ||
ack.EnsureSuccess(); | ||
return Results.Created(); | ||
}); | ||
|
||
app.MapGet("/consume/{name}", async (string name, INatsJSContext jetStream) => | ||
{ | ||
var stream = await jetStream.GetStreamAsync(name); | ||
var consumer = await stream.CreateOrderedConsumerAsync(); | ||
|
||
var events = new List<AppEvent>(); | ||
await foreach(var msg in consumer.ConsumeAsync<AppEvent>()) | ||
{ | ||
events.Add(msg.Data!); | ||
|
||
if (msg.Metadata?.NumPending == 0) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
return Results.Ok(events); | ||
}); | ||
|
||
app.Run(); | ||
|
||
public record AppEvent(string Subject, string Name, string Description, decimal Priority); | ||
|
||
[JsonSerializable(typeof(AppEvent))] | ||
[JsonSourceGenerationOptions(PropertyNameCaseInsensitive = true)] | ||
public partial class AppJsonContext : JsonSerializerContext | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var nats = builder.AddNats("nats"); | ||
var nats = builder.AddNats("nats", enableJetStream: true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may wish to add a |
||
|
||
builder.AddProject<Projects.Nats_ApiService>("apiservice") | ||
.WithReference(nats); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this app a little more real? Can it use pubsub as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, let me have a go!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a simple JetStream example since it's easier demonstrate persisted messages using Web API. As for pubsub I suppose we'd need a frontend using SignalR or Blazor. Maybe a simple chat or something?