实际情况:
我有 2 个 blob 触发的 azure 函数
,它们运行良好(一个是 v2
,另一个是 v1
)另一方面,我在我的 azure Devops 中发布了一个 REST WEB API 应用程序
(公开加密和解密流的方法)(实际上仍然没有部署在 azure 门户上,实际上只添加了代码到天蓝色的 Devops 存储库)
I have a 2 blob triggered azure functions
that are working perfectly (one is v2
and the other v1
)
I have, in the other hand, a REST WEB API application
(that exposes methods to encrypt and decrypt a stream) published in my azure Devops (still not deployed on azure portal, actually, only code is added to an azure Devops repo)
-> 我想做的是:
通过我的 azure 函数中的 http 调用(调用加密或解密等)调用 Web api 应用程序来解密 blob 内容.
call the web api application via http calls(call encrypt or decrypt or whatever) from my azure function to decrypt blob content.
无需身份验证.
按照最佳实践的顺序,从我的 web api 制作一个 API APP 更合适,还是只是将我的 web api 项目作为 web 应用程序部署到 azure?为什么?
In order of best practices, is it more suitable to make an API APP from my web api, or just deploy my web api project as web app to azure? and why ?
换句话说,从我的 azure 函数调用 api 的最佳方式是什么?
In other terms, what is the best way to call an api from my azure function.?
谁能给我一些代码示例?
Can anyone provide me some code samples?
看来你想在 azure function
里面调用 API
这里是代码示例供你理解:
It seems you want to call API
inside your azure function
here is the code sample for your understanding:
在此函数中,我提供了一个 MPN 编号作为输入,该编号从 3rd 方 API
有效并返回 true
和 false
作为响应
In this Function I supplied a MPN number as Input which valid from a 3rd party API
and return true
and false
in response
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"
}
如果您仍有任何疑问,请随时分享.谢谢,编码愉快!
If you still have any query feel free to share. Thanks and happy coding!
这篇关于从 azure 函数调用 web api 的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!