From b4660e243549b9a87e2c4a0347c2d75dd80dc03a Mon Sep 17 00:00:00 2001 From: Oisin Grehan Date: Thu, 17 Oct 2024 17:21:25 -0400 Subject: [PATCH 1/3] replace null check for endPoint with IsAllocated instead --- Aspire.sln | 11 ++++- Directory.Packages.props | 7 ++-- playground/Directory.Packages.props | 6 ++- .../dapr/Dapr.AppHost/Dapr.AppHost.csproj | 1 + playground/dapr/Dapr.AppHost/Program.cs | 5 +++ playground/dapr/ServiceC/DaprServiceC.csproj | 15 +++++++ playground/dapr/ServiceC/Program.cs | 41 +++++++++++++++++++ ...DaprDistributedApplicationLifecycleHook.cs | 12 +++--- 8 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 playground/dapr/ServiceC/DaprServiceC.csproj create mode 100644 playground/dapr/ServiceC/Program.cs diff --git a/Aspire.sln b/Aspire.sln index 19051baa4d1..7972b48f7b1 100644 --- a/Aspire.sln +++ b/Aspire.sln @@ -635,11 +635,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AzureContainerApps.ApiServi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.ProjectTemplates", "src\Aspire.ProjectTemplates\Aspire.ProjectTemplates.csproj", "{D07A8344-C61A-478A-92F0-E9B2DA00D647}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WaitFor.Frontend", "playground\waitfor\WaitFor.Frontend\WaitFor.Frontend.csproj", "{C88BBF0B-7566-48BD-A2BC-05374B1A2B22}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WaitFor.Frontend", "playground\waitfor\WaitFor.Frontend\WaitFor.Frontend.csproj", "{C88BBF0B-7566-48BD-A2BC-05374B1A2B22}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "HealthChecks", "HealthChecks", "{B7345F72-712F-436C-AE18-CAF7CDD4A990}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HealthChecksSandbox.AppHost", "playground\HealthChecks\HealthChecksSandbox.AppHost\HealthChecksSandbox.AppHost.csproj", "{042DD8C6-A26C-4B06-80A1-FE7F8659C5BC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HealthChecksSandbox.AppHost", "playground\HealthChecks\HealthChecksSandbox.AppHost\HealthChecksSandbox.AppHost.csproj", "{042DD8C6-A26C-4B06-80A1-FE7F8659C5BC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DaprServiceC", "playground\dapr\ServiceC\DaprServiceC.csproj", "{B26653B9-439E-4850-A7F8-43C6E5121952}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -1683,6 +1685,10 @@ Global {042DD8C6-A26C-4B06-80A1-FE7F8659C5BC}.Debug|Any CPU.Build.0 = Debug|Any CPU {042DD8C6-A26C-4B06-80A1-FE7F8659C5BC}.Release|Any CPU.ActiveCfg = Release|Any CPU {042DD8C6-A26C-4B06-80A1-FE7F8659C5BC}.Release|Any CPU.Build.0 = Release|Any CPU + {B26653B9-439E-4850-A7F8-43C6E5121952}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B26653B9-439E-4850-A7F8-43C6E5121952}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B26653B9-439E-4850-A7F8-43C6E5121952}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B26653B9-439E-4850-A7F8-43C6E5121952}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1991,6 +1997,7 @@ Global {C88BBF0B-7566-48BD-A2BC-05374B1A2B22} = {3FF3F00C-95C0-46FC-B2BE-A3920C71E393} {B7345F72-712F-436C-AE18-CAF7CDD4A990} = {D173887B-AF42-4576-B9C1-96B9E9B3D9C0} {042DD8C6-A26C-4B06-80A1-FE7F8659C5BC} = {B7345F72-712F-436C-AE18-CAF7CDD4A990} + {B26653B9-439E-4850-A7F8-43C6E5121952} = {57A42144-739E-49A7-BADB-BB8F5F20FA17} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {6DCEDFEC-988E-4CB3-B45B-191EB5086E0C} diff --git a/Directory.Packages.props b/Directory.Packages.props index b55b645f5bb..a908932c024 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -96,7 +96,7 @@ - + @@ -189,11 +189,10 @@ - + - @@ -202,4 +201,4 @@ - + \ No newline at end of file diff --git a/playground/Directory.Packages.props b/playground/Directory.Packages.props index ebc3670efb4..abdaeaf7ab0 100644 --- a/playground/Directory.Packages.props +++ b/playground/Directory.Packages.props @@ -1,5 +1,7 @@ - - + + + + \ No newline at end of file diff --git a/playground/dapr/Dapr.AppHost/Dapr.AppHost.csproj b/playground/dapr/Dapr.AppHost/Dapr.AppHost.csproj index 63a9cb3244d..1e2b1f25293 100644 --- a/playground/dapr/Dapr.AppHost/Dapr.AppHost.csproj +++ b/playground/dapr/Dapr.AppHost/Dapr.AppHost.csproj @@ -19,6 +19,7 @@ + diff --git a/playground/dapr/Dapr.AppHost/Program.cs b/playground/dapr/Dapr.AppHost/Program.cs index dbdf6838095..91db1e4264e 100644 --- a/playground/dapr/Dapr.AppHost/Program.cs +++ b/playground/dapr/Dapr.AppHost/Program.cs @@ -18,6 +18,11 @@ .WithDaprSidecar() .WithReference(pubSub); +// console app with no appPort (sender only) +builder.AddProject("servicec") + .WithDaprSidecar() + .WithReference(pubSub); + #if !SKIP_DASHBOARD_REFERENCE // This project is only added in playground projects to support development/debugging // of the dashboard. It is not required in end developer code. Comment out this code diff --git a/playground/dapr/ServiceC/DaprServiceC.csproj b/playground/dapr/ServiceC/DaprServiceC.csproj new file mode 100644 index 00000000000..9b35e4db9ef --- /dev/null +++ b/playground/dapr/ServiceC/DaprServiceC.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + diff --git a/playground/dapr/ServiceC/Program.cs b/playground/dapr/ServiceC/Program.cs new file mode 100644 index 00000000000..7941e63e064 --- /dev/null +++ b/playground/dapr/ServiceC/Program.cs @@ -0,0 +1,41 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Dapr.Client; +using Test; + +var builder = Host.CreateApplicationBuilder(args); + +var dapr = new DaprClientBuilder() + .Build(); + +builder.Services.AddSingleton(dapr); + +builder.Services.AddHostedService(); + +var app = builder.Build(); + +await app.RunAsync(); + +Console.WriteLine("Goodbye, World!"); + +namespace Test +{ + public sealed class Worker(DaprClient dapr) : BackgroundService + { + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + await dapr.WaitForSidecarAsync(stoppingToken); + + while (!stoppingToken.IsCancellationRequested) + { + var state = await dapr.GetStateAsync( + "statestore", "key", cancellationToken: stoppingToken); + + Console.WriteLine($"State: {state ?? ""}"); + + await Task.Delay(1000, stoppingToken); + } + } + } +} diff --git a/src/Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs b/src/Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs index 087e2a1b51f..417a4a7cda3 100644 --- a/src/Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs +++ b/src/Aspire.Hosting.Dapr/DaprDistributedApplicationLifecycleHook.cs @@ -190,12 +190,10 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell { updatedArgs.AddRange(daprCommandLine.Arguments); var endPoint = GetEndpointReference(sidecarOptions, resource); - if (endPoint is not null) + + if (sidecarOptions?.AppPort is null && endPoint is { appEndpoint.IsAllocated: true }) { - if (endPoint.Value.appEndpoint.IsAllocated && sidecarOptions?.AppPort is null) - { - updatedArgs.AddRange(daprAppPortArg(endPoint.Value.appEndpoint.Port)()); - } + updatedArgs.AddRange(daprAppPortArg(endPoint.Value.appEndpoint.Port)()); } var grpc = daprCli.GetEndpoint("grpc"); @@ -213,11 +211,11 @@ public async Task BeforeStartAsync(DistributedApplicationModel appModel, Cancell updatedArgs.AddRange(daprProfilePortArg(profiling.Property(EndpointProperty.TargetPort))()); } - if (sidecarOptions?.AppChannelAddress is null && endPoint is not null) + if (sidecarOptions?.AppChannelAddress is null && endPoint is { appEndpoint.IsAllocated: true }) { updatedArgs.AddRange(daprAppChannelAddressArg(endPoint.Value.appEndpoint.Host)()); } - if (sidecarOptions?.AppProtocol is null && endPoint is not null) + if (sidecarOptions?.AppProtocol is null && endPoint is { appEndpoint.IsAllocated: true }) { updatedArgs.AddRange(daprAppProtocol(endPoint.Value.protocol)()); } From 47eb2986fa08d5f391e3dcc9949fed5e4e7b57b5 Mon Sep 17 00:00:00 2001 From: Oisin Grehan Date: Thu, 17 Oct 2024 17:34:19 -0400 Subject: [PATCH 2/3] fix service c apphost refs --- playground/dapr/Dapr.AppHost/Program.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/playground/dapr/Dapr.AppHost/Program.cs b/playground/dapr/Dapr.AppHost/Program.cs index 91db1e4264e..97856186bcd 100644 --- a/playground/dapr/Dapr.AppHost/Program.cs +++ b/playground/dapr/Dapr.AppHost/Program.cs @@ -20,8 +20,7 @@ // console app with no appPort (sender only) builder.AddProject("servicec") - .WithDaprSidecar() - .WithReference(pubSub); + .WithDaprSidecar(); #if !SKIP_DASHBOARD_REFERENCE // This project is only added in playground projects to support development/debugging From dddbdf5fea41bfd2715820258e0e697fda2789f7 Mon Sep 17 00:00:00 2001 From: Oisin Grehan Date: Fri, 18 Oct 2024 09:15:56 -0400 Subject: [PATCH 3/3] switch out console for logger; update cache key name for consistency in servicec --- playground/dapr/Dapr.AppHost/Program.cs | 1 + playground/dapr/ServiceC/Program.cs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/playground/dapr/Dapr.AppHost/Program.cs b/playground/dapr/Dapr.AppHost/Program.cs index 97856186bcd..24fb6d9b621 100644 --- a/playground/dapr/Dapr.AppHost/Program.cs +++ b/playground/dapr/Dapr.AppHost/Program.cs @@ -20,6 +20,7 @@ // console app with no appPort (sender only) builder.AddProject("servicec") + .WithReference(stateStore) .WithDaprSidecar(); #if !SKIP_DASHBOARD_REFERENCE diff --git a/playground/dapr/ServiceC/Program.cs b/playground/dapr/ServiceC/Program.cs index 7941e63e064..c5f641494c0 100644 --- a/playground/dapr/ServiceC/Program.cs +++ b/playground/dapr/ServiceC/Program.cs @@ -21,7 +21,7 @@ namespace Test { - public sealed class Worker(DaprClient dapr) : BackgroundService + public sealed class Worker(ILogger logger, DaprClient dapr) : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { @@ -30,9 +30,9 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) while (!stoppingToken.IsCancellationRequested) { var state = await dapr.GetStateAsync( - "statestore", "key", cancellationToken: stoppingToken); + "statestore", "cache", cancellationToken: stoppingToken); - Console.WriteLine($"State: {state ?? ""}"); + logger.LogInformation("State: {0}", state ?? ""); await Task.Delay(1000, stoppingToken); }