c# - 在 Azure 环境中托管 functionapp 时从 Dictionary 读取值的问题
问题描述
我在 Azure 环境中有以下函数应用设置。我试图按照链接的输入并扩展它
在这里,每当任何图像上传到容器时,我都会尝试生成两个不同大小的缩略图图像。
在这种情况下,我有一个输入: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# 区分大小写。如果您没有指定名称,新的元组应该是 Item1, Item2....
请注意,您还可以在声明中为这些整数指定名称:
private static Dictionary<ImageSize, (int Width, int Height)> imageDimensionsTable
这使得
ResizeImage(sizesm.Width, sizesm.Height,smartCropping,myBlob, outputBlobsm);
更具可读性
哦......你可能需要制作ResizeImage
静态
推荐阅读
- django - 如何在每次页面刷新时更新 ModelAdmin 变量?
- machine-learning - 分类变量的降维
- macos - 如何取消最小化最小化窗口(苹果脚本不适用于从 Capitan 升级到 Mojave)
- javascript - 计算最小数量 使第一个字符和最后一个字符相等所需的操作
- javascript - 从 URL 下载 PDF 文件
- javascript - 在 Android WebView 中使用文本输入时防止退出全屏模式?
- r - R得到错误“`x`和`labels`必须是同一类型”
- spring - 如何将springboot项目转换为spring mvc
- python - Pandas Interpolate 为某些方法和某些大小的数据帧返回 ValueErrors
- windows - C# - 使用加密凭据以不同用户启动进程