Azure function not working “Cannot declare namespace in script code”

后端 未结 1 1616
我寻月下人不归
我寻月下人不归 2021-01-23 15:09

I am a java microservices person - not much into .NET.

With the help from StackOverflow community .. I was able to get a working code for my problem.

The code is w

相关标签:
1条回答
  • 2021-01-23 16:01

    If you want to use c# as script, you must remove the namespace (as the error describes) and also add a Run method:

    //PS: review which package will you use
    #r "Microsoft.WindowsAzure.Storage"
    #r "Azure.Storage.Blobs"
    #r "Microsoft.Azure.Storage.Blob"
    
    using Azure.Storage.Blobs;
    using Azure.Storage.Blobs.Models;
    using Microsoft.Azure.Storage.Blob;
    using Microsoft.Extensions.Logging;
    using Microsoft.WindowsAzure.Storage;
    using System;
    
    public static void Run(CloudQueueMessage myQueueItem, ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem.AsString}");
        await CopyBlob();
    }
    
    static async Task CopyBlob()
            {
                BlobServiceClient blobServiceClient = new BlobServiceClient(storageconnstring);
    
                BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    
                BlobClient blobClient = containerClient.GetBlobClient(filename);
                var blobUri = blobClient.Uri;
    
                BlobContainerClient targetContainerClient = blobServiceClient.GetBlobContainerClient("demo-copy");//This is the container where we want to copy the blob
                BlobClient targetBlobClient = targetContainerClient.GetBlobClient(filename);
                await targetBlobClient.StartCopyFromUriAsync(blobUri);
            }
    

    if you want to keep with your local development experience, you'd better use Azure Functions template in your visual studio. More info:

    https://docs.microsoft.com/en-us/azure/azure-functions/functions-develop-vs

    https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp

    https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library

    0 讨论(0)
提交回复
热议问题