Skip to content
Merged
23 changes: 19 additions & 4 deletions extensions/Worker.Extensions.Rpc/src/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

namespace Microsoft.Azure.Functions.Worker.Extensions.Rpc
{
internal static class ConfigurationExtensions
/// <summary>
/// contains extension methods for IConfiguration.
/// </summary>
public static class ConfigurationExtensions
Comment thread
kshyju marked this conversation as resolved.
Outdated
{
/// <summary>
/// Gets the functions host gRPC address from <see cref="IConfiguration"/>.
Expand All @@ -18,10 +21,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)

3 changes: 2 additions & 1 deletion src/DotNetWorker.Grpc/DotNetWorker.Grpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
we use the legacy Grpc.Core package instead, due to limitations with the current
Grpc.Net.Client implementation-->
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Grpc.Core" Version="2.45.0" />
<PackageReference Include="Grpc.Core" Version="2.46.6" />
Comment thread
kshyju marked this conversation as resolved.
Outdated
<Compile Remove="NativeHostIntegration/**" />
</ItemGroup>

Expand All @@ -55,6 +55,7 @@

<ItemGroup>
<ProjectReference Include="..\..\extensions\Worker.Extensions.Abstractions\src\Worker.Extensions.Abstractions.csproj" />
<ProjectReference Include="..\..\extensions\Worker.Extensions.Rpc\src\Worker.Extensions.Rpc.csproj" />
Comment thread
kshyju marked this conversation as resolved.
Outdated
<ProjectReference Include="..\DotNetWorker.Core\DotNetWorker.Core.csproj" />
</ItemGroup>

Expand Down
9 changes: 6 additions & 3 deletions src/DotNetWorker.Grpc/GrpcServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
using Microsoft.Azure.Functions.Worker.Logging;
using Microsoft.Azure.Functions.Worker.Grpc;
using Microsoft.Azure.Functions.Worker.Diagnostics;
using Microsoft.Azure.Functions.Worker.Extensions.Rpc;
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,9 +68,12 @@ public static IServiceCollection AddGrpc(this IServiceCollection services)
#endif

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

return services;
Expand Down
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);
}
}
}