Skip to content
18 changes: 15 additions & 3 deletions extensions/Worker.Extensions.Rpc/src/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ internal static class ConfigurationExtensions
/// </exception>
public static Uri GetFunctionsHostGrpcUri(this IConfiguration configuration)
{
string uriString = $"http://{configuration["HOST"]}:{configuration["PORT"]}";
if (!Uri.TryCreate(uriString, UriKind.Absolute, out Uri? grpcUri))
Uri? grpcUri;
var functionsUri = configuration["Functions:Worker:HostEndpoint"];
if (functionsUri is not null)
{
throw new InvalidOperationException($"The gRPC channel URI '{uriString}' could not be parsed.");
if (!Uri.TryCreate(functionsUri, UriKind.Absolute, out grpcUri))
{
throw new InvalidOperationException($"The gRPC channel URI '{functionsUri}' could not be parsed.");
}
}
else
{
var uriString = $"http://{configuration["HOST"]}:{configuration["PORT"]}";
if (!Uri.TryCreate(uriString, UriKind.Absolute, out grpcUri))
{
throw new InvalidOperationException($"The gRPC channel URI '{uriString}' could not be parsed.");
}
}

return grpcUri;
Expand Down
2 changes: 2 additions & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@

### Microsoft.Azure.Functions.Worker.Grpc <version>

- Added support for handling the new command line arguments with "functions-" prefix. (#1897)
- Adding optional parameter support (#1868)

31 changes: 28 additions & 3 deletions src/DotNetWorker.Grpc/GrpcServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.Azure.Functions.Worker.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Azure.Functions.Worker.Handlers;

namespace Microsoft.Extensions.DependencyInjection
Expand Down Expand Up @@ -68,12 +67,38 @@ public static IServiceCollection AddGrpc(this IServiceCollection services)
#endif

services.AddOptions<GrpcWorkerStartupOptions>()
.Configure<IConfiguration>((arguments, config) =>
.Configure<IConfiguration>((grpcWorkerStartupOption, config) =>
{
config.Bind(arguments);
grpcWorkerStartupOption.HostEndpoint = GetFunctionsHostGrpcUri(config);
grpcWorkerStartupOption.RequestId = config["Functions:Worker:RequestId"] ?? config["requestId"];
grpcWorkerStartupOption.WorkerId = config["Functions:Worker:WorkerId"] ?? config["workerId"];
grpcWorkerStartupOption.GrpcMaxMessageLength = config.GetValue<int?>("Functions:Worker:GrpcMaxMessageLength", null) ?? config.GetValue<int>("grpcMaxMessageLength");
});

return services;
}

private static Uri GetFunctionsHostGrpcUri(IConfiguration configuration)
{
Uri? grpcUri;
var functionsUri = configuration["Functions:Worker:HostEndpoint"];
if (functionsUri is not null)
{
if (!Uri.TryCreate(functionsUri, UriKind.Absolute, out grpcUri))
{
throw new InvalidOperationException($"The gRPC channel URI '{functionsUri}' could not be parsed.");
}
}
else
{
var uriString = $"http://{configuration["HOST"]}:{configuration["PORT"]}";
if (!Uri.TryCreate(uriString, UriKind.Absolute, out grpcUri))
{
throw new InvalidOperationException($"The gRPC channel URI '{uriString}' could not be parsed.");
}
}

return grpcUri;
}
}
}
6 changes: 3 additions & 3 deletions src/DotNetWorker.Grpc/GrpcStartupOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Licensed under the MIT License. See License.txt in the project root for license information.


using System;

namespace Microsoft.Azure.Functions.Worker
{
internal class GrpcWorkerStartupOptions
{
public string? Host { get; set; }

public int Port { get; set; }
public Uri? HostEndpoint { get; set; }

public string? WorkerId { get; set; }

Expand Down
11 changes: 2 additions & 9 deletions src/DotNetWorker.Grpc/GrpcWorkerClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,8 @@ private async Task StartReaderAsync(IAsyncStreamReader<StreamingMessage> respons

private FunctionRpcClient CreateClient()
{
string uriString = $"http://{_startupOptions.Host}:{_startupOptions.Port}";
if (!Uri.TryCreate(uriString, UriKind.Absolute, out Uri? grpcUri))
{
throw new InvalidOperationException($"The gRPC channel URI '{uriString}' could not be parsed.");
}


#if NET5_0_OR_GREATER
GrpcChannel grpcChannel = GrpcChannel.ForAddress(grpcUri, new GrpcChannelOptions()
GrpcChannel grpcChannel = GrpcChannel.ForAddress(_startupOptions.HostEndpoint!.AbsoluteUri, new GrpcChannelOptions()
{
MaxReceiveMessageSize = _startupOptions.GrpcMaxMessageLength,
MaxSendMessageSize = _startupOptions.GrpcMaxMessageLength,
Expand All @@ -126,7 +119,7 @@ private FunctionRpcClient CreateClient()
new ChannelOption(GrpcCore.ChannelOptions.MaxSendMessageLength, _startupOptions.GrpcMaxMessageLength)
};

GrpcCore.Channel grpcChannel = new GrpcCore.Channel(_startupOptions.Host, _startupOptions.Port, ChannelCredentials.Insecure, options);
GrpcCore.Channel grpcChannel = new GrpcCore.Channel(_startupOptions.HostEndpoint!.Host, _startupOptions.HostEndpoint.Port, ChannelCredentials.Insecure, options);

#endif
return new FunctionRpcClient(grpcChannel);
Expand Down
10 changes: 9 additions & 1 deletion src/DotNetWorker/Hosting/WorkerHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -200,7 +201,14 @@ internal static void RegisterCommandLine(IConfigurationBuilder builder, string[]
cmdLine[i] = $"\"{arg}\"";
}

builder.AddCommandLine(cmdLine);
var switchMappings = new Dictionary<string, string>
{
{ "--functions-uri", "Functions:Worker:HostEndpoint" },
{ "--functions-request-id", "Functions:Worker:RequestId" },
{ "--functions-worker-id", "Functions:Worker:WorkerId" },
{ "--functions-grpc-max-message-length", "Functions:Worker:GrpcMaxMessageLength" },
};
builder.AddCommandLine(cmdLine, switchMappings);
}
}
}