实际情况:
我有一个 2 blob triggered azure functions
运行完美(一个是 v2
,另一个是 v1
)
另一方面,我有一个 REST WEB API application
(公开了加密和解密流的方法)在我的 azure Devops 中发布(仍未部署在 azure 门户上,实际上,仅将代码添加到 azure Devops 存储库中)
-> 我想做的是:
通过 http 调用(调用加密或解密或其他方式)从我的 azure 函数调用 Web API 应用程序来解密 blob 内容。
无需身份验证。
按照最佳实践的顺序,从我的 Web api 制作 API 应用程序更合适,还是只是将我的 Web api 项目作为 Web 应用程序部署到 azure 更合适?为什么?
换句话说,从我的 azure 函数调用 api 的最佳方式是什么?
谁能给我一些代码示例吗?
最佳答案
您似乎想在azure函数
中调用API
,这里是供您理解的代码示例:
在此函数中,我提供了一个 MPN 号码作为输入,该号码在第 3 方 API
中有效,并在响应中返回 true
和 false
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net;
using System.Text;
namespace HaithemKAROUIApiCase.Functions
{
public static class HaithemKAROUIApiCaseClass
{
[FunctionName("HaithemKAROUIApiCaseFunction")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
// Convert all request param into Json object
var content = req.Content;
string jsonContent = content.ReadAsStringAsync().Result;
dynamic requestPram = JsonConvert.DeserializeObject<PartnerMpnModel>(jsonContent);
// Extract each param
//string mpnId = requestPram.mpnId;
if (string.IsNullOrEmpty(requestPram.MpnID))
{
return req.CreateResponse(HttpStatusCode.OK, "Please enter the valid partner Mpn Id!");
}
// Call Your API
HttpClient newClient = new HttpClient();
HttpRequestMessage newRequest = new HttpRequestMessage(HttpMethod.Get, string.Format("YourAPIURL?mpnId={0}", requestPram.MpnID));
//Read Server Response
HttpResponseMessage response = await newClient.SendAsync(newRequest);
bool isValidMpn = await response.Content.ReadAsAsync<bool>();
//Return Mpn status
return req.CreateResponse(HttpStatusCode.OK, new PartnerMpnResponseModel { isValidMpn = isValidMpn });
}
catch (Exception ex)
{
return req.CreateResponse(HttpStatusCode.OK, "Invaild MPN Number! Reason: {0}", string.Format(ex.Message));
}
}
}
public class PartnerMpnModel
{
public string MpnID { get; set; }
}
public class PartnerMpnResponseModel
{
public bool isValidMpn { get; set; }
}
}
请求格式
{
"MpnID": "123456789"
}
如果您还有任何疑问,请随时分享。谢谢,编码愉快!
关于c# - 从 azure 函数调用 Web api 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56288606/