【问题标题】:Error when trying to access Azure Function尝试访问 Azure Function 时出错
【发布时间】:2018-07-25 08:11:33
【问题描述】:

我在 Azure 上有一个匿名函数,使用 Microsoft.NET.Sdk.Functions (1.0.13) 成功部署 (.netstandard 2.0),但由于某种原因它突然停止工作,当我调用它时,响应是:

<ApiErrorModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Microsoft.Azure.WebJobs.Script.WebHost.Models">
<Arguments xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true"/>
<ErrorCode>0</ErrorCode>
<ErrorDetails i:nil="true"/>
<Id>91fab400-3447-4913-878f-715d9d4ab46b</Id>
<Message>
An error has occurred. For more information, please check the logs for error ID 91fab400-3447-4913-878f-715d9d4ab46b
</Message>
<RequestId>0fb00298-733d-4d88-9e73-c328d024e1bb</RequestId>
<StatusCode>InternalServerError</StatusCode>
</ApiErrorModel>

如何解决?

编辑:当我在本地启动 AF 环境并运行该功能时,它按预期工作,没有任何问题,尽管我在控制台中看到的是红色消息:

并搜索它,偶然发现了这篇 GitHub 帖子: https://github.com/Azure/azure-functions-host/issues/2765

我注意到这部分的地方:

@m-demydiuk 注意到 azure 函数可以在控制台中处理此错误。所以这个红色错误不会破坏本地机器上的功能。但我担心它可能会在其他环境中引起任何问题。

这让我很困扰。会不会是问题?

我使用了一个版本与我的目标框架不匹配的库,但在本地仍然可以正常工作,而且之前在 Azure 上也可以正常工作

我的主机版本是“Version=2.0.11651.0”

这是整个函数:

public static class Function1
    {
        [FunctionName("HTML2IMG")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
        {
            string url = req.Query["url"];

            byte[] EncodedData = Convert.FromBase64String(url);
            string DecodedURL = Encoding.UTF8.GetString(EncodedData);

            string requestBody = new StreamReader(req.Body).ReadToEnd();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            DecodedURL = DecodedURL ?? data?.name;
            var api = new HtmlToPdfOrImage.Api("9123314e-219c-342d-a763-0a3dsdf8ad21", "vmZ31vyg");

            var ApiResult = api.Convert(new Uri($"{DecodedURL}"), new HtmlToPdfOrImage.GenerateSettings() { OutputType = HtmlToPdfOrImage.OutputType.Image });

            string BlobName = Guid.NewGuid().ToString("n");
            string ImageURL = await CreateBlob($"{BlobName}.png", (byte[])ApiResult.model, log);
            var Result = new HttpResponseMessage(HttpStatusCode.OK);
            var oJSON = new { url = ImageURL, hash = BlobName };
            var jsonToReturn = JsonConvert.SerializeObject(oJSON);

            Result.Content = new StringContent(jsonToReturn);
            Result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            return Result;
        }

        private async static Task<string> CreateBlob(string name, byte[] data, TraceWriter log)
        {
            string accessKey = "xxx";
            string accountName = "xxx";
            string connectionString = "DefaultEndpointsProtocol=https;AccountName=" + accountName + ";AccountKey=" + accessKey + ";EndpointSuffix=core.windows.net";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient client = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = client.GetContainerReference("images");

            await container.CreateIfNotExistsAsync();

            BlobContainerPermissions permissions = await container.GetPermissionsAsync();
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            await container.SetPermissionsAsync(permissions);

            CloudBlockBlob blob = container.GetBlockBlobReference(name);
            blob.Properties.ContentType = "image/png";

            using (Stream stream = new MemoryStream(data))
            {
                await blob.UploadFromStreamAsync(stream);
            }

            return blob.Uri.AbsoluteUri;
        }

双重编辑: 我在 VS 中创建了空的 V2 .net 核心 AF 并立即发布 -> 我得到了同样的错误......我什至将 Microsoft.NET.Sdk.Function 更新为最新的 1.0.14 而不是 1.0.13 并且仍然得到同样的错误。显然 Azure 或 Visual Studio (15.7.5) 中的某些东西坏了?!?!

【问题讨论】:

  • 能否展示一下azure函数的代码?
  • 不,但它在 localhost 上完美运行,所以我认为问题不在于代码隐藏
  • bcoz 它的显示内部服务错误,所以我们必须检查哪一行在实时而不是在本地给出错误
  • 我也想查看此信息,但是否可以从 Azure 门户的某个地方获取它,也许是一些日志?
  • @nmrlqa4 java_home 提示不是问题所在。您能否告诉我们您的函数主机版本,请在您的 CLI 输出 Starting Host (...,Version=2.xxx,...) 中查看。

标签: c# .net visual-studio azure azure-functions


【解决方案1】:

解决方案

在 Azure 门户上,转到函数应用设置检查您的运行时版本。可能是Runtime version: 1.0.11913.0 (~1) 在你身边。这就是问题。在应用程序设置中将 FUNCTIONS_EXTENSION_VERSION 更改为 beta,您的代码应该可以在 Azure 上运行。

说明

您创建一个 v2 函数,因为您的本地主机版本是 2.0.11651.0。所以函数运行时也应该是beta 2.x(最新是2.0.11933.0)在线。

之前你从VS发布函数的时候,你可能看到过这个提示

你可能选择了No,所以你得到了错误。

请注意,如果我们通过 VSTS 或 Git 等 CI/CD 发布,则此类通知不可用。所以我们需要确保这些配置设置正确。

建议

如您所见,您的本地主机版本为 2.0.11651,低于 Azure 上的 2.0.11933。我建议您将Azure Functions and Web Jobs Tools(在 VS 菜单上,工具->扩展和更新)更新为最新(15.0.40617.0),以便 VS 使用最新的函数运行时。

至于您的代码,我建议您创建images 容器并在门户上手动设置其公共访问级别,因为此过程只需要执行一次。

然后我们可以使用blob output bindings

  1. 使用存储连接字符串将StorageConnection 添加到应用程序设置。如果您的images 容器在函数应用使用的存储帐户中(应用程序设置中的AzureWebJobsStorge),请忽略此步骤并删除下面的Connection 参数,因为绑定默认使用该存储帐户。

    李>
  2. 添加 blob 输出绑定

    public static async Task<HttpResponseMessage> Run(...,TraceWriter log,
            [Blob("images", FileAccess.Read, Connection = "StorageConnection")] CloudBlobContainer container)
    
  3. 更改 CreateBlob 方法

    private async static Task<string> CreateBlob(string name, byte[] data, TraceWriter log, CloudBlobContainer container)
    {
        CloudBlockBlob blob = container.GetBlockBlobReference(name);
        blob.Properties.ContentType = "image/png";
    
        using (Stream stream = new MemoryStream(data))
        {
            await blob.UploadFromStreamAsync(stream);
        }
    
        return blob.Uri.AbsoluteUri;
    }
    

【讨论】:

  • 通过 CI 部署(即通过 VSTS)时,您不会收到弹出通知,而且这个错误可能很烦人。
  • @Peter 感谢您提供有用的信息,我会做一些测试并稍后更新我的答案。
猜你喜欢
  • 2021-01-28
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-07
  • 2015-04-11
  • 2012-03-18
  • 1970-01-01
相关资源
最近更新 更多