Azure函数不起作用“无法在脚本代码中声明名称空间" [英] Azure function not working "Cannot declare namespace in script code"

查看:41
本文介绍了Azure函数不起作用“无法在脚本代码中声明名称空间"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java微服务人员-对.NET的了解不多.

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

在StackOverflow社区的帮助下..我能够获得针对我的问题的有效代码.

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

代码在本地完美运行..

The code is working perfectly locally ..

但是当我尝试将其作为功能移至Azure时.我遇到了错误..

But when I tried to move it to Azure as a function . I am getting an error ..

请指导.

代码 在本地工作正常

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Microsoft.Azure.Storage.Blob;
using System;
using System.IO;
using System.Threading.Tasks;

namespace whizlabblob
{
    class Program
    {
        static string storageconnstring = "DefaultEndpoint*******************************";
        static string containerName = "demo";
        static string filename = "sample.txt";
        static string filepath="C:\\Work\\sample.txt";
        static string downloadpath = "C:\\Work\\sample2.txt";
        static async Task Main(string[] args)
        {
            //Container().Wait();
            //CreateBlob().Wait();
            //GetBlobs().Wait();
            // GetBlob().Wait();
            CopyBlob().Wait();
            Console.WriteLine("Complete");
            Console.ReadKey();
        }

        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);
        }

}
}

错误 在Azure上

Connected!
2020-06-26T13:57:05Z   [Information]   Executing 'Functions.HttpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=a6869c88-b1ba-4c55-9671-9be26feb66ba)
2020-06-26T13:57:05Z   [Error]   Function compilation error
2020-06-26T13:57:05Z   [Error]   run.csx(8,1): error CS7021: Cannot declare namespace in script code
2020-06-26T13:57:05Z   [Error]   run.csx(1,7): error CS0246: The type or namespace name 'Azure' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:05Z   [Error]   run.csx(2,7): error CS0246: The type or namespace name 'Azure' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:05Z   [Error]   run.csx(3,23): error CS0234: The type or namespace name 'Storage' does not exist in the namespace 'Microsoft.Azure' (are you missing an assembly reference?)
2020-06-26T13:57:05Z   [Warning]   run.csx(17,27): warning CS7022: The entry point of the program is global script code; ignoring 'Program.Main(string[])' entry point.
2020-06-26T13:57:05Z   [Warning]   run.csx(17,27): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
2020-06-26T13:57:05Z   [Error]   run.csx(27,13): error CS0246: The type or namespace name 'BlobServiceClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Error]   run.csx(27,55): error CS0246: The type or namespace name 'BlobServiceClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Error]   run.csx(29,13): error CS0246: The type or namespace name 'BlobContainerClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Error]   run.csx(31,13): error CS0246: The type or namespace name 'BlobClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Error]   run.csx(34,13): error CS0246: The type or namespace name 'BlobContainerClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Error]   run.csx(35,13): error CS0246: The type or namespace name 'BlobClient' could not be found (are you missing a using directive or an assembly reference?)
2020-06-26T13:57:06Z   [Warning]   run.csx(15,23): warning CS0414: The field 'Program.filepath' is assigned but its value is never used
2020-06-26T13:57:06Z   [Warning]   run.csx(16,23): warning CS0414: The field 'Program.downloadpath' is assigned but its value is never used
2020-06-26T13:57:06Z   [Error]   Executed 'Functions.HttpTrigger1' (Failed, Id=a6869c88-b1ba-4c55-9671-9be26feb66ba)

推荐答案

如果要使用c#作为脚本,则必须删除名称空间(如错误所述),并添加Run方法:

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);
        }

如果要保持本地开发经验,最好在Visual Studio中使用Azure Functions模板.更多信息:

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/zh-CN/azure/azure-functions/functions-dotnet-class-library

这篇关于Azure函数不起作用“无法在脚本代码中声明名称空间"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆