Eklee.Azure.Functions.Http 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Eklee.Azure.Functions.Http --version 0.2.0                
NuGet\Install-Package Eklee.Azure.Functions.Http -Version 0.2.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="Eklee.Azure.Functions.Http" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Eklee.Azure.Functions.Http --version 0.2.0                
#r "nuget: Eklee.Azure.Functions.Http, 0.2.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 Eklee.Azure.Functions.Http as a Cake Addin
#addin nuget:?package=Eklee.Azure.Functions.Http&version=0.2.0

// Install Eklee.Azure.Functions.Http as a Cake Tool
#tool nuget:?package=Eklee.Azure.Functions.Http&version=0.2.0                

Introduction

The purpose of this library is to help developers with dependency injection per HTTP request context. This is currently not available via the default dependency injection infrastructure.

DI Usage

In order to leverage this library, there are 3 steps. You would want to setup your DI, apply the ExecutionContextDependencyInjection attribute, and inject the ExecutionContext as a parameter in your function.

Step 1: Setup DI

The first step is to setup your DI via the Autofac Module.

using Autofac;

namespace FunctionApp1
{
    public class MyModuleConfig : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<MyDomain>().As<IMyDomain>();
        }
    }
}

Step 2/3: Setup ExecutionContextDependencyInjection attribute on said function and inject ExecutionContext.

The second step is to apply the ExecutionContextDependencyInjection on your function and tell it which Module type you would like. Next, you can inject the ExecutionContext which internally carries the function instance Id.

public static class Function1
{
    [ExecutionContextDependencyInjection(typeof(MyModuleConfig))]
    [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log,
        ExecutionContext executionContext)
    {

Dependency Resolution Usage:

Now, there are 2 extension methods you can use against the ExecutionContext. You can choose the Resolve method to resolve your dependency.

var myDomain = executionContext.Resolve<IMyDomain>();
myDomain.DoWork();

Or you can get the Resolver.

var resolver = executionContext.GetResolver();
var myDomain = resolver.Get<IMyDomain>();
myDomain.DoWork();

Domain Usage:

Note that we have already captured ILogger as well as the actual HttpRequest. So, if you can inject IHttpRequestContext to access the logger or Http request which is hanging off IHttpRequestContext.

public class MyDomain : IMyDomain
{
    private readonly IHttpRequestContext _requestContext;

    public MyDomain(IHttpRequestContext requestContext)
    {
        _requestContext = requestContext;
    }

    public void DoWork()
    {
        _requestContext.Logger.LogInformation("FOOBAR");
    }
}

Exception Handling Usage:

To translate an exception-type to an HTTP response like Bad Request, you can implement the IExceptionHandler which will provide a way for the infrastructure code to recognize an exception type and return the correct HTTP response.

public class MyArgumentExceptionHandler : IExceptionHandler
{
    public ExceptionHandlerResult Handle(Exception ex)
    {
        if (ex.GetType() == typeof(ArgumentException))
        {
            return new ExceptionHandlerResult(true, new BadRequestObjectResult(new ErrorMessage { Message = ex.Message }));
        }

        return null;
    }
}

Remember to register the handler in your Module. You can register more than one.

builder.RegisterType<MyArgumentExceptionHandler>().As<IExceptionHandler>();
builder.RegisterType<MyCustomExceptionHandler>().As<IExceptionHandler>();

To leverage the handlers, you will need to use the extension method Run.

[ExecutionContextDependencyInjection(typeof(MyModule))]
[FunctionName("Function3")]
public static async Task<IActionResult> Run3(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]
    HttpRequest req, ILogger log, ExecutionContext executionContext)
{
    // Example of how we can directly resolve a dependency.
    return await executionContext.Run<IDtoDomain, DtoResponse>(domain => Task.FromResult(domain.DoWork()));
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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 Eklee.Azure.Functions.Http:

Package Downloads
Eklee.Azure.Functions.GraphQl

Implement one or more Serverless GraphQL API(s) using Azure Functions, Azure Cosmos DB, Azure Search, Azure Table Storage etc.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.12.19 2,513 11/26/2020
0.12.18 513 11/26/2020
0.10.1 1,442 6/14/2020
0.9.0 1,318 3/28/2020
0.8.7 1,042 12/22/2019
0.8.6 2,285 9/8/2019
0.8.4 2,841 8/11/2019
0.8.3 4,444 6/16/2019
0.8.2 1,836 3/25/2019
0.8.1 5,903 2/10/2019
0.7.0 2,249 12/28/2018
0.6.1 1,715 11/2/2018
0.5.0 1,199 10/27/2018
0.4.1 899 10/25/2018
0.3.0 854 10/21/2018
0.2.0 880 10/19/2018
0.1.1 915 10/16/2018

Implement an opinionated means of rendering http responses based on exception-types.