diff --git a/src/DotNetWorker.Grpc/DotNetWorker.Grpc.csproj b/src/DotNetWorker.Grpc/DotNetWorker.Grpc.csproj index b3a364ff4..5fef991e4 100644 --- a/src/DotNetWorker.Grpc/DotNetWorker.Grpc.csproj +++ b/src/DotNetWorker.Grpc/DotNetWorker.Grpc.csproj @@ -8,7 +8,7 @@ Microsoft.Azure.Functions.Worker.Grpc Microsoft.Azure.Functions.Worker.Grpc true - 2 + 3 @@ -39,8 +39,4 @@ - - - - diff --git a/src/DotNetWorker.Grpc/GrpcServiceCollectionExtensions.cs b/src/DotNetWorker.Grpc/GrpcServiceCollectionExtensions.cs index f63fae996..61e54cf6c 100644 --- a/src/DotNetWorker.Grpc/GrpcServiceCollectionExtensions.cs +++ b/src/DotNetWorker.Grpc/GrpcServiceCollectionExtensions.cs @@ -55,7 +55,13 @@ public static IServiceCollection AddGrpc(this IServiceCollection services) GrpcWorkerStartupOptions arguments = argumentsOptions.Value; - GrpcChannel grpcChannel = GrpcChannel.ForAddress($"http://{arguments.Host}:{arguments.Port}", new GrpcChannelOptions() + string uriString = $"http://{arguments.Host}:{arguments.Port}"; + if (!Uri.TryCreate(uriString, UriKind.Absolute, out Uri? grpcUri)) + { + throw new InvalidOperationException($"The gRPC channel URI '{uriString}' could not be parsed."); + } + + GrpcChannel grpcChannel = GrpcChannel.ForAddress(grpcUri, new GrpcChannelOptions() { MaxReceiveMessageSize = arguments.GrpcMaxMessageLength, MaxSendMessageSize = arguments.GrpcMaxMessageLength, diff --git a/src/DotNetWorker/DotNetWorker.csproj b/src/DotNetWorker/DotNetWorker.csproj index be1f49781..cb3cf6cc8 100644 --- a/src/DotNetWorker/DotNetWorker.csproj +++ b/src/DotNetWorker/DotNetWorker.csproj @@ -8,7 +8,7 @@ Microsoft.Azure.Functions.Worker Microsoft.Azure.Functions.Worker true - 3 + 4 diff --git a/src/DotNetWorker/Hosting/WorkerHostBuilderExtensions.cs b/src/DotNetWorker/Hosting/WorkerHostBuilderExtensions.cs index e6d928faf..122fc3c04 100644 --- a/src/DotNetWorker/Hosting/WorkerHostBuilderExtensions.cs +++ b/src/DotNetWorker/Hosting/WorkerHostBuilderExtensions.cs @@ -154,10 +154,24 @@ public static IHostBuilder ConfigureFunctionsWorkerDefaults(this IHostBuilder bu internal static void RegisterCommandLine(IConfigurationBuilder builder, string[] cmdLine) { - if (cmdLine.Length > 0 && - !cmdLine[0].StartsWith("--")) + // If either of the first two arguments do not begin with '--', wrap them in + // quotes. On Linux, either of these first two arguments can be the path to the + // assembly, which begins with a '/' and is interpreted as a switch. + for (int i = 0; i <= 1; i++) { - cmdLine[0] = $"\"{cmdLine[0]}\""; + if (cmdLine.Length <= i) + { + break; + } + + string arg = cmdLine[i]; + + if (arg.StartsWith("--")) + { + break; + } + + cmdLine[i] = $"\"{arg}\""; } builder.AddCommandLine(cmdLine); diff --git a/test/DotNetWorkerTests/WorkerHostBuilderExtensionsTests.cs b/test/DotNetWorkerTests/WorkerHostBuilderExtensionsTests.cs index 8b5500980..abcab2c10 100644 --- a/test/DotNetWorkerTests/WorkerHostBuilderExtensionsTests.cs +++ b/test/DotNetWorkerTests/WorkerHostBuilderExtensionsTests.cs @@ -1,40 +1,44 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. -using System.Collections.Generic; +using System; +using System.Linq; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Configuration.EnvironmentVariables; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Xunit; -using System.Linq; -using Microsoft.Extensions.Configuration.EnvironmentVariables; -using System; namespace Microsoft.Azure.Functions.Worker.Tests { public class WorkerHostBuilderExtensionsTests { [Theory] - [InlineData("/home/usr/", "\"/home/usr/\"")] - [InlineData(null, "--host")] - public void QuoteFirstArg(string firstArg, string expected) + [InlineData("--host", "127.0.0.1", "--port", "45040")] + [InlineData("/home/usr/a.dll", "--host", "127.0.0.1", "--port", "45040")] + [InlineData("/home/usr/a.dll", "/home/usr/a.dll", "--host", "127.0.0.1", "--port", "45040")] + public void QuoteFirstArg(params string[] args) { - var cmdLineList = new List { "--host", "127.0.0.1" }; + var configBuilder = new ConfigurationBuilder(); + WorkerHostBuilderExtensions.RegisterCommandLine(configBuilder, args); - if (firstArg != null) - { - cmdLineList.Insert(0, firstArg); - } + var config = configBuilder.Build(); - var cmdLine = cmdLineList.ToArray(); + Assert.Equal("127.0.0.1", config["host"]); + } - var configBuilder = new ConfigurationBuilder(); - WorkerHostBuilderExtensions.RegisterCommandLine(configBuilder, cmdLine); + [Theory] + [InlineData(0)] + [InlineData(1)] + public void RegisterCommandLine_NoArgs(int count) + { + var args = Enumerable.Repeat("test", count).ToArray(); - Assert.Equal(expected, cmdLine[0]); + var configBuilder = new ConfigurationBuilder(); + WorkerHostBuilderExtensions.RegisterCommandLine(configBuilder, args); - var config = configBuilder.Build(); - Assert.Equal("127.0.0.1", config["host"]); + // Ensures we don't throw an IndexOutOfRangeException; no assert necessary. + configBuilder.Build(); } [Fact]