在Azure环境上托管functionapp的情况下,从Dictionary中读取值的问题

时间:2018-09-05 15:55:03

标签: c# azure azure-storage azure-functions

我在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?)

任何人都可以帮助我解决此问题。

1 个答案:

答案 0 :(得分:1)

C#区分大小写。如果您未指定名称,则新的元组应为Item1,Item2。...

See examples here under "Tuples"

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

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

这使得

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

更具可读性

哦...,您可能需要将ResizeImage设为静态