c# - 从 azure 函数调用 Web api 的最佳方法

标签 c# azure httpclient azure-functions

实际情况:

我有一个 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 中有效,并在响应中返回 truefalse

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/

相关文章:

c# - 在 C# 中使用参数化查询时抛出异常

c# - Entity Framework 6 插入重复值

c# - Azure ServiceBus FaultTolerantAmqpObject`1'异常

asp.net-mvc - 当前上下文中不存在“HttpClientFactory”

java - HttpClient 4 静静地卡在 socketRead 上

delphi - 是否可以在 TIdHTTPServer onCommandGet 事件中使用 TIdHTTP(客户端)重定向请求并返回响应?

c# mvc 将请求重新路由到不同的服务器

Azure Windows Server 2016 上的 ASP.NET5 CLR 网站正在运行,但在浏览器中出现 404 NOT FOUND

asp.net - 错误 : A security token validation error occured for the received JWT token. HTTP 状态代码:未经授权

azure - 如何在另一个资源组中引用 SQL Server