Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions samples/WebAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IOrchestrationService>(orchestrationService);
builder.Services.AddSingleton<IOrchestrationServiceClient>(orchestrationService);
builder.Services.AddSingleton<TaskHubGrpcServer>();
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 =>
Expand All @@ -25,5 +69,9 @@
});

WebApplication app = builder.Build();

// Map the gRPC sidecar service endpoint
app.MapGrpcService<TaskHubGrpcServer>();

app.MapControllers();
app.Run("http://0.0.0.0:8080");
app.Run();
24 changes: 3 additions & 21 deletions samples/WebAPI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
10 changes: 10 additions & 0 deletions samples/WebAPI/WebAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@
</PropertyGroup>

<ItemGroup>
<!-- Real projects would use package references -->
<!--
<PackageReference Include="Microsoft.DurableTask.Client.Grpc" />
<PackageReference Include="Microsoft.DurableTask.Worker.Grpc" />
-->
<PackageReference Include="Microsoft.DurableTask.Generators" OutputItemType="Analyzer" />
</ItemGroup>

<ItemGroup>
<!-- Using p2p references so we can show latest changes in samples and to include the InProcessTestHost. -->
<ProjectReference Include="$(SrcRoot)Client/Grpc/Client.Grpc.csproj" />
<ProjectReference Include="$(SrcRoot)Worker/Grpc/Worker.Grpc.csproj" />
<ProjectReference Include="$(SrcRoot)InProcessTestHost/InProcessTestHost.csproj" />
</ItemGroup>

</Project>
Loading