gpt4 book ai didi

.net - Azure功能不工作 "Cannot declare namespace in script code"

转载 作者:行者123 更新时间:2023-12-03 00:59:40 29 4
gpt4 key购买 nike

我是一名 Java 微服务人员 - 对 .NET 不太了解。

在 StackOverflow 社区的帮助下......我能够获得解决我的问题的工作代码。

代码在本地完美运行..

但是当我尝试将其作为函数移动到 Azure 时。我收到错误..

请指导。

代码 本地工作正常

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 方法:

//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 模板。更多信息:

Azure Functions Visual Studio Tools

Creating a new Azure Functions using Visual Studio

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

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

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

关于.net - Azure功能不工作 "Cannot declare namespace in script code",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62596741/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com