From 616638f54df943b98508624df1fe6ada63c24670 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Mon, 14 Oct 2024 19:17:45 -0500 Subject: [PATCH] Made some lines a little more concise, added hostname default to DaprDefaults to use when building endpoints. Signed-off-by: Whit Waldo --- .../DaprServiceCollectionExtensions.cs | 12 ++++++++---- src/Shared/DaprDefaults.cs | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs b/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs index 0f2993ab8..5b22a46e0 100644 --- a/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs +++ b/src/Dapr.AspNetCore/DaprServiceCollectionExtensions.cs @@ -115,10 +115,12 @@ internal static string GetHttpEndpoint(IConfiguration? configuration) //Prioritize pulling from IConfiguration with a fallback of pulling from the environment variable directly var httpEndpoint = GetResourceValue(configuration, DaprDefaults.DaprHttpEndpointName); var httpPort = GetResourceValue(configuration, DaprDefaults.DaprHttpPortName); - int? parsedHttpPort = string.IsNullOrWhiteSpace(httpPort) ? null : int.Parse(httpPort); + int? parsedHttpPort = int.TryParse(httpPort, out var port) ? port : null; var endpoint = BuildEndpoint(httpEndpoint, parsedHttpPort); - return string.IsNullOrWhiteSpace(endpoint) ? $"http://localhost:{DaprDefaults.DefaultHttpPort}/" : endpoint; + return string.IsNullOrWhiteSpace(endpoint) + ? $"http://{DaprDefaults.DaprHostName}:{DaprDefaults.DefaultHttpPort}/" + : endpoint; } /// @@ -135,10 +137,12 @@ internal static string GetGrpcEndpoint(IConfiguration? configuration) //Prioritize pulling from IConfiguration with a fallback from pulling from the environment variable directly var grpcEndpoint = GetResourceValue(configuration, DaprDefaults.DaprGrpcEndpointName); var grpcPort = GetResourceValue(configuration, DaprDefaults.DaprGrpcPortName); - int? parsedGrpcPort = string.IsNullOrWhiteSpace(grpcPort) ? null : int.Parse(grpcPort); + int? parsedGrpcPort = int.TryParse(grpcPort, out var port) ? port : null; var endpoint = BuildEndpoint(grpcEndpoint, parsedGrpcPort); - return string.IsNullOrWhiteSpace(endpoint) ? $"http://localhost:{DaprDefaults.DefaultGrpcPort}/" : endpoint; + return string.IsNullOrWhiteSpace(endpoint) + ? $"http://{DaprDefaults.DaprHostName}:{DaprDefaults.DefaultGrpcPort}/" + : endpoint; } /// diff --git a/src/Shared/DaprDefaults.cs b/src/Shared/DaprDefaults.cs index 2208d97fa..bb71115ae 100644 --- a/src/Shared/DaprDefaults.cs +++ b/src/Shared/DaprDefaults.cs @@ -29,6 +29,7 @@ internal static class DaprDefaults public const string DaprGrpcEndpointName = "DAPR_GRPC_ENDPOINT"; public const string DaprGrpcPortName = "DAPR_GRPC_PORT"; + public const string DaprHostName = "localhost"; public const int DefaultHttpPort = 3500; public const int DefaultGrpcPort = 50001;