Mastronardi.Utils.Azure.Functions
1.2.16
Prefix Reserved
See the version list below for details.
dotnet add package Mastronardi.Utils.Azure.Functions --version 1.2.16
NuGet\Install-Package Mastronardi.Utils.Azure.Functions -Version 1.2.16
<PackageReference Include="Mastronardi.Utils.Azure.Functions" Version="1.2.16" />
paket add Mastronardi.Utils.Azure.Functions --version 1.2.16
#r "nuget: Mastronardi.Utils.Azure.Functions, 1.2.16"
// Install Mastronardi.Utils.Azure.Functions as a Cake Addin #addin nuget:?package=Mastronardi.Utils.Azure.Functions&version=1.2.16 // Install Mastronardi.Utils.Azure.Functions as a Cake Tool #tool nuget:?package=Mastronardi.Utils.Azure.Functions&version=1.2.16
Azure functions Utilities
Bindings
In Mvc we can use FromBody and FromQuery in our controllers but in Azure Functions we lack this functionality. Hence there are these bindings you can use to enable it for Azure Functions.
In addition it also provides Data Annotation validation out ouf the box.
It uses the Utf8Json serializer and MessageBack by nuecc.
How to enable:
in Startup.cs add this on top
using Mastronardi.Utils
Then add this to enable these bindings
builder.AddFunctionRequestBindings();
How to use:
You cannot use the normal FromBody
and FromQuery
attribute, instead you must use the one from this utility library.
Mastronardi.Utils.Azure.Functions.Bindings.FromBody
and Mastronardi.Utils.Azure.Functions.Bindings.FromQuery
You can do it like this:
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log,
[Mastronardi.Utils.Azure.Functions.Bindings.FromBody] CustomObject dataBody)
{
...
}
It will serialize what it can to your CustomObject and you can just access the properties directly in your function. It will fail if its not a correctly formatted request, but missing parameters will just be empty or default. The other option is to do it like this:
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log,
[Mastronardi.Utils.Azure.Functions.Bindings.FromBody(false)] IValidated<CustomObject> dataBody)
{
...
}
It will validate your object and you can use .IsValid
and .IsEmpty
to check if it is valid or empty.
You can also request the validation errors in the .ValidationResults property.
In the example the recursive validation is disabled on the attribute. This is usefull if you don't want to validate subobjects.
The same can be done by replacing FromBody with FromQuery and it will fill your object from the QueryString. It also supports validation, so you can make certain querystring parameters mandatory easily. Just display the errors in case its not valid and you're good.
This binding is very performant due to its use of Utf8Json (and also for MessagePack) and that many optimizations have been done to do the validation, instantiating, etc.
Healthchecks
When you do this in your startup:
serviceCollection.AddFunctionHealthChecks().Add....(
(use using Mastronardi.Utils;
)
you can use healthcheck in your function app.
You can use the pre-existing health checks just like you are used to do.
If you make a function like this:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
namespace MyApp.Functions {
public class HealthCheckFunctions{
private readonly HealthCheckService _healthCheckService;
public HealthCheckFunctions(HealthCheckService healthCheckService)
{
_healthCheckService = healthCheckService;
}
[FunctionName(nameof(HeartBeat))]
public async Task<IActionResult> HeartBeat(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "healthcheck")]
HttpRequest req,
ILogger log)
{
var report = await _healthCheckService.CheckHealthAsync();
if (report.Status == HealthStatus.Unhealthy) return new BadRequestObjectResult(report);
return new OkObjectResult(report);
}
}
}
You can poll this endpoint to see the health status.
Generic extensions
By using HttpRequestMessage.SetCultureInfo()
you can easily set the Culture of the current request to the correct one as specified in the Accept-Language header... this allows you to make your functions language aware.
You still have to do something with the culture info ofcourse... it may change your text responses etc, if you do something with it.
Multipart Form File Stream
You can instantiate the Multipart Provider like this:
var provider = new MultipartFormDataIFileSystemStreamProvider(fileSystem, blobPath);
where fileSystem is of type Mastronardi.Utils.IIO.IFilesystem
and blobPath is the path where to save your files
You can then detect multipart and upload the file like this:
if (req.Content.IsMimeMultipartContent("form-data"))
{
// When multipart, files are streamed to storage
var result = await req.Content.ReadAsMultipartAsync(provider);
...
}
result.FileData
contains the uploaded file meta information
Product | Versions 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 | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Auth0.ManagementApi (>= 7.6.0)
- Cryptisk.Utf8Json (>= 1.4.0)
- Mastronardi.Utils.Reflection (>= 1.1.10)
- Mastronardi.Utils.Web (>= 1.1.28)
- MessagePack (>= 2.4.35)
- Microsoft.AspNet.WebApi.Client (>= 5.2.9)
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.AspNetCore.WebUtilities (>= 2.2.0)
- Microsoft.Azure.WebJobs (>= 3.0.33)
- Microsoft.Azure.WebJobs.Core (>= 3.0.33)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 3.1.5)
- Microsoft.Extensions.Primitives (>= 3.1.5)
- System.ComponentModel.Annotations (>= 4.7.0)
- System.Net.Http (>= 4.3.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.