c# - 如何从.NET Core API调用Azure门户中的函数App

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

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

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

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

提前致谢。

最佳答案

以下是如何将 Azure 函数调用到 .net core API Controller 中的示例。

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

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;
        }
    }

Note: Just replace your local host and put azure portal API URL

Controller 调用的测试函数:

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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d2b3f22202c37383f280d2b38232e39242223632e2220" rel="noreferrer noopener nofollow">[email protected]</a>";

            return req.CreateResponse(HttpStatusCode.OK, objContact);
        }
    }

我使用过的简单 ContactInformation 类:

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

PostMan 测试:

我已从 Post Man 调用 Controller 操作,并且它通过本地 Controller 操作成功从我的本地 azure 函数返回数据。请参阅下面的屏幕截图:

enter image description here

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

关于c# - 如何从.NET Core API调用Azure门户中的函数App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59874589/

相关文章:

c# - 编写通用 WCF 服务客户端配置/端点检查器?

azure - 如何设置 Azure 最大支出限额或成本上限金额?

sql - 删除临时表(如果 SQL Azure 上存在)

c# - 在 C# 中使用接口(interface)有哪些优点?

c# - 在 C# 中使用 string.Format 格式化 MAC 地址

.net - 从 System.__ComObject 基类型调用方法

c# - 如何将多线程与 "for"或 "foreach"循环一起使用?

c# - ASP.NET MVC 中的动态 LINQ 分组查询

api - Azure API 应用程序 - 如何定义登录策略

c# - 有没有办法使用 LINQ 从 List<T> 中删除某些元素?