Search code examples
azureazure-storageazure-functions

Azure function not recognizing cloud storage and queue classes


I am following along with this guide: https://learn.microsoft.com/en-us/azure/storage/queues/storage-dotnet-how-to-use-queues and attempting to create a simple queue in a time triggered function. It is not recognizing CloudStorageAcount, CloudConfigurationManager, CloudQueueClient, etc.

Here is my run.csx file

using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using System;

public static void Run(TimerInfo myTimer, TraceWriter log) {   
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

    // Retrieve storage account from connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

    // Create the queue client.
    CloudQueueClient queueClient = 
    storageAccount.CreateCloudQueueClient();

    // Retrieve a reference to a container.
    CloudQueue queue = queueClient.GetQueueReference("myqueue");

    // Create the queue if it doesn't already exist
    queue.CreateIfNotExists();
}

Here is my project.json file:

{
    "frameworks": {
        "net45":{
             "dependencies": {

                "Microsoft.WindowsAzure.ConfigurationManager" : "3.2.3",
                "Microsoft.WindowsAzure.Storage" : "8.0.0"
        },
         "frameworks": {
            "netcoreapp1.0": {
                "dependencies": {
                    "Microsoft.NETCore.App": {
                        "type": "platform",
                        "version": "1.0.0"
                    }
                },
            "imports": "dnxcore50"
                }
            }
        }
    }
}

Solution

  • The package Microsoft.WindowsAzure.Storage is referenced by Azure Functions itself by default. Remove the whole project.json file and add this line to the top of your function:

    #r "Microsoft.WindowsAzure.Storage"
    

    But you might not even need that. Azure Functions have higher-level API to work with Storage Queues, both for sending (output bindings) and receiving (triggers). Refer to Azure Queue storage bindings for Azure Functions.

    One more advice: it's preferred to use precompiled functions deployed as class libraries compiled with Visual Studio or VS Code. This way it's much easier to manage dependencies and troubleshoot.