From c6ae676695b2dd4ea99f2cd8be702c69a7de4b42 Mon Sep 17 00:00:00 2001 From: Brett Samblanet Date: Wed, 3 Feb 2021 08:46:19 -0800 Subject: [PATCH 1/3] fixing nullable warnings --- .../DefaultFunctionExecutionContext.cs | 19 +++++++-------- .../Context/FunctionExecutionContext.cs | 4 ++-- .../Converters/Converter/ConverterContext.cs | 4 ++-- .../Converter/DefaultConverterContext.cs | 2 +- .../Converters/ExactMatchConverter.cs | 4 ++-- src/DotNetWorker/DefaultOutputBinding.cs | 13 +++++----- .../DefaultFunctionDefinitionFactory.cs | 11 +++++++-- .../Definition/FunctionMetadata.cs | 17 +++++-------- .../Definition/GrpcFunctionMetadata.cs | 24 +++++++++++++++++++ src/DotNetWorker/IFunctionActivator.cs | 2 +- .../Invocation/DefaultFunctionExecutor.cs | 13 +++++++--- .../Invocation/DefaultFunctionInvoker.cs | 6 ++--- .../Invocation/IFunctionInvoker.cs | 4 ++-- src/DotNetWorker/Invocation/IMethodInvoker.cs | 4 ++-- src/DotNetWorker/OutputBinding.cs | 6 ++--- src/DotNetWorker/RpcExtensions.cs | 16 +++---------- test/DotNetWorkerTests/Book.cs | 8 +++---- .../Converters/JsonPocoConverterTests.cs | 4 ++-- .../DefaultFunctionInvokerTests.cs | 5 ++-- test/DotNetWorkerTests/FunctionBrokerTests.cs | 4 ++-- .../TestFunctionExecutionContext.cs | 4 ++-- .../DotNetWorkerTests/TestFunctionMetadata.cs | 13 ++++++++++ 22 files changed, 110 insertions(+), 77 deletions(-) create mode 100644 src/DotNetWorker/Definition/GrpcFunctionMetadata.cs create mode 100644 test/DotNetWorkerTests/TestFunctionMetadata.cs diff --git a/src/DotNetWorker/Context/DefaultFunctionExecutionContext.cs b/src/DotNetWorker/Context/DefaultFunctionExecutionContext.cs index 558f8d287..c6c541c46 100644 --- a/src/DotNetWorker/Context/DefaultFunctionExecutionContext.cs +++ b/src/DotNetWorker/Context/DefaultFunctionExecutionContext.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System; @@ -20,9 +20,9 @@ internal class DefaultFunctionExecutionContext : FunctionExecutionContext, IDisp public DefaultFunctionExecutionContext(IServiceScopeFactory serviceScopeFactory, FunctionInvocation invocation, FunctionDefinition definition) { - _serviceScopeFactory = serviceScopeFactory; - Invocation = invocation; - FunctionDefinition = definition; + _serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory)); + Invocation = invocation ?? throw new ArgumentNullException(nameof(invocation)); + FunctionDefinition = definition ?? throw new ArgumentNullException(nameof(definition)); OutputBindings = new Dictionary(); } @@ -32,7 +32,7 @@ public DefaultFunctionExecutionContext(IServiceScopeFactory serviceScopeFactory, public override object? InvocationResult { get; set; } - public override InvocationLogger Logger { get; set; } + public override InvocationLogger? Logger { get; set; } public override IDictionary OutputBindings { get; } @@ -42,19 +42,16 @@ public override IServiceProvider InstanceServices { get { - if (_instanceServicesScope == null && _serviceScopeFactory != null) + if (_instanceServicesScope == null) { _instanceServicesScope = _serviceScopeFactory.CreateScope(); _instanceServices = _instanceServicesScope.ServiceProvider; } - return _instanceServices; + return _instanceServices!; } - set - { - _instanceServices = value; - } + set => _instanceServices = value; } public virtual void Dispose() diff --git a/src/DotNetWorker/Context/FunctionExecutionContext.cs b/src/DotNetWorker/Context/FunctionExecutionContext.cs index aa20389ac..78bf9b7bf 100644 --- a/src/DotNetWorker/Context/FunctionExecutionContext.cs +++ b/src/DotNetWorker/Context/FunctionExecutionContext.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System; @@ -18,7 +18,7 @@ public abstract class FunctionExecutionContext public abstract object? InvocationResult { get; set; } - public abstract InvocationLogger Logger { get; set; } + public abstract InvocationLogger? Logger { get; set; } // TODO: Double-check previous projects for layout of FunctionInvocation, Bindings, etc public abstract IDictionary OutputBindings { get; } diff --git a/src/DotNetWorker/Converters/Converter/ConverterContext.cs b/src/DotNetWorker/Converters/Converter/ConverterContext.cs index 7584c0144..f613ef489 100644 --- a/src/DotNetWorker/Converters/Converter/ConverterContext.cs +++ b/src/DotNetWorker/Converters/Converter/ConverterContext.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using Microsoft.Azure.Functions.Worker.Definition; @@ -10,7 +10,7 @@ internal abstract class ConverterContext { public abstract FunctionParameter Parameter { get; set; } - public abstract object Source { get; set; } + public abstract object? Source { get; set; } public abstract FunctionExecutionContext ExecutionContext { get; set; } } diff --git a/src/DotNetWorker/Converters/Converter/DefaultConverterContext.cs b/src/DotNetWorker/Converters/Converter/DefaultConverterContext.cs index 070f2442c..014153ba6 100644 --- a/src/DotNetWorker/Converters/Converter/DefaultConverterContext.cs +++ b/src/DotNetWorker/Converters/Converter/DefaultConverterContext.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using Microsoft.Azure.Functions.Worker.Definition; diff --git a/src/DotNetWorker/Converters/ExactMatchConverter.cs b/src/DotNetWorker/Converters/ExactMatchConverter.cs index 25f38d680..077c69111 100644 --- a/src/DotNetWorker/Converters/ExactMatchConverter.cs +++ b/src/DotNetWorker/Converters/ExactMatchConverter.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. namespace Microsoft.Azure.Functions.Worker.Converters @@ -7,7 +7,7 @@ internal class ExactMatchConverter : IConverter { public bool TryConvert(ConverterContext context, out object? target) { - if (context.Source.GetType() == context.Parameter.Type) + if (context.Source?.GetType() == context.Parameter.Type) { target = context.Source; return true; diff --git a/src/DotNetWorker/DefaultOutputBinding.cs b/src/DotNetWorker/DefaultOutputBinding.cs index 92153b202..41fa5a2a5 100644 --- a/src/DotNetWorker/DefaultOutputBinding.cs +++ b/src/DotNetWorker/DefaultOutputBinding.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -9,22 +9,23 @@ namespace Microsoft.Azure.Functions.Worker internal class DefaultOutputBinding : OutputBinding { private readonly FunctionParameter _param; - private readonly IDictionary _outputBindings; + private readonly IDictionary _outputBindings; - public DefaultOutputBinding(FunctionParameter param, IDictionary outputBindings) + public DefaultOutputBinding(FunctionParameter param, IDictionary outputBindings) { _param = param; _outputBindings = outputBindings; } - public override void SetValue(T value) + public override void SetValue(T? value) { _outputBindings[_param.Name] = value; } - internal override T GetValue() + internal override T? GetValue() { - return (T)_outputBindings[_param.Name]; + var value = _outputBindings[_param.Name]; + return (T)value; } } } diff --git a/src/DotNetWorker/Definition/DefaultFunctionDefinitionFactory.cs b/src/DotNetWorker/Definition/DefaultFunctionDefinitionFactory.cs index e8707570d..8ac42dfaa 100644 --- a/src/DotNetWorker/Definition/DefaultFunctionDefinitionFactory.cs +++ b/src/DotNetWorker/Definition/DefaultFunctionDefinitionFactory.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System; @@ -54,9 +54,16 @@ public FunctionDefinition Create(FunctionLoadRequest request) MethodInfo? methodInfo = functionType?.GetMethod(methodName); + if (methodInfo == null) + { + throw new InvalidOperationException($"Method '{methodName}' specified in {nameof(FunctionMetadata.EntryPoint)} was not found. This function cannot be created."); + } + IFunctionInvoker invoker = _functionInvokerFactory.Create(methodInfo); - IEnumerable parameters = methodInfo.GetParameters().Select(p => new FunctionParameter(p.Name, p.ParameterType)); + IEnumerable parameters = methodInfo.GetParameters() + .Where(p => p.Name != null) + .Select(p => new FunctionParameter(p.Name!, p.ParameterType)); return new DefaultFunctionDefinition(metadata, invoker, parameters); } diff --git a/src/DotNetWorker/Definition/FunctionMetadata.cs b/src/DotNetWorker/Definition/FunctionMetadata.cs index 4cf5ac073..8662acf52 100644 --- a/src/DotNetWorker/Definition/FunctionMetadata.cs +++ b/src/DotNetWorker/Definition/FunctionMetadata.cs @@ -1,21 +1,16 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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.Immutable; -using System.Reflection; - namespace Microsoft.Azure.Functions.Worker { - public class FunctionMetadata + public abstract class FunctionMetadata { - public string? PathToAssembly { get; set; } - - public string? EntryPoint { get; set; } + public abstract string PathToAssembly { get; set; } - public string? TypeName { get; set; } + public abstract string EntryPoint { get; set; } - public string? FunctionId { get; set; } + public abstract string FunctionId { get; set; } - public string? FuncName { get; set; } + public abstract string Name { get; set; } } } diff --git a/src/DotNetWorker/Definition/GrpcFunctionMetadata.cs b/src/DotNetWorker/Definition/GrpcFunctionMetadata.cs new file mode 100644 index 000000000..9272f4834 --- /dev/null +++ b/src/DotNetWorker/Definition/GrpcFunctionMetadata.cs @@ -0,0 +1,24 @@ +using System.IO; +using Microsoft.Azure.WebJobs.Script.Grpc.Messages; + +namespace Microsoft.Azure.Functions.Worker.Definition +{ + internal class GrpcFunctionMetadata : FunctionMetadata + { + public GrpcFunctionMetadata(FunctionLoadRequest loadRequest) + { + EntryPoint = loadRequest.Metadata.EntryPoint; + Name = loadRequest.Metadata.Name; + PathToAssembly = Path.GetFullPath(loadRequest.Metadata.ScriptFile); + FunctionId = loadRequest.FunctionId; + } + + public override string PathToAssembly { get; set; } + + public override string EntryPoint { get; set; } + + public override string FunctionId { get; set; } + + public override string Name { get; set; } + } +} diff --git a/src/DotNetWorker/IFunctionActivator.cs b/src/DotNetWorker/IFunctionActivator.cs index 6bcaf80f2..7f1b2fa8e 100644 --- a/src/DotNetWorker/IFunctionActivator.cs +++ b/src/DotNetWorker/IFunctionActivator.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System; diff --git a/src/DotNetWorker/Invocation/DefaultFunctionExecutor.cs b/src/DotNetWorker/Invocation/DefaultFunctionExecutor.cs index 2f5afb74b..266394436 100644 --- a/src/DotNetWorker/Invocation/DefaultFunctionExecutor.cs +++ b/src/DotNetWorker/Invocation/DefaultFunctionExecutor.cs @@ -1,6 +1,7 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +using System; using System.Linq; using System.Threading.Tasks; using Microsoft.Azure.Functions.Worker.Pipeline; @@ -12,8 +13,14 @@ internal class DefaultFunctionExecutor : IFunctionExecutor public async Task ExecuteAsync(FunctionExecutionContext context) { var invoker = context.FunctionDefinition.Invoker; - object instance = invoker.CreateInstance(context.InstanceServices); - object result = await invoker.InvokeAsync(instance, context.FunctionDefinition.Parameters.Select(p => p.Value).ToArray()); + object? instance = invoker.CreateInstance(context.InstanceServices); + + if (instance == null) + { + throw new InvalidOperationException($"Unable to create instance of function {context.FunctionDefinition.Metadata.Name}."); + } + + object? result = await invoker.InvokeAsync(instance, context.FunctionDefinition.Parameters.Select(p => p.Value).ToArray()); context.InvocationResult = result; } } diff --git a/src/DotNetWorker/Invocation/DefaultFunctionInvoker.cs b/src/DotNetWorker/Invocation/DefaultFunctionInvoker.cs index 032a63ad5..9a5d88431 100644 --- a/src/DotNetWorker/Invocation/DefaultFunctionInvoker.cs +++ b/src/DotNetWorker/Invocation/DefaultFunctionInvoker.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System; @@ -19,12 +19,12 @@ public DefaultFunctionInvoker(IMethodInvoker methodInvoker, // For testing internal IFunctionActivator FunctionActivator { get; } - public object CreateInstance(IServiceProvider instanceServices) + public object? CreateInstance(IServiceProvider instanceServices) { return FunctionActivator.CreateInstance(instanceServices); } - public Task InvokeAsync(object instance, object[] arguments) + public Task InvokeAsync(object instance, object?[] arguments) { return _methodInvoker.InvokeAsync((TInstance)instance, arguments) .ContinueWith(t => t.Result); diff --git a/src/DotNetWorker/Invocation/IFunctionInvoker.cs b/src/DotNetWorker/Invocation/IFunctionInvoker.cs index 40dce33d9..ba32438d8 100644 --- a/src/DotNetWorker/Invocation/IFunctionInvoker.cs +++ b/src/DotNetWorker/Invocation/IFunctionInvoker.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System; @@ -8,7 +8,7 @@ namespace Microsoft.Azure.Functions.Worker.Invocation { public interface IFunctionInvoker { - object CreateInstance(IServiceProvider instanceServices); + object? CreateInstance(IServiceProvider instanceServices); Task InvokeAsync(object instance, object?[] arguments); } diff --git a/src/DotNetWorker/Invocation/IMethodInvoker.cs b/src/DotNetWorker/Invocation/IMethodInvoker.cs index 06b9b068f..e43aa5c71 100644 --- a/src/DotNetWorker/Invocation/IMethodInvoker.cs +++ b/src/DotNetWorker/Invocation/IMethodInvoker.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System.Threading.Tasks; @@ -8,6 +8,6 @@ namespace Microsoft.Azure.Functions.Worker.Invocation internal interface IMethodInvoker { // The cancellation token, if any, is provided along with the other arguments. - Task InvokeAsync(TInstance instance, object[] arguments); + Task InvokeAsync(TInstance instance, object?[] arguments); } } diff --git a/src/DotNetWorker/OutputBinding.cs b/src/DotNetWorker/OutputBinding.cs index f9bf762c4..1919e0dff 100644 --- a/src/DotNetWorker/OutputBinding.cs +++ b/src/DotNetWorker/OutputBinding.cs @@ -1,12 +1,12 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. namespace Microsoft.Azure.Functions.Worker { public abstract class OutputBinding { - internal abstract T GetValue(); + internal abstract T? GetValue(); - public abstract void SetValue(T value); + public abstract void SetValue(T? value); } } diff --git a/src/DotNetWorker/RpcExtensions.cs b/src/DotNetWorker/RpcExtensions.cs index effa7696f..0bd7c6b0c 100644 --- a/src/DotNetWorker/RpcExtensions.cs +++ b/src/DotNetWorker/RpcExtensions.cs @@ -1,11 +1,10 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System; -using System.IO; -using System.Net.Http; using System.Text.Json; using Google.Protobuf; +using Microsoft.Azure.Functions.Worker.Definition; using Microsoft.Azure.WebJobs.Script.Grpc.Messages; namespace Microsoft.Azure.Functions.Worker @@ -177,16 +176,7 @@ internal static TypedData ToRpcLongArray(this long[] arrLong) return typedData; } - internal static FunctionMetadata ToFunctionMetadata(this FunctionLoadRequest loadRequest) - { - return new FunctionMetadata - { - EntryPoint = loadRequest.Metadata.EntryPoint, - FuncName = loadRequest.Metadata.Name, - PathToAssembly = Path.GetFullPath(loadRequest.Metadata.ScriptFile), - FunctionId = loadRequest.FunctionId - }; - } + internal static FunctionMetadata ToFunctionMetadata(this FunctionLoadRequest loadRequest) => new GrpcFunctionMetadata(loadRequest); internal static RpcException? ToRpcException(this Exception exception) { diff --git a/test/DotNetWorkerTests/Book.cs b/test/DotNetWorkerTests/Book.cs index 936aaf2fb..23ba1c012 100644 --- a/test/DotNetWorkerTests/Book.cs +++ b/test/DotNetWorkerTests/Book.cs @@ -1,14 +1,14 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. namespace Microsoft.Azure.Functions.Worker.Tests { public class Book { - public string? Id { get; set; } + public string Id { get; set; } - public string? Title { get; set; } + public string Title { get; set; } - public string? Author { get; set; } + public string Author { get; set; } } } diff --git a/test/DotNetWorkerTests/Converters/JsonPocoConverterTests.cs b/test/DotNetWorkerTests/Converters/JsonPocoConverterTests.cs index 64d14b04e..cf5210fd1 100644 --- a/test/DotNetWorkerTests/Converters/JsonPocoConverterTests.cs +++ b/test/DotNetWorkerTests/Converters/JsonPocoConverterTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -52,7 +52,7 @@ public void ConvertJsonStringArrayToIEnumerableOfT() var source = "[ \"a\", \"b\", \"c\" ]"; var context = new TestConverterContext("input", typeof(IEnumerable), source); - Assert.True(_jsonPocoConverter.TryConvert(context, out object? target)); + Assert.True(_jsonPocoConverter.TryConvert(context, out object target)); var targetEnum = TestUtility.AssertIsTypeAndConvert>(target); Assert.Collection(targetEnum, diff --git a/test/DotNetWorkerTests/DefaultFunctionInvokerTests.cs b/test/DotNetWorkerTests/DefaultFunctionInvokerTests.cs index 621d2a2d5..eab935b6b 100644 --- a/test/DotNetWorkerTests/DefaultFunctionInvokerTests.cs +++ b/test/DotNetWorkerTests/DefaultFunctionInvokerTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System.Linq; @@ -16,7 +16,6 @@ namespace Microsoft.Azure.Functions.Worker.Tests public class DefaultFunctionInvokerTests { private readonly DefaultFunctionExecutor _executor; - private readonly Channel _channel; private readonly DefaultFunctionInvokerFactory _functionInvokerFactory; public DefaultFunctionInvokerTests() @@ -129,7 +128,7 @@ public async Task InvokeAsync_StaticFunctionWithVoidTaskReturn() private FunctionExecutionContext CreateContext(MethodInfo mi) { var context = new TestFunctionExecutionContext(); - var metadata = new FunctionMetadata(); + var metadata = new TestFunctionMetadata(); var parameters = mi.GetParameters().Select(p => new FunctionParameter(p.Name, p.ParameterType)); context.FunctionDefinition = new DefaultFunctionDefinition(metadata, _functionInvokerFactory.Create(mi), parameters); diff --git a/test/DotNetWorkerTests/FunctionBrokerTests.cs b/test/DotNetWorkerTests/FunctionBrokerTests.cs index 770095df7..6ff593d61 100644 --- a/test/DotNetWorkerTests/FunctionBrokerTests.cs +++ b/test/DotNetWorkerTests/FunctionBrokerTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System.Threading.Tasks; @@ -28,7 +28,7 @@ public async void DiposeExecutionContextTestAsync() var invocationRequest = new TestFunctionInvocation(); invocationRequest.FunctionId = "123"; - var functionDescriptor = new FunctionMetadata(); + var functionDescriptor = new TestFunctionMetadata(); functionDescriptor.FunctionId = "123"; var definition = new TestFunctionDefinition diff --git a/test/DotNetWorkerTests/TestFunctionExecutionContext.cs b/test/DotNetWorkerTests/TestFunctionExecutionContext.cs index b29e50d5c..53d07419b 100644 --- a/test/DotNetWorkerTests/TestFunctionExecutionContext.cs +++ b/test/DotNetWorkerTests/TestFunctionExecutionContext.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System; @@ -17,7 +17,7 @@ internal class TestFunctionExecutionContext : FunctionExecutionContext, IDisposa public override FunctionDefinition FunctionDefinition { get; set; } - public override object? InvocationResult { get; set; } + public override object InvocationResult { get; set; } public override InvocationLogger Logger { get; set; } diff --git a/test/DotNetWorkerTests/TestFunctionMetadata.cs b/test/DotNetWorkerTests/TestFunctionMetadata.cs new file mode 100644 index 000000000..99867a18d --- /dev/null +++ b/test/DotNetWorkerTests/TestFunctionMetadata.cs @@ -0,0 +1,13 @@ +namespace Microsoft.Azure.Functions.Worker.Tests +{ + internal class TestFunctionMetadata : FunctionMetadata + { + public override string PathToAssembly { get; set; } + + public override string EntryPoint { get; set; } + + public override string FunctionId { get; set; } + + public override string Name { get; set; } + } +} From 8ab71a9be3b778e17a539bcb009637ec814a2ff4 Mon Sep 17 00:00:00 2001 From: Brett Samblanet Date: Wed, 3 Feb 2021 09:16:46 -0800 Subject: [PATCH 2/3] disable nullable for invoker --- .../Invocation/DefaultFunctionExecutor.cs | 7 ------- .../Invocation/DefaultFunctionInvoker.cs | 8 +++++--- .../Invocation/DefaultMethodInvokerFactory.cs | 6 ++++-- src/DotNetWorker/Invocation/IFunctionInvoker.cs | 6 ++++-- src/DotNetWorker/Invocation/IMethodInvoker.cs | 4 +++- .../Invocation/MethodInvokerWithReturnValue.cs | 4 +++- src/DotNetWorker/Invocation/TaskMethodInvoker.cs | 4 +++- src/DotNetWorker/Invocation/VoidMethodInvoker.cs | 4 +++- .../Invocation/VoidTaskMethodInvoker.cs | 4 +++- src/DotNetWorker/Logging/InvocationLogger.cs | 13 +++++++++++-- src/DotNetWorker/NullFunctionActivator.cs | 4 ++-- .../DefaultFunctionInvokerTests.cs | 2 -- 12 files changed, 41 insertions(+), 25 deletions(-) diff --git a/src/DotNetWorker/Invocation/DefaultFunctionExecutor.cs b/src/DotNetWorker/Invocation/DefaultFunctionExecutor.cs index 266394436..e78216159 100644 --- a/src/DotNetWorker/Invocation/DefaultFunctionExecutor.cs +++ b/src/DotNetWorker/Invocation/DefaultFunctionExecutor.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. -using System; using System.Linq; using System.Threading.Tasks; using Microsoft.Azure.Functions.Worker.Pipeline; @@ -14,12 +13,6 @@ public async Task ExecuteAsync(FunctionExecutionContext context) { var invoker = context.FunctionDefinition.Invoker; object? instance = invoker.CreateInstance(context.InstanceServices); - - if (instance == null) - { - throw new InvalidOperationException($"Unable to create instance of function {context.FunctionDefinition.Metadata.Name}."); - } - object? result = await invoker.InvokeAsync(instance, context.FunctionDefinition.Parameters.Select(p => p.Value).ToArray()); context.InvocationResult = result; } diff --git a/src/DotNetWorker/Invocation/DefaultFunctionInvoker.cs b/src/DotNetWorker/Invocation/DefaultFunctionInvoker.cs index 9a5d88431..9f334e27f 100644 --- a/src/DotNetWorker/Invocation/DefaultFunctionInvoker.cs +++ b/src/DotNetWorker/Invocation/DefaultFunctionInvoker.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +#nullable disable + using System; using System.Threading.Tasks; @@ -19,15 +21,15 @@ public DefaultFunctionInvoker(IMethodInvoker methodInvoker, // For testing internal IFunctionActivator FunctionActivator { get; } - public object? CreateInstance(IServiceProvider instanceServices) + public object CreateInstance(IServiceProvider instanceServices) { return FunctionActivator.CreateInstance(instanceServices); } - public Task InvokeAsync(object instance, object?[] arguments) + public Task InvokeAsync(object instance, object[] arguments) { return _methodInvoker.InvokeAsync((TInstance)instance, arguments) - .ContinueWith(t => t.Result); + .ContinueWith(t => t.Result); } } } diff --git a/src/DotNetWorker/Invocation/DefaultMethodInvokerFactory.cs b/src/DotNetWorker/Invocation/DefaultMethodInvokerFactory.cs index 77a6f38a1..d6e9c6676 100644 --- a/src/DotNetWorker/Invocation/DefaultMethodInvokerFactory.cs +++ b/src/DotNetWorker/Invocation/DefaultMethodInvokerFactory.cs @@ -1,6 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +#nullable disable + using System; using System.Collections.Generic; using System.Diagnostics; @@ -63,7 +65,7 @@ public IMethodInvoker Create(MethodInfo if (argumentType.IsByRef) { // The type of the local variable (and object in the arguments array) should be T rather than T&. - argumentType = argumentType.GetElementType(); + argumentType = argumentType.GetElementType()!; } // T argumentN diff --git a/src/DotNetWorker/Invocation/IFunctionInvoker.cs b/src/DotNetWorker/Invocation/IFunctionInvoker.cs index ba32438d8..c970b95d0 100644 --- a/src/DotNetWorker/Invocation/IFunctionInvoker.cs +++ b/src/DotNetWorker/Invocation/IFunctionInvoker.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +#nullable disable + using System; using System.Threading.Tasks; @@ -8,8 +10,8 @@ namespace Microsoft.Azure.Functions.Worker.Invocation { public interface IFunctionInvoker { - object? CreateInstance(IServiceProvider instanceServices); + object CreateInstance(IServiceProvider instanceServices); - Task InvokeAsync(object instance, object?[] arguments); + Task InvokeAsync(object instance, object[] arguments); } } diff --git a/src/DotNetWorker/Invocation/IMethodInvoker.cs b/src/DotNetWorker/Invocation/IMethodInvoker.cs index e43aa5c71..f3ec055b8 100644 --- a/src/DotNetWorker/Invocation/IMethodInvoker.cs +++ b/src/DotNetWorker/Invocation/IMethodInvoker.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +#nullable disable + using System.Threading.Tasks; namespace Microsoft.Azure.Functions.Worker.Invocation @@ -8,6 +10,6 @@ namespace Microsoft.Azure.Functions.Worker.Invocation internal interface IMethodInvoker { // The cancellation token, if any, is provided along with the other arguments. - Task InvokeAsync(TInstance instance, object?[] arguments); + Task InvokeAsync(TInstance instance, object[] arguments); } } diff --git a/src/DotNetWorker/Invocation/MethodInvokerWithReturnValue.cs b/src/DotNetWorker/Invocation/MethodInvokerWithReturnValue.cs index 102dbed1a..1fe7349cc 100644 --- a/src/DotNetWorker/Invocation/MethodInvokerWithReturnValue.cs +++ b/src/DotNetWorker/Invocation/MethodInvokerWithReturnValue.cs @@ -1,6 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +#nullable disable + using System; using System.Threading.Tasks; diff --git a/src/DotNetWorker/Invocation/TaskMethodInvoker.cs b/src/DotNetWorker/Invocation/TaskMethodInvoker.cs index 1cf288757..c3993ff5d 100644 --- a/src/DotNetWorker/Invocation/TaskMethodInvoker.cs +++ b/src/DotNetWorker/Invocation/TaskMethodInvoker.cs @@ -1,6 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +#nullable disable + using System; using System.Diagnostics; using System.Threading.Tasks; diff --git a/src/DotNetWorker/Invocation/VoidMethodInvoker.cs b/src/DotNetWorker/Invocation/VoidMethodInvoker.cs index 08666edb2..b1530b39b 100644 --- a/src/DotNetWorker/Invocation/VoidMethodInvoker.cs +++ b/src/DotNetWorker/Invocation/VoidMethodInvoker.cs @@ -1,6 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +#nullable disable + using System; using System.Threading.Tasks; diff --git a/src/DotNetWorker/Invocation/VoidTaskMethodInvoker.cs b/src/DotNetWorker/Invocation/VoidTaskMethodInvoker.cs index df3afd266..049a6d12e 100644 --- a/src/DotNetWorker/Invocation/VoidTaskMethodInvoker.cs +++ b/src/DotNetWorker/Invocation/VoidTaskMethodInvoker.cs @@ -1,6 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +#nullable disable + using System; using System.Threading.Tasks; diff --git a/src/DotNetWorker/Logging/InvocationLogger.cs b/src/DotNetWorker/Logging/InvocationLogger.cs index 7058ec69c..88b08efcd 100644 --- a/src/DotNetWorker/Logging/InvocationLogger.cs +++ b/src/DotNetWorker/Logging/InvocationLogger.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System; @@ -22,7 +22,7 @@ public InvocationLogger(string invocationId, ChannelWriter cha public IDisposable BeginScope(TState state) { - return null; + return EmptyDisposable.Instance; } public bool IsEnabled(LogLevel logLevel) @@ -67,5 +67,14 @@ private static Level ToRpcLogLevel(LogLevel logLevel) return Level.None; } } + + private class EmptyDisposable : IDisposable + { + public static IDisposable Instance = new EmptyDisposable(); + + public void Dispose() + { + } + } } } diff --git a/src/DotNetWorker/NullFunctionActivator.cs b/src/DotNetWorker/NullFunctionActivator.cs index fe0b24063..8eaa70b43 100644 --- a/src/DotNetWorker/NullFunctionActivator.cs +++ b/src/DotNetWorker/NullFunctionActivator.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. using System; @@ -13,7 +13,7 @@ private NullFunctionActivator() public static NullFunctionActivator Instance { get; } = new NullFunctionActivator(); - public T CreateInstance(IServiceProvider services) + public T? CreateInstance(IServiceProvider services) { return default(T); } diff --git a/test/DotNetWorkerTests/DefaultFunctionInvokerTests.cs b/test/DotNetWorkerTests/DefaultFunctionInvokerTests.cs index eab935b6b..b60404b43 100644 --- a/test/DotNetWorkerTests/DefaultFunctionInvokerTests.cs +++ b/test/DotNetWorkerTests/DefaultFunctionInvokerTests.cs @@ -3,12 +3,10 @@ using System.Linq; using System.Reflection; -using System.Threading.Channels; using System.Threading.Tasks; using Microsoft.Azure.Functions.Worker.Definition; using Microsoft.Azure.Functions.Worker.Invocation; using Microsoft.Azure.Functions.Worker.Pipeline; -using Microsoft.Azure.WebJobs.Script.Grpc.Messages; using Xunit; namespace Microsoft.Azure.Functions.Worker.Tests From 546eda09e3320869bebb293e713f894708151c06 Mon Sep 17 00:00:00 2001 From: Brett Samblanet Date: Wed, 3 Feb 2021 09:58:45 -0800 Subject: [PATCH 3/3] adding header --- src/DotNetWorker/Definition/GrpcFunctionMetadata.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/DotNetWorker/Definition/GrpcFunctionMetadata.cs b/src/DotNetWorker/Definition/GrpcFunctionMetadata.cs index 9272f4834..4bb9a3c10 100644 --- a/src/DotNetWorker/Definition/GrpcFunctionMetadata.cs +++ b/src/DotNetWorker/Definition/GrpcFunctionMetadata.cs @@ -1,4 +1,7 @@ -using System.IO; +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System.IO; using Microsoft.Azure.WebJobs.Script.Grpc.Messages; namespace Microsoft.Azure.Functions.Worker.Definition