diff --git a/samples/WebAPI/Program.cs b/samples/WebAPI/Program.cs index 111413ee..0bfdf968 100644 --- a/samples/WebAPI/Program.cs +++ b/samples/WebAPI/Program.cs @@ -2,20 +2,64 @@ // Licensed under the MIT License. using System.Text.Json.Serialization; +using DurableTask.Core; +using Grpc.Net.Client; +using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.DurableTask; using Microsoft.DurableTask.Client; +using Microsoft.DurableTask.Testing.Sidecar; +using Microsoft.DurableTask.Testing.Sidecar.Grpc; using Microsoft.DurableTask.Worker; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); -// Add all the generated tasks +// The REST API will listen on port 8080 (HTTP/1.1) and the gRPC sidecar will listen on port 8081 (HTTP/2) +int restPort = 8080; +int grpcPort = 8081; + +// Configure Kestrel with two endpoints: +// - Port 8080 for REST API (HTTP/1.1) +// - Port 8081 for gRPC sidecar (HTTP/2) +builder.WebHost.ConfigureKestrel(options => +{ + options.ListenAnyIP(restPort, listenOptions => + { + listenOptions.Protocols = HttpProtocols.Http1; + }); + options.ListenAnyIP(grpcPort, listenOptions => + { + listenOptions.Protocols = HttpProtocols.Http2; + }); +}); + +// Create the in-memory orchestration service that will store all orchestration state. +// This eliminates the need for an external gRPC sidecar. +InMemoryOrchestrationService orchestrationService = new(); + +// Register the in-memory orchestration service for the gRPC sidecar +builder.Services.AddSingleton(orchestrationService); +builder.Services.AddSingleton(orchestrationService); +builder.Services.AddSingleton(); +builder.Services.AddGrpc(); + +// Create the gRPC channel that will connect to the embedded sidecar on the gRPC port. +// Note: GrpcChannel.ForAddress() doesn't establish a connection until a call is made, +// and the DurableTask worker starts making calls after the host is fully started. +GrpcChannel grpcChannel = GrpcChannel.ForAddress($"http://localhost:{grpcPort}"); + +// Add all the generated tasks and configure worker to use the embedded gRPC sidecar builder.Services.AddDurableTaskWorker(builder => { builder.AddTasks(r => r.AddAllGeneratedTasks()); - builder.UseGrpc(); + builder.UseGrpc(grpcChannel); }); -builder.Services.AddDurableTaskClient(b => b.UseGrpc()); +// Configure the durable task client to use the embedded gRPC sidecar +builder.Services.AddDurableTaskClient(builder => +{ + builder.UseGrpc(grpcChannel); + builder.RegisterDirectly(); +}); // Configure the HTTP request pipeline. builder.Services.AddControllers().AddJsonOptions(options => @@ -25,5 +69,9 @@ }); WebApplication app = builder.Build(); + +// Map the gRPC sidecar service endpoint +app.MapGrpcService(); + app.MapControllers(); -app.Run("http://0.0.0.0:8080"); +app.Run(); diff --git a/samples/WebAPI/Properties/launchSettings.json b/samples/WebAPI/Properties/launchSettings.json index e119b069..2d6964b5 100644 --- a/samples/WebAPI/Properties/launchSettings.json +++ b/samples/WebAPI/Properties/launchSettings.json @@ -1,32 +1,14 @@ { "$schema": "https://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:47697", - "sslPort": 44371 - } - }, "profiles": { "WebAPI": { "commandName": "Project", - "launchBrowser": true, - "launchUrl": "weatherforecast", + "launchBrowser": false, + "applicationUrl": "http://localhost:8080", "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development", - "DURABLETASK_SIDECAR_ADDRESS": "http://localhost:4001" + "ASPNETCORE_ENVIRONMENT": "Development" }, - "applicationUrl": "https://localhost:7217;http://localhost:5217", "dotnetRunMessages": true - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "weatherforecast", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } } } } \ No newline at end of file diff --git a/samples/WebAPI/WebAPI.csproj b/samples/WebAPI/WebAPI.csproj index 84680817..d101c47d 100644 --- a/samples/WebAPI/WebAPI.csproj +++ b/samples/WebAPI/WebAPI.csproj @@ -9,9 +9,19 @@ + + + + + + + + +