Skip to content
19 changes: 16 additions & 3 deletions extensions/Worker.Extensions.Rpc/src/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,28 @@ internal static class ConfigurationExtensions
/// </summary>
/// <param name="configuration">The configuration to get the host URI from.</param>
/// <returns>The URI representing the functions host.</returns>
/// <exception cref="UriFormatException">If <paramref name="configuration"/> contain a value for 'functions-uri', but is not a valid URL.</exception>
/// <exception cref="InvalidOperationException">
/// If <paramref name="configuration"/> does not contain the required values.
/// </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-uri"];
Comment thread
kshyju marked this conversation as resolved.
Outdated
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 UriFormatException($"'{functionsUri}' is not a valid value for 'functions-uri'. Value should be a valid URL.");
Comment thread
kshyju marked this conversation as resolved.
Outdated
}
}
else
{
string 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)

26 changes: 23 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,9 +67,30 @@ 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);
Uri? grpcUri;
var functionsUri = config["functions-uri"];
Comment thread
kshyju marked this conversation as resolved.
Outdated
if (functionsUri is not null)
{
if (!Uri.TryCreate(functionsUri, UriKind.Absolute, out grpcUri))
{
throw new UriFormatException($"'{functionsUri}' is not a valid value for 'functions-uri'. Value should be a valid URL.");
}
}
else
{
var uriString = $"http://{config["HOST"]}:{config["PORT"]}";
if (!Uri.TryCreate(uriString, UriKind.Absolute, out grpcUri))
{
throw new InvalidOperationException($"The gRPC channel URI '{uriString}' could not be parsed.");
}
}

grpcWorkerStartupOption.Uri = grpcUri;
grpcWorkerStartupOption.RequestId = config["functions-request-id"] ?? config["requestId"];
grpcWorkerStartupOption.WorkerId = config["functions-worker-id"] ?? config["workerId"];
grpcWorkerStartupOption.GrpcMaxMessageLength = config.GetValue<int?>("functions-grpc-max-message-length", 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? Uri { get; set; }
Comment thread
kshyju marked this conversation as resolved.
Outdated

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.Uri!.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.Uri.Host, _startupOptions.Uri.Port, ChannelCredentials.Insecure, options);

#endif
return new FunctionRpcClient(grpcChannel);
Expand Down