• <bdo id='oIzBa'></bdo><ul id='oIzBa'></ul>

  1. <small id='oIzBa'></small><noframes id='oIzBa'>

    <tfoot id='oIzBa'></tfoot>
    1. <i id='oIzBa'><tr id='oIzBa'><dt id='oIzBa'><q id='oIzBa'><span id='oIzBa'><b id='oIzBa'><form id='oIzBa'><ins id='oIzBa'></ins><ul id='oIzBa'></ul><sub id='oIzBa'></sub></form><legend id='oIzBa'></legend><bdo id='oIzBa'><pre id='oIzBa'><center id='oIzBa'></center></pre></bdo></b><th id='oIzBa'></th></span></q></dt></tr></i><div id='oIzBa'><tfoot id='oIzBa'></tfoot><dl id='oIzBa'><fieldset id='oIzBa'></fieldset></dl></div>

      <legend id='oIzBa'><style id='oIzBa'><dir id='oIzBa'><q id='oIzBa'></q></dir></style></legend>
    2. 从 azure 函数调用 web api 的最佳方法

      时间:2023-10-05
        <bdo id='ugINi'></bdo><ul id='ugINi'></ul>

            <tfoot id='ugINi'></tfoot>
            <i id='ugINi'><tr id='ugINi'><dt id='ugINi'><q id='ugINi'><span id='ugINi'><b id='ugINi'><form id='ugINi'><ins id='ugINi'></ins><ul id='ugINi'></ul><sub id='ugINi'></sub></form><legend id='ugINi'></legend><bdo id='ugINi'><pre id='ugINi'><center id='ugINi'></center></pre></bdo></b><th id='ugINi'></th></span></q></dt></tr></i><div id='ugINi'><tfoot id='ugINi'></tfoot><dl id='ugINi'><fieldset id='ugINi'></fieldset></dl></div>

              <legend id='ugINi'><style id='ugINi'><dir id='ugINi'><q id='ugINi'></q></dir></style></legend>
                <tbody id='ugINi'></tbody>

                <small id='ugINi'></small><noframes id='ugINi'>

                本文介绍了从 azure 函数调用 web api 的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                实际情况:

                我有 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 有效并返回 truefalse 作为响应

                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 的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Azure Function V2 服务总线消息延迟 下一篇:本地实现的 Azure 功能在云中不起作用

                相关文章

                • <bdo id='i01xh'></bdo><ul id='i01xh'></ul>

                  1. <small id='i01xh'></small><noframes id='i01xh'>

                    <legend id='i01xh'><style id='i01xh'><dir id='i01xh'><q id='i01xh'></q></dir></style></legend>
                  2. <i id='i01xh'><tr id='i01xh'><dt id='i01xh'><q id='i01xh'><span id='i01xh'><b id='i01xh'><form id='i01xh'><ins id='i01xh'></ins><ul id='i01xh'></ul><sub id='i01xh'></sub></form><legend id='i01xh'></legend><bdo id='i01xh'><pre id='i01xh'><center id='i01xh'></center></pre></bdo></b><th id='i01xh'></th></span></q></dt></tr></i><div id='i01xh'><tfoot id='i01xh'></tfoot><dl id='i01xh'><fieldset id='i01xh'></fieldset></dl></div>

                  3. <tfoot id='i01xh'></tfoot>