skip to Main Content

I’m trying to make an example of BlobTrigger Azure Function using Visual Studio Code.
For that I was following along the Book "Beginning Azure Functions" of Rahul Sawhney.

My csproj file is:

  <Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <RootNamespace>blob_trigger</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.2.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="4.0.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
    <PackageReference Include="SixLabors.ImageSharp" Version="3.0.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>

My Function class is:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace AzureFunctionV2Book.Function
{
    public static class BlobTriggerCSharp
    {
        [FunctionName("BlobTriggerCSharp")]
        public static async Task Run([BlobTrigger("image-blob/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, [Blob("output-blob/{name}", FileAccess.ReadWrite, Connection ="AzureWebJobsStorage")] CloudBlockBlob outputBlob, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blobn Name:{name} n Size: {myBlob.Length} Bytes");

            var width = 100;
            var height = 200;
            var encoder = new PngEncoder();
            using (var output = new MemoryStream())
            using (Image<Rgba32> image = (Image<Rgba32>)Image.Load(myBlob)) {
                image.Mutate(x => x.Resize(width, height));
                image.Save(output, encoder);
                output.Position = 0;
                await outputBlob.UploadFromStreamAsync(output);
            }
        }
    }
}

My local.settings.json file is:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=buildingazurefunctionsfr;AccountKey=somekey;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "FUNCTIONS_EXTENSION_VERSION": "~4",
    "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING": "DefaultEndpointsProtocol=https;AccountName=buildingazurefunctionsfr;AccountKey=somekey;EndpointSuffix=core.windows.net",
    "WEBSITE_CONTENTSHARE": "building-azure-functions-fromvscodebed7b8",
    "WEBSITE_RUN_FROM_PACKAGE": "1",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "app-key"
  }
}

When I try to run the code, I get the following error:

[2023-10-20T15:17:59.993Z] A host error has occurred during startup operation ‘f67d9178-3d34-43ab-8715-a2a55f227595’.
[2023-10-20T15:17:59.998Z] System.Private.CoreLib: Exception has been thrown by the target of an invocation. Microsoft.Azure.WebJobs.Extensions.Storage.Queues: Could not load type ‘Microsoft.Azure.WebJobs.ParameterBindingData’ from assembly ‘Microsoft.Azure.WebJobs, Version=3.0.34.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’.
Value cannot be null. (Parameter ‘provider’)

Any idea how to solve it?

2

Answers


  1. Chosen as BEST ANSWER

    WORKING CODE

        namespace AzureFunctionV2Book.Function
    {
        public class BlobTriggerCSharp
        {
            [FunctionName("BlobTriggerCSharp")]
            public static async Task Run([BlobTrigger("image-blob/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, [Blob("output-blob/{name}", FileAccess.ReadWrite, Connection ="AzureWebJobsStorage")] BlockBlobClient outputBlob, ILogger log)
            {
                log.LogInformation($"C# Blob trigger function Processed blobn Name:{name} n Size: {myBlob.Length} Bytes");
    
                var width = 100;
                var height = 200;
                var encoder = new PngEncoder();
                using (var output = new MemoryStream())
                using (Image<Rgb24> image = (SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgb24>)Image.Load(myBlob)) {
                    image.Mutate(x => x.Resize(width, height));
                    image.Save(output, encoder);
                    output.Position = 0;
                    await outputBlob.UploadAsync(output);
                }
            }
        }
    }
    

    And the host.json is

    {
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            },
            "enableLiveMetricsFilters": true
        },
        "extensionBundle": {
            "id": "Microsoft.Azure.Functions.ExtensionBundle",
            "version": "[3.*, 3.9.0)"
        }
    }
    

    }


  2. I was getting the same error when I tried with the configuration with versions [3.0.0, 4.0.0) and [4.*, 5.0.0):

    enter image description here

    • So, I had to use the below configuration in host.json to fix the above issue:
    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          },
          "enableLiveMetricsFilters": true
        },
        "extensionBundle": {
          "id": "Microsoft.Azure.Functions.ExtensionBundle",
          "version": "[3.*, 3.9.0)"
        }
      }
    }
    

    • I have followed MSDOC to implement Blob Trigger Output binding with below code:

    Code Snippet:

    [FunctionName("Function1")]
    [StorageAccount("con")]
    public static void Run([BlobTrigger("sample-images/{name}")] Stream image,
        [Blob("sample-images-sm/{name}", FileAccess.Write)] Stream imageSmall,
        [Blob("sample-images-md/{name}", FileAccess.Write)] Stream imageMedium)
    {
            IImageFormat format;
    
        using (Image<Rgba32> input = Image.Load<Rgba32>(image, out format))
        {
            ResizeImage(input, imageSmall, ImageSize.Small, format);
        }
    
        image.Position = 0;
        using (Image<Rgba32> input = Image.Load<Rgba32>(image, out format))
        {
            ResizeImage(input, imageMedium, ImageSize.Medium, format);
        }
    }
    
    public static void ResizeImage(Image<Rgba32> input, Stream output, ImageSize size, IImageFormat format)
    {
        var dimensions = imageDimensionsTable[size];
    
        input.Mutate(x => x.Resize(dimensions.Item1, dimensions.Item2));
        input.Save(output, format);
    }
    
    public enum ImageSize { ExtraSmall, Small, Medium }
    
    private static Dictionary<ImageSize, (int, int)> imageDimensionsTable = new Dictionary<ImageSize, (int, int)>() {
        { ImageSize.ExtraSmall, (320, 200) },
        { ImageSize.Small,      (640, 400) },
        { ImageSize.Medium,     (800, 600) }
    };
    

    Uploading Image to Container:

    enter image description here

    Output:

    enter image description here

    • Resized, created a blob automatically and uploaded the Resized Images(as per code):

    enter image description here

    Updating answer as per the comments:

    [2023-10-30T13:42:50.935Z] Microsoft.Azure.WebJobs.Host: Error indexing method ‘BlobTriggerCSharp’. Microsoft.Azure.WebJobs.Host: Can’t bind Blob to type ‘Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob’.
    [2023-10-30T13:42:50.938Z] Possible causes:

    I was also getting the same error:

    enter image description here

    • As per my research, got to know that CloudBlockBlob class is only supported with Microsoft.Azure.Storage.Blob(deprecated package).
    • Instead, we need to use BlobClient or BlobCloudClient class to achieve the same which is part of Azure.Storage.Blobs package(latest).

    enter image description here


    I have reproduced the same using your code with few modifications as shown below:

    Code Snippet:

    [FunctionName("BlobTrigger")]
    public static async Task Run([BlobTrigger("samples-workitems/{name}", Connection = "conn")] Stream myBlob, string name, [Blob("output-blob/{name}", FileAccess.ReadWrite, Connection = "AzureWebJobsStorage")] BlockBlobClient outputBlob, ILogger log)
    {
        log.LogInformation($"C# Blob trigger function Processed blobn Name:{name} n Size: {myBlob.Length} Bytes");
        var width = 100;
        var height = 200;
        var encoder = new PngEncoder();
        using (var output = new MemoryStream())
        using (Image<Rgba32> image = (Image<Rgba32>)Image.Load(myBlob))
        {
            image.Mutate(x => x.Resize(width, height));
            image.Save(output, encoder);
            output.Position = 0;
            await outputBlob.UploadAsync(output);
    
        }   
    }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search