【问题标题】:Best approach to call web api from azure function [closed]从 azure 函数调用 web api 的最佳方法 [关闭]
【发布时间】:2019-05-24 08:25:05
【问题描述】:

实际情况:

我有 2 个运行良好的 blob triggered azure functions(一个是 v2,另一个是 v1) 另一方面,我在我的 azure Devops 中发布了一个 REST WEB API application(公开加密和解密流的方法)(实际上,仍然没有部署在 azure 门户上,只有代码被添加到 azure Devops repo)

-> 我想做的是:

通过我的 azure 函数中的 http 调用(调用加密或解密等)调用 Web api 应用程序来解密 blob 内容。

无需身份验证。

按照最佳实践的顺序,从我的 web api 制作一个 API APP 更合适,还是将我的 web api 项目作为 web 应用程序部署到 azure 更合适?为什么?

换句话说,从我的 azure 函数调用 api 的最佳方式是什么?

谁能给我一些代码示例?

【问题讨论】:

  • 看一看。如果您有任何疑问,请告诉我。谢谢,编码愉快!

标签: c# azure httpclient azure-functions


【解决方案1】:

您似乎想在您的azure function 中调用API,这是供您理解的代码示例:

在这个函数中,我提供了一个 MPN 号作为输入,它从 3rd party 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"
}

如果您仍有任何疑问,请随时分享。谢谢,编码愉快!

【讨论】:

  • run.csx(10,42): 错误 CS0234: 类型或命名空间名称 'Http' 在命名空间 'Microsoft.Azure.WebJobs.Extensions' 中不存在(您是否缺少程序集引用?)
  • 请在新线程中发布您的问题以及更多信息,以便可以复制。我会在那里回答。因为这个上下文不同于你的问题。希望你明白我的意思谢谢
猜你喜欢
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 2015-04-12
相关资源
最近更新 更多