简体   繁体   中英

Migrating in-proc Azure function to isolated function - How to use generic Binder

I have a working .NET6 in-proc Azure Function. I'm in the process of converting it to an isolated worker process (out-of-proc) .net7 function.
I have a HttpTrigger function that should get a file from the blob-storage. Since the blob-storage is case-sensitive and all files are saved in upper-case, I need to make the input upper-case as well.
With the in-proc function, I did that by injecting Microsoft.Azure.WebJobs.IBinder binder and then use binder.BindAsync<byte[]>(new Microsoft.Azure.WebJobs.BlobAttribute($"%BlobContainerName%/{fooUpper}", FileAccess.Read)

This is the .net6 in-proc function:

[Function(nameof(GetFile))]
public async Task<IActionResult> GetFile(
    [HttpTrigger(AuthorizationLevel.Function, "get", Route = "bar/{foo}")] HttpRequest req,
    Microsoft.Azure.WebJobs.IBinder binder, Microsoft.Azure.WebJobs.ExecutionContext context, string foo)
{
    // The file name is always upper-case:
    var fooUpper = foo.ToUpperInvariant();
    var blobStream = await binder.BindAsync<byte[]>(
            new Microsoft.Azure.WebJobs.BlobAttribute($"%BlobContainerName%/{fooUpper}", FileAccess.Read),
            req.FunctionContext.CancellationToken).ConfigureAwait(false);

        return await ExecuteAsync(fooUpper, context.FunctionDirectory, blobStream,
                req.FunctionContext.CancellationToken)
            .ConfigureAwait(false);
}

Of course, I tried migrating this function to a .net7 isolated function by replacing HttpRequest req with HttpRequestData req and it compiles but when I run it binder is always null .

What is the isolated function version of my function?

  • Do I need to create a custom attribute?
  • Or create some middleware that makes the URL upper-case?
  • Or should I create a service that I register during start-up and use with DI in the function class?

Please advice.

Check if my below findings helpful to fix your issue:

BlobContainerClient is no longer working in isolated mode

AFAIK, BlobContainerClient should work with the .NET Isolated Process Version 7 in Azure Functions using Azure.Storage.Blobs NuGet Package as shown in this SO Answer # 75015570 by the user @ HariKrishna and MS DOC .

在此处输入图像描述

If you use the NuGet Package Azure.Storage.Blobs , you can initialize the BlobContainerClient class-objects which is compatible in .NET Isolated Process and for working with the Blob Container Objects, you have to initialize the BlobServiceClient for Connecting to Storage Account and getting access to the Blob Container.

public HttpResponseData Run([Microsoft.Azure.Functions.Worker.HttpTrigger(AuthorizationLevel.Function, "get", Route = "bar/{foo}")] HttpRequestData req,
            BlobContainerClient blobContainerClient, IBinder binder, ExecutionContext executionContext)

Here is the GitHub Article on Azure Functions .NET Isolated Process of using the Ibinder input parameter for data binding and there are some examples provided in the same article for working in the context of Ibinder and StorageAccountAttribute such as Microsoft.Azure.WebJobs.BlobAttribute .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM