From daaeaec3ff5dbf3f20e4bbed44947bb5213303ee Mon Sep 17 00:00:00 2001 From: Ryan Lettieri Date: Tue, 5 Sep 2023 18:36:28 -0600 Subject: [PATCH] Potential fix for grpc workflow issue Signed-off-by: Ryan Lettieri --- .../WorkflowServiceCollectionExtensions.cs | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs b/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs index 0c63e0a49..d036a9d7e 100644 --- a/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs +++ b/src/Dapr.Workflow/WorkflowServiceCollectionExtensions.cs @@ -62,10 +62,9 @@ public static IServiceCollection AddDaprWorkflow( var apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN"); if (!string.IsNullOrEmpty(apiToken)) { - // serviceCollection.ConfigureDurableGrpcClient(apiToken); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken); - builder.UseGrpc(CreateChannel(client)); + builder.UseGrpc(CreateChannel(address, client)); } else { @@ -102,10 +101,9 @@ public static IServiceCollection AddDaprWorkflowClient(this IServiceCollection s var apiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN"); if (!string.IsNullOrEmpty(apiToken)) { - // services.ConfigureDurableGrpcClient(apiToken); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Dapr-Api-Token", apiToken); - builder.UseGrpc(CreateChannel(client)); + builder.UseGrpc(CreateChannel(address, client)); } else { @@ -153,19 +151,30 @@ static bool TryGetGrpcAddress(out string address) return false; } - static GrpcChannel CreateChannel(HttpClient client) + static GrpcChannel CreateChannel(string address, HttpClient client) { - var address = "localhost"; + + var daprEndpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT"); + if (!String.IsNullOrEmpty(daprEndpoint)) { + GrpcChannelOptions options1 = new() { HttpClient = client}; + return GrpcChannel.ForAddress(address, options1); + } + var daprPortStr = Environment.GetEnvironmentVariable("DAPR_GRPC_PORT"); if (int.TryParse(daprPortStr, out int daprGrpcPort)) { - // There is a bug in the Durable Task SDK that requires us to change the format of the address - // depending on the version of .NET that we're targeting. For now, we work around this manually. -#if NET6_0_OR_GREATER - address = $"http://localhost:{daprGrpcPort}"; -#else - address = $"localhost:{daprGrpcPort}"; -#endif + // If there is no address passed in, we default to localhost + if (String.IsNullOrEmpty(address)) + { + // There is a bug in the Durable Task SDK that requires us to change the format of the address + // depending on the version of .NET that we're targeting. For now, we work around this manually. + #if NET6_0_OR_GREATER + address = $"http://localhost:{daprGrpcPort}"; + #else + address = $"localhost:{daprGrpcPort}"; + #endif + } + } GrpcChannelOptions options = new() { HttpClient = client}; return GrpcChannel.ForAddress(address, options);