Functions.Worker.ContextAccessor 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Functions.Worker.ContextAccessor --version 1.0.0                
NuGet\Install-Package Functions.Worker.ContextAccessor -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Functions.Worker.ContextAccessor" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Functions.Worker.ContextAccessor --version 1.0.0                
#r "nuget: Functions.Worker.ContextAccessor, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Functions.Worker.ContextAccessor as a Cake Addin
#addin nuget:?package=Functions.Worker.ContextAccessor&version=1.0.0

// Install Functions.Worker.ContextAccessor as a Cake Tool
#tool nuget:?package=Functions.Worker.ContextAccessor&version=1.0.0                

FunctionContext accessor .NET 5+ (.NET isolated) Azure Functions

Usage

Add to your Program.cs file

.ConfigureFunctionsWorkerDefaults(applicationBuilder => applicationBuilder.UseFunctionContextAccessor())

and

.ConfigureServices(services => services.AddFunctionContextAccessor())

Final result would look something like this:<br/> Program.cs

using Functions.Worker.ContextAccessor;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
    .ConfigureAppConfiguration(configBuilder => configBuilder.AddEnvironmentVariables())
    .ConfigureFunctionsWorkerDefaults(applicationBuilder => applicationBuilder.UseFunctionContextAccessor())
    .ConfigureServices(services => services.AddFunctionContextAccessor())
    .ConfigureServices(Startup.Configure)
    .UseDefaultServiceProvider((_, options) =>
    {
        options.ValidateScopes = true;
        options.ValidateOnBuild = true;
    })
    .Build();

host.Run();

Scope Validation

It is strongly recommended that scopes are validated as seen by the inclusion of

.UseDefaultServiceProvider((_, options) =>
{
    options.ValidateScopes = true;
    options.ValidateOnBuild = true;
})

because unintended behavior may occur when there's no validation.

Why

As of this writting, the interface IFunctionsWorkerMiddleware does NOT have a IFunctionsWorkerMiddlewareFactory counterpart and gets registered as a singleton. As opposed to ASP.NET Core's IMiddleware which DOES have an IMiddlewareFactory counterpart and will allow the middleware to be registered with either a scoped or instance lifetime. This resulted in scoped access to the FunctionContext only being available to the function method and context information cannot be easily injected into other parts of the application.

This library is modeled after the implementation of HttpContextAccessor (usage seen here) and is inspired by @dolphinspired's gist.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Functions.Worker.ContextAccessor:

Package Downloads
Optsol.EventDriven.Components.Core.Application

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 93,059 2/28/2023
1.0.0 78,015 11/7/2021

1.0.0 Initial Release