【问题标题】:How to call function App in Azure portal from .NET Core API如何从 .NET Core API 在 Azure 门户中调用函数 App
【发布时间】:2020-01-23 08:52:11
【问题描述】:

我必须在 Azure 门户中设置一个函数应用,并且需要通过我的 .NET Core 应用程序 (API) 访问。

主要是我需要一个函数,其中必须将 3 个参数传递给函数应用程序(来自 C# 代码)并接受应为数据表格式的返回值。

由于我对此很陌生,所以我对可行性和实施技术了解不多。如果有人用详细的示例进行解释,那将非常有帮助。

提前致谢。

【问题讨论】:

    标签: c# .net azure azure-function-app


    【解决方案1】:

    以下是如何将 azure 函数调用到 .net 核心 API 控制器的示例。

    我有一个简单的天蓝色函数,一旦调用它就会返回一个名称和电子邮件。让我们看看下面的例子:

    public class InvokeAzureFunctionController : ApiController
        {
            // GET api/<controller>
            public async System.Threading.Tasks.Task<IEnumerable<object>> GetAsync()
            {
                HttpClient _client = new HttpClient();
                HttpRequestMessage newRequest = new HttpRequestMessage(HttpMethod.Get, "http://localhost:7071/api/FunctionForController");
                HttpResponseMessage response = await _client.SendAsync(newRequest);
    
                dynamic responseResutls = await response.Content.ReadAsAsync<dynamic>();
                return responseResutls;
            }
        }
    

    注意:只需替换您的本地主机并放入 azure portal API URL

    控制器调用的测试函数:

    public static class FunctionForController
        {
            [FunctionName("FunctionForController")]
            public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
            {
                log.Info("C# HTTP trigger function processed a request.");
    
                // parse query parameter
                string name = req.GetQueryNameValuePairs()
                    .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                    .Value;
    
                if (name == null)
                {
                    // Get request body
                    dynamic data = await req.Content.ReadAsAsync<object>();
                    name = data?.name;
                }
    
                ContactInformation objContact = new ContactInformation();
    
                objContact.Name = "From Azure Function";
                objContact.Email = "fromazure@function.com";
    
                return req.CreateResponse(HttpStatusCode.OK, objContact);
            }
        }
    

    我使用过的简单 ContactInformation 类:

       public class ContactInformation
        {
            public string Name { get; set; }
            public string Email { get; set; }
        }
    

    邮递员测试:

    我已经从 Post Man 调用了 controller action,它通过 local controller action 成功地从我的本地 azure 函数返回了数据。请参阅下面的屏幕截图:

    希望你能理解。即插即用。

    【讨论】:

      【解决方案2】:

      您可以创建 HTTP 触发的 Azure 函数。 HTTP 触发的 Azure 函数有一个公共 URL,您的应用程序可以使用它来调用 Azure 函数。

      然后您可以根据应用程序的需要在 GET 或 POST 请求中发送参数。

      Azure 函数将返回 HTTP 响应。

      更多详情请参考this documentation page

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-06
        • 2023-01-21
        • 2023-02-22
        • 1970-01-01
        • 2016-07-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多