首页 > 解决方案 > 在 Azure 环境中托管 functionapp 时从 Dictionary 读取值的问题

问题描述

我在 Azure 环境中有以下函数应用设置。我试图按照链接的输入并扩展它

https://mitra.computa.asia/articles/msdn-smart-image-re-sizing-azure-functions-and-cognitive-services

在这里,每当任何图像上传到容器时,我都会尝试生成两个不同大小的缩略图图像。

在这种情况下,我有一个输入:myBlob 和两个不同的输出:在 Integrate 部分下的 outputBlobsm、outputBlobmd。

功能应用代码:

using System;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Collections.Generic;


public static void Run(Stream myBlob,string blobname, string blobextension, Stream outputBlobsm,Stream outputBlobmd, TraceWriter log)
{
    bool smartCropping = true;

    log.Info($"C# Blob trigger function Processed blob\n Name:{blobname} \n Extension: {blobextension} extension");

    var sizesm = imageDimensionsTable[ImageSize.Small];
    log.Info($"C# Blob \n Sizesm:{sizesm}");
    log.Info($"C# Blob \n width:{sizesm.item1} \n height: {sizesm.item2}");
    ResizeImage(sizesm.item1, sizesm.item2,smartCropping,myBlob, outputBlobsm);

    var sizemd = imageDimensionsTable[ImageSize.Medium];
    log.Info($"C# Blob \n width:{sizemd.item1} \n height: {sizemd.item2}");
    ResizeImage(sizemd.item1, sizemd.item2,smartCropping,myBlob, outputBlobmd);
}

public void ResizeImage(int width, int height, bool smartCropping,Stream myBlob, Stream outputBlob)
{    
    string _apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxx";
    string _apiUrlBase = "xxxxxxxxxxxxxxxxxxx/generateThumbnail";
  using (var httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri(_apiUrlBase);
        httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _apiKey);
        using (HttpContent content = new StreamContent(myBlob))
        {
            //get response
            content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream");
            var uri = $"{_apiUrlBase}?width={width}&height={height}&smartCropping={smartCropping.ToString()}";
            var response = httpClient.PostAsync(uri, content).Result;
            var responseBytes = response.Content.ReadAsByteArrayAsync().Result;

            //write to output thumb
            outputBlob.Write(responseBytes, 0, responseBytes.Length);
        }
    }
}

public enum ImageSize { ExtraSmall, Small, Medium }

private static Dictionary<ImageSize, (int, int)> imageDimensionsTable = new Dictionary<ImageSize, (int, int)>() {
    { ImageSize.ExtraSmall, (320, 200) },
    { ImageSize.Small,      (640, 400) },
    { ImageSize.Medium,     (800, 600) }
};

在编译代码时,我收到以下错误:

[Error] run.csx(16,41): error CS1061: '(int, int)' does not contain a definition for 'item1' and no extension method 'item1' accepting a first argument of type '(int, int)' could be found (are you missing a using directive or an assembly reference?)

谁能帮我解决这个问题。

标签: c#azureazure-storageazure-functions

解决方案


C# 区分大小写。如果您没有指定名称,新的元组应该是 Item1, Item2....

请参阅“元组”下的示例

请注意,您还可以在声明中为这些整数指定名称:

 private static Dictionary<ImageSize, (int Width, int Height)> imageDimensionsTable

这使得

ResizeImage(sizesm.Width, sizesm.Height,smartCropping,myBlob, outputBlobsm);

更具可读性

哦......你可能需要制作ResizeImage静态


推荐阅读