diff --git a/extensions/Worker.Extensions.Http.AspNetCore/src/AspNetMiddleware/FunctionsHttpContextExtensions.cs b/extensions/Worker.Extensions.Http.AspNetCore/src/AspNetMiddleware/FunctionsHttpContextExtensions.cs index a1b0bd2a2..aa8c8a835 100644 --- a/extensions/Worker.Extensions.Http.AspNetCore/src/AspNetMiddleware/FunctionsHttpContextExtensions.cs +++ b/extensions/Worker.Extensions.Http.AspNetCore/src/AspNetMiddleware/FunctionsHttpContextExtensions.cs @@ -14,7 +14,7 @@ internal static Task InvokeFunctionAsync(this HttpContext context) { var coordinator = context.RequestServices.GetRequiredService(); context.Request.Headers.TryGetValue(Constants.CorrelationHeader, out StringValues invocationId); - return coordinator.RunFunctionInvocationAsync(invocationId); + return coordinator.RunFunctionInvocationAsync(invocationId!); // will fail later if null. } } } diff --git a/extensions/Worker.Extensions.Http.AspNetCore/src/AspNetMiddleware/WorkerRequestServicesMiddleware.cs b/extensions/Worker.Extensions.Http.AspNetCore/src/AspNetMiddleware/WorkerRequestServicesMiddleware.cs index 8fa0e583d..36073af78 100644 --- a/extensions/Worker.Extensions.Http.AspNetCore/src/AspNetMiddleware/WorkerRequestServicesMiddleware.cs +++ b/extensions/Worker.Extensions.Http.AspNetCore/src/AspNetMiddleware/WorkerRequestServicesMiddleware.cs @@ -25,7 +25,7 @@ public async Task Invoke(HttpContext context) throw new InvalidOperationException($"Expected correlation id header ('{Constants.CorrelationHeader}') not present"); } - FunctionContext functionContext = await _coordinator.SetHttpContextAsync(invocationId, context); + FunctionContext functionContext = await _coordinator.SetHttpContextAsync(invocationId!, context); // Explicitly set the RequestServices to prevent a new scope from being created internally. // This also prevents the scope from being disposed when the request is complete. We want this to diff --git a/extensions/Worker.Extensions.Http.AspNetCore/src/HttpDataModel/AspNetCoreHttpHeadersCollection.cs b/extensions/Worker.Extensions.Http.AspNetCore/src/HttpDataModel/AspNetCoreHttpHeadersCollection.cs index 20e859fe1..6378ce415 100644 --- a/extensions/Worker.Extensions.Http.AspNetCore/src/HttpDataModel/AspNetCoreHttpHeadersCollection.cs +++ b/extensions/Worker.Extensions.Http.AspNetCore/src/HttpDataModel/AspNetCoreHttpHeadersCollection.cs @@ -57,7 +57,7 @@ private Task ProcessResponseStarting() _originalResponseHeaders.Clear(); foreach (var item in ((HeadersEnumerable)this)) { - _originalResponseHeaders.Add(item.Key, item.Value); + _originalResponseHeaders[item.Key] = item.Value; } return Task.CompletedTask; diff --git a/extensions/Worker.Extensions.Http.AspNetCore/src/Infrastructure/ExtensionTrace.cs b/extensions/Worker.Extensions.Http.AspNetCore/src/Infrastructure/ExtensionTrace.cs index 545cce031..89724adfa 100644 --- a/extensions/Worker.Extensions.Http.AspNetCore/src/Infrastructure/ExtensionTrace.cs +++ b/extensions/Worker.Extensions.Http.AspNetCore/src/Infrastructure/ExtensionTrace.cs @@ -15,7 +15,9 @@ public ExtensionTrace(ILoggerFactory loggerFactory) _defaultLogger = loggerFactory.CreateLogger("Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore"); } - public IDisposable BeginScope(TState state) => _defaultLogger.BeginScope(state); + public IDisposable? BeginScope(TState state) + where TState : notnull + => _defaultLogger.BeginScope(state); public bool IsEnabled(LogLevel logLevel) => _defaultLogger.IsEnabled(logLevel); diff --git a/sdk/Sdk.Analyzers/Sdk.Analyzers.csproj b/sdk/Sdk.Analyzers/Sdk.Analyzers.csproj index 4e1e142d6..aaef6653a 100644 --- a/sdk/Sdk.Analyzers/Sdk.Analyzers.csproj +++ b/sdk/Sdk.Analyzers/Sdk.Analyzers.csproj @@ -14,6 +14,7 @@ disable Azure Functions Worker, analyzers $(TargetsForTfmSpecificContentInPackage);_AddAnalyzersToOutput + true diff --git a/sdk/Sdk.Generators/Constants.cs b/sdk/Sdk.Generators/Constants.cs index 7799f557e..d3a5ceb29 100644 --- a/sdk/Sdk.Generators/Constants.cs +++ b/sdk/Sdk.Generators/Constants.cs @@ -1,10 +1,17 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +using System; + namespace Microsoft.Azure.Functions.Worker.Sdk.Generators { internal static class Constants { +#pragma warning disable RS1035 // Do not use APIs banned for analyzers + // Suppressing warning based on https://github.com/dotnet/roslyn-analyzers/issues/6467 + internal static readonly string NewLine = Environment.NewLine; +#pragma warning restore RS1035 // Do not use APIs banned for analyzers + internal static class ExecutionModel { internal const string Isolated = "isolated"; diff --git a/sdk/Sdk.Generators/FunctionExecutor/FunctionExecutorGenerator.Emitter.cs b/sdk/Sdk.Generators/FunctionExecutor/FunctionExecutorGenerator.Emitter.cs index 40aeee233..18844cc6b 100644 --- a/sdk/Sdk.Generators/FunctionExecutor/FunctionExecutorGenerator.Emitter.cs +++ b/sdk/Sdk.Generators/FunctionExecutor/FunctionExecutorGenerator.Emitter.cs @@ -36,7 +36,7 @@ namespace {{FunctionsUtil.GetNamespaceForGeneratedCode(context)}} [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class DirectFunctionExecutor : global::Microsoft.Azure.Functions.Worker.Invocation.IFunctionExecutor { - private readonly global::Microsoft.Azure.Functions.Worker.IFunctionActivator _functionActivator;{{(defaultExecutorNeeded ? $"{Environment.NewLine} private Lazy _defaultExecutor;" : string.Empty)}} + private readonly global::Microsoft.Azure.Functions.Worker.IFunctionActivator _functionActivator;{{(defaultExecutorNeeded ? $"{Constants.NewLine} private Lazy _defaultExecutor;" : string.Empty)}} {{GetTypesDictionary(functions)}} public DirectFunctionExecutor(global::Microsoft.Azure.Functions.Worker.IFunctionActivator functionActivator) { @@ -47,7 +47,7 @@ public DirectFunctionExecutor(global::Microsoft.Azure.Functions.Worker.IFunction public async global::System.Threading.Tasks.ValueTask ExecuteAsync(global::Microsoft.Azure.Functions.Worker.FunctionContext context) { {{GetMethodBody(functions, defaultExecutorNeeded)}} - }{{(defaultExecutorNeeded ? $"{Environment.NewLine}{EmitCreateDefaultExecutorMethod(context)}" : string.Empty)}} + }{{(defaultExecutorNeeded ? $"{Constants.NewLine}{EmitCreateDefaultExecutorMethod(context)}" : string.Empty)}} } /// @@ -105,7 +105,7 @@ private static string GetTypesDictionary(IEnumerable functio return $$""" private readonly Dictionary types = new Dictionary() { - {{string.Join($",{Environment.NewLine} ", typesDict.Select(c => $$""" { "{{c.Key}}", Type.GetType("{{c.Key}}, {{c.Value}}") }"""))}} + {{string.Join($",{Constants.NewLine} ", typesDict.Select(c => $$""" { "{{c.Key}}", Type.GetType("{{c.Key}}, {{c.Value}}") }"""))}} }; """; @@ -147,7 +147,7 @@ private static string GetMethodBody(IEnumerable functions, b var inputBindingFeature = context.Features.Get(); var inputBindingResult = await inputBindingFeature.BindFunctionInputAsync(context); var inputArguments = inputBindingResult.Values; - {(anyDefaultExecutor ? $" _defaultExecutor = new Lazy(() => CreateDefaultExecutorInstance(context));{Environment.NewLine}" : string.Empty)} + {(anyDefaultExecutor ? $" _defaultExecutor = new Lazy(() => CreateDefaultExecutorInstance(context));{Constants.NewLine}" : string.Empty)} """); foreach (ExecutableFunction function in functions) diff --git a/sdk/Sdk.Generators/Sdk.Generators.csproj b/sdk/Sdk.Generators/Sdk.Generators.csproj index 98d57854d..93af6de7a 100644 --- a/sdk/Sdk.Generators/Sdk.Generators.csproj +++ b/sdk/Sdk.Generators/Sdk.Generators.csproj @@ -12,6 +12,7 @@ 3 4 true + true @@ -35,4 +36,8 @@ + + + + diff --git a/src/DotNetWorker.Core/Logging/ISystemLogWriter.cs b/src/DotNetWorker.Core/Logging/ISystemLogWriter.cs index 0f7b3eba4..4f6b6f16a 100644 --- a/src/DotNetWorker.Core/Logging/ISystemLogWriter.cs +++ b/src/DotNetWorker.Core/Logging/ISystemLogWriter.cs @@ -22,6 +22,6 @@ internal interface ISystemLogWriter /// The entry to be written. Can be also an object. /// The exception related to this entry. /// Function to create a message of the state and exception. - void WriteSystemLog(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter); + void WriteSystemLog(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter); } } diff --git a/src/DotNetWorker.Core/Logging/IUserLogWriter.cs b/src/DotNetWorker.Core/Logging/IUserLogWriter.cs index 79d0d0ef5..594fb626f 100644 --- a/src/DotNetWorker.Core/Logging/IUserLogWriter.cs +++ b/src/DotNetWorker.Core/Logging/IUserLogWriter.cs @@ -22,6 +22,6 @@ internal interface IUserLogWriter /// The entry to be written. Can be also an object. /// The exception related to this entry. /// Function to create a message of the state and exception. - void WriteUserLog(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter); + void WriteUserLog(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter); } } diff --git a/src/DotNetWorker.Core/Logging/NullLogWriter.cs b/src/DotNetWorker.Core/Logging/NullLogWriter.cs index bb08e2bb6..43b633cd6 100644 --- a/src/DotNetWorker.Core/Logging/NullLogWriter.cs +++ b/src/DotNetWorker.Core/Logging/NullLogWriter.cs @@ -22,12 +22,12 @@ private NullLogWriter() public static NullLogWriter Instance = new NullLogWriter(); /// - public void WriteSystemLog(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + public void WriteSystemLog(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { } /// - public void WriteUserLog(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + public void WriteUserLog(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { } diff --git a/src/DotNetWorker.Core/Logging/WorkerLogger.cs b/src/DotNetWorker.Core/Logging/WorkerLogger.cs index 324e58a86..7a9c1a22d 100644 --- a/src/DotNetWorker.Core/Logging/WorkerLogger.cs +++ b/src/DotNetWorker.Core/Logging/WorkerLogger.cs @@ -22,7 +22,8 @@ public WorkerLogger(string category, ISystemLogWriter systemLogWriter, IUserLogW _scopeProvider = scopeProvider; } - public IDisposable BeginScope(TState state) + public IDisposable? BeginScope(TState state) + where TState : notnull { // The built-in DI wire-up guarantees that scope provider will be set. return _scopeProvider.Push(state); @@ -33,7 +34,7 @@ public bool IsEnabled(LogLevel logLevel) return logLevel != LogLevel.None; } - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { if (WorkerMessage.IsSystemLog) { diff --git a/src/DotNetWorker.Grpc/GrpcFunctionsHostLogWriter.cs b/src/DotNetWorker.Grpc/GrpcFunctionsHostLogWriter.cs index 9178a9af2..f04cd3a5d 100644 --- a/src/DotNetWorker.Grpc/GrpcFunctionsHostLogWriter.cs +++ b/src/DotNetWorker.Grpc/GrpcFunctionsHostLogWriter.cs @@ -28,7 +28,7 @@ public GrpcFunctionsHostLogWriter(GrpcHostChannel channel, IOptions(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + public void WriteUserLog(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { Log(RpcLogCategory.User, scopeProvider, categoryName, logLevel, eventId, state, exception, formatter); } @@ -54,12 +54,12 @@ public void WriteUserMetric(IExternalScopeProvider scopeProvider, IDictionary(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + public void WriteSystemLog(IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { Log(RpcLogCategory.System, scopeProvider, categoryName, logLevel, eventId, state, exception, formatter); } - public void Log(RpcLogCategory category, IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + public void Log(RpcLogCategory category, IExternalScopeProvider scopeProvider, string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { var response = new StreamingMessage(); var rpcLog = new RpcLog diff --git a/src/DotNetWorker.Grpc/Handlers/InvocationHandler.cs b/src/DotNetWorker.Grpc/Handlers/InvocationHandler.cs index 8ddfaea64..225be24da 100644 --- a/src/DotNetWorker.Grpc/Handlers/InvocationHandler.cs +++ b/src/DotNetWorker.Grpc/Handlers/InvocationHandler.cs @@ -116,9 +116,11 @@ public async Task InvokeAsync(InvocationRequest request) } catch (Exception ex) when (!ex.IsFatal()) { +#pragma warning disable CS0618 // Type or member is obsolete response.Result.Exception = _workerOptions.EnableUserCodeException ? ex.ToUserRpcException() : ex.ToRpcException(); - response.Result.Status = StatusResult.Types.Status.Failure; +#pragma warning restore CS0618 // Type or member is obsolete + response.Result.Status = StatusResult.Types.Status.Failure; if (ex.InnerException is TaskCanceledException or OperationCanceledException) { response.Result.Status = StatusResult.Types.Status.Cancelled; diff --git a/src/DotNetWorker.Grpc/RpcExtensions.cs b/src/DotNetWorker.Grpc/RpcExtensions.cs index 5df5377be..1d0f14cde 100644 --- a/src/DotNetWorker.Grpc/RpcExtensions.cs +++ b/src/DotNetWorker.Grpc/RpcExtensions.cs @@ -122,7 +122,7 @@ internal static FunctionDefinition ToFunctionDefinition(this FunctionLoadRequest /// Returns an RpcException for system exception scenarios. /// /// An RpcException - internal static RpcException? ToRpcException(this Exception exception) + internal static RpcException? ToRpcException(this Exception? exception) { if (exception is null) { diff --git a/test/Worker.Extensions.Tests/Worker.Extensions.Tests.csproj b/test/Worker.Extensions.Tests/Worker.Extensions.Tests.csproj index 1a566f4e8..b63593eb5 100644 --- a/test/Worker.Extensions.Tests/Worker.Extensions.Tests.csproj +++ b/test/Worker.Extensions.Tests/Worker.Extensions.Tests.csproj @@ -11,7 +11,7 @@ disable - +