简体   繁体   中英

Issue in reading values from Dictionary in case of functionapp hosted on Azure environment

I have the following function app setup on Azure environment. I tried to follow the inputs from the link and extended it

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

Here I am trying to generate two different sized thumbnail images whenever any image is uploaded to the container.

I have one input: myBlob and two different outputs added in this case: outputBlobsm, outputBlobmd under the Integrate section.

Function App Code:

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

On compiling the code I am getting the below error:

[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?)

Can anyone help me to fix this issue.

C# is case sensitive. The new tuples if you haven't specified names should be Item1, Item2....

See examples here under "Tuples"

Note that you can also specify names for those ints in the declaration:

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

which makes

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

much more readable

Oh... and you'll might need to make ResizeImage static

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM