Easily add ILogger (non-generic) support logging back into Azure Functions (Isolated Process) for improved DI, better de-coupling from generic types, improved code portability, etc.
$ dotnet add package Functions.Worker.ILoggerSupportA repository of various add-ons for Azure Functions Isolated Process (e.g. Worker AddOns)
Most are published as independent Nuget Packages for buffet style inclusion of whichever add-ons you need.
I'm happy to share with the community, but if you find this useful (e.g for professional use), and are so inclinded, then I do love-me-some-coffee!
<a href="https://www.buymeacoffee.com/cajuncoding" target="_blank"> <img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"> </a>Easily add ILogger (non-generic) support logging back into Azure Functions (Isolated Process) for improved DI, better de-coupling from generic types, improved code portability, etc.
This is dependent on having valid DI support for the FunctionContext which is provided by the handy package Functions.Worker.ContextAccessor.
Add to your startup process in the Program.cs file:
//Required so that the FunctionContextAccessor Middleware is enabled!
.ConfigureFunctionsWorkerDefaults(app => app.UseFunctionContextAccessor())
and then you may simply add functionality....
//This will automatically call .AddFunctionContextAccessor() for you...
.ConfigureServices(services => services.AddFunctionILoggerSupport())
Full example of the startup process in Program.cs:
using Microsoft.Extensions.Hosting;
using Functions.Worker.ContextAccessor;
using Functions.Worker.ILoggerSupport;
var host = Host
.CreateDefaultBuilder()
.ConfigureFunctionsWorkerDefaults(app =>
{
//MUST be called for FunctionContextAccessor to be available!
app.UseFunctionContextAccessor();
})
.ConfigureServices(svc =>
{
svc.AddFunctionILoggerSupport();
})
.Build();
await host.RunAsync().ConfigureAwait(false);
Easily add response compression support, via middleware, for Azure Functions (Isolated Process) when using HttpResponseData.
This provides the correct middleware for Azure Functions Worker (Isolated Process) to enable response compression within Azure Functions using the HttpResponseData (e.g. plain vanilla Azure Function Isolated Process).
It works by simply inspecting the AcceptEncoding header to determine which, if any, compression encodings are supported (Gzip, Brotli, Deflate) and then buffering and compressing the Response Body Stream with the correct implementation to encode the response while also setting the correct Response Header for the Client to correctly decode the response.
NOTES:
Add to your startup process in the Program.cs file:
//Required so that the HttpResponseData Compression Middleware is enabled!
.ConfigureFunctionsWorkerDefaults(app => app.UseHttpResponseDataCompression())
and then you may optionally configure the compression levels for each type....
//This will automatically call .AddFunctionContextAccessor() for you...
.ConfigureServices(svc => {
svc.ConfigureHttpResponseDataCompression(opt =>
{
opt.GzipCompressionLevel = CompressionLevel.SmallestSize;
opt.BrotliCompressionLevel= CompressionLevel.Fastest;
opt.DeflateCompressionLevel = CompressionLevel.SmallestSize;
});
})
Full example of the startup process in Program.cs:
using Microsoft.Extensions.Hosting;
using System.IO.Compression;
using Functions.Worker.HttpResponseDataCompression;
var host = Host
.CreateDefaultBuilder()
.ConfigureFunctionsWorkerDefaults(app =>
{
app.UseHttpResponseDataCompression();
})
.ConfigureServices(svc =>
{
svc.ConfigureHttpResponseDataCompression(opt =>
{
opt.GzipCompressionLevel = CompressionLevel.SmallestSize;
opt.BrotliCompressionLevel= CompressionLevel.Fastest;
opt.DeflateCompressionLevel = CompressionLevel.SmallestSize;
});
})
.Build();
await host.RunAsync().ConfigureAwait(false);
Easily add response Json response handling for POCO or DTO objects from plain vanilla Isolated Process Azure Functions. This reduces the need for full AspNetCore dependencies for simple APIs while also minimizing hte need to handle HttpResponseData manually in every function.
This provides a middleware for Azure Functions Worker (Isolated Process) to enable Json response handling of POCO or DTO objects when using only HttpRequestData/HttpResponseData (e.g. plain vanilla Azure Function Isolated Process).
By enabling the easy addition of automatic Json response handling we reduces the need for full AspNetCore dependencies for simple APIs while also minimizing hte need to handle HttpResponseData manually in every function.
In addition, this can be used in combination with the Functions.Worker.HttpResponseDataCompression (separate Nuget package) when added after the compression middleware is added.
It works by handling the response of any Function that has an HttpTrigger binding, intercepting the invocation result and automatically serializing to Json anytime the result is not an HttpResponseData; thereby enabling full manual control anytime you want by returning the low level HttpResponseData. Otherwise, anytime a data model (POCO/DTO) is returned from the Function, then it will be rendered out as proper Json along with proper Content-Type and Encoding headers.
NOTES:
text/plain responses.
This violates good practices for a Json API so unfortunately we have to manually account for this behavior.Add to your startup process in the Program.cs file:
//Required so that the Json Response Middleware is enabled!
.ConfigureFunctionsWorkerDefaults(app => app.UseJsonResponses())
Full example of the startup process in Program.cs:
using Microsoft.Extensions.Hosting;
using System.IO.Compression;
using Functions.Worker.HttpResponseDataCompression;
var host = Host
.CreateDefaultBuilder()
.ConfigureFunctionsWorkerDefaults(app =>
{
//To use in combination with the Functions.Worker.HttpResponseDataCompression
// simply initialize the compression middleware first...
app.UseHttpResponseDataCompression();
//Then add the Json response middleware...
app.UseJsonResponses();
})
.Build();
await host.RunAsync().ConfigureAwait(false);