Muhammad Ashhar Hasan
Muhammad Ashhar Hasan

Reputation: 299

Getting "Azure.Storage.Queues: Value cannot be null" error on Azure function with blob trigger

I just want to test a simple Azure function with blob trigger but I get following error locally when I try to run/debug the Azure Function.

An unhandled exception has occurred. Host is shutting down. Functions: Azure.Storage.Queues: Value cannot be null. (Parameter 'value').

Error Snapshot

Code is also very simple (basically the boiler plate code which comes with the template).

using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace Wpm;

public class ThumbnailFunction
{
    [FunctionName(nameof(ThumbnailFunction))]
    public void Run([BlobTrigger("wpm/{name}", Connection = "wpmStorageConn")]Stream myBlob, string name, ILogger logger)
    {
        logger.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    }
}

By checking the other similar questions, it looks like there is some problem with connectionstring but I checked that it is same in secrets.json file and same connection string is working fine in Azure Storage explorer and other services.

connectionstring name is wpmStorageConn. I also tried different things in local.settings.json file, following are its contents:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "wpmStorageConn": "<<HereIsMyConnectionString>>",
    "ConnectionStrings:wpmStorageConn": "<<HereIsMyConnectionString>>",
    "AzureWebJobsSecretStorageType": "files"
  },
  "ConnectionStrings": {
    "wpmStorageConn": "<<HereIsMyConnectionString>>",
    "ConnectionStrings:wpmStorageConn": "<<HereIsMyConnectionString>>"

  }
  }

and following is secrets.json:

{
  "ConnectionStrings:wpmStorageConn": "<<HereIsMyConnectionString>>",
  "ConnectionStrings:wpmStorageConn:blob": "https://wpmstorage11.blob.core.windows.net/",
  "ConnectionStrings:wpmStorageConn:queue": "https://wpmstorage11.queue.core.windows.net/",
  "wpmStorageConn": "UseDevelopmentStorage=true"
}

Upvotes: 0

Views: 150

Answers (2)

Muhammad Ashhar Hasan
Muhammad Ashhar Hasan

Reputation: 299

Step 1: Run the following command in cmd (Administrator)

netstat -ano | findstr :10001

Result will be like following Cmd result

Step 2: Pick the first process ID (in this cae it is 38508) and run following command to kill it.

taskkill /PID 38508 /F

Run the same command again if you find multiple processes, in this case it is only 38508.

Step 3: Type azurite immediately after step 2. (You need to be really quick on this, better to copy paste)

Step 4: Run the project, this error will be gone.

Explanation:

I have tried almost everything which could find in stackoverflow but nothing worked. However, after spending 3 days, I get to know that the error is happening because azurite was not able to start properly. I noticed that error in output window> service dependencies. (Check snapshot below)

Error Snapshot

To resolve such an error, you need to first check that which process is listening to this (10001) port, then close that process so that azurite can run properly and once azurite properly runs then your project will also run without errors.

However, problem in my case is that, somehow this process automatically starts again after a while. So by the time I ran my project, the process started again and I was facing the same issue again. So Solution is to first kill the process and then immediately run azurite command. After that you can run the project without errors.

Upvotes: 0

Ikhtesam Afrin
Ikhtesam Afrin

Reputation: 6497

There are two approaches to make this function work.

  1. By Enabling Use Azurite for runtime storage account option while creating the Function App.

enter image description here

Either you can directly Connect to the dependency like Azure Storage which will store the blob connection string in Secrets.json file for you but in case I didn't use it and clicked Cancel.

enter image description here

By doing so, you will have given code in .csproj and local.settings.json file respectively.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <RootNamespace>_78893453</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.2.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.4.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>
{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_INPROC_NET8_ENABLED": "1",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "BlobConnectionString": "DefaultEndpointsProtocol=https;AccountName=afrinstore1;AccountKey=EEpoF*******AStpzopOQ==;EndpointSuffix=core.windows.net"
  }
}

I am also using default function code.

using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace _78893453
{
    public class Function1
    {
        [FunctionName("Function1")]
        public void Run([BlobTrigger("sample-container/{name}", Connection = "BlobConnectionString")]Stream myBlob, string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
        }
    }
}

This configuration gives me expected response.

enter image description here

  1. In this approach, I didn't enable Use Azurite for runtime storage account.

enter image description here

Used Connect to dependency to connect to Azure Storage account and by doing this, I don't need to store the connection string in local settings file instead it will automatically store the connection string in Secrets.json file.

enter image description here

enter image description here

Your folder structure should look like below.

enter image description here

You will have the below codes in the mentioned files.

.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <RootNamespace>_78893453</RootNamespace>
    <UserSecretsId>9a7******c5ab4</UserSecretsId>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Azure.Storage.Blobs" Version="12.21.2" />
    <PackageReference Include="Azure.Storage.Files.Shares" Version="12.19.1" />
    <PackageReference Include="Azure.Storage.Queues" Version="12.19.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage.Blobs" Version="5.3.1" />
    <PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.5" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.4.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

local.settings.json

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_INPROC_NET8_ENABLED": "1",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

Secrets.json

{
  "BlobConnectionString": "DefaultEndpointsProtocol=https;AccountName=afrinstore1;AccountKey=EEpo******AStpzopOQ==;BlobEndpoint=https://afrinstore1.blob.core.windows.net/;TableEndpoint=https://afrinstore1.table.core.windows.net/;QueueEndpoint=https://afrinstore1.queue.core.windows.net/;FileEndpoint=https://afrinstore1.file.core.windows.net/",
  "BlobConnectionString:blob": "https://afrinstore1.blob.core.windows.net/",
  "BlobConnectionString:queue": "https://afrinstore1.queue.core.windows.net/"
}

I got expected response.

enter image description here

One might get this error message target machine actively refused it (127.0.0.1:10000) if Azurite is not running in the machine.

Upvotes: 0

Related Questions