[SOLVED] Getting Bindingdata from Azure function .net6 (function v4)

Issue

In dotnet5 (function v3) we could fetch the metadata from FunctionContext by calling functionContext.BindingContext.BindingData.TryGetValue("Metadata", out var meta);
However dotnet 6 (v4) does not seem to have this option.

Azure function v3 (.net5)

Can anyone help me out by providing the correct implementation for the new dotnet 6 (v4) functions

Solution

To use .net6 with Function V4 we need VS 2022 environment or VS CODE .

I have tried with one simple blobtrigger azure function running with .net 5 and it works fine to fetch the meta data.

using System;
using System.IO;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace testfuncapp
{
    public static class Function1
    {
        [Function("Function1")]
        public static void Run([BlobTrigger("test/{name}", Connection = "AzureWebJobsStorage")] string myBlob, string name,
            FunctionContext context)
        {
            var logger = context.GetLogger("Function1");
            logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {myBlob}");
        }
    }
}

OUTPUT:-
enter image description here
And the same code we have tried with .net 6 environment and its works fine as well.

To do that Open VS CODE and update the .csproj file with .net 6 and function runtime version to V4

NOTE: We need to install only one runtime version at our end.

Update .csproj file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="4.0.4" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.2.0" OutputItemType="Analyzer" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.5.2" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

And update the settings.json file to .net 6 and function runtime to V4 then save it .
enter image description here

And then use the command as below:

1- dotnet restore
2- dotnet build
3- func host start

OUTPUT:-
enter image description here

enter image description here

enter image description here

For more information please refer the below links:

BLOG : Migration from Asp.Net Core 5.0 to 6.0

MS Q&A : Migrating Auzre Function runtime version from 3.x to 4.x not working in VS 2019

Answered By – AjayKumarGhose-MT

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published.