c# - 如何从本地调用另一个函数应用程序?

标签 c# azure azure-functions azure-function-app

在 Visual Studio 中,我创建了 2 个 Azure 函数应用 f1f2

我已经更改了两个函数应用的端口。

我想从 f1 调用 f2,但收到 NotFound 错误。

最佳答案

我尝试过在同一项目和不同项目中调用一个函数另一个函数,两者都工作正常。

函数 1 示例:

 [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        try
        {
            //Extract Request Param

            var content = await new StreamReader(req.Body).ReadToEndAsync();
            QnAMakerQuestion objQnAMakerQuestion = JsonConvert.DeserializeObject<QnAMakerQuestion>(content);

            //Global Variable for containing message

            dynamic validationMessage;

            // Validate param

            if (string.IsNullOrEmpty(objQnAMakerQuestion.question))
            {
                validationMessage = new OkObjectResult("Question is required!");
                return (IActionResult)validationMessage;
            }
            //Selialize Request Param
            var json = JsonConvert.SerializeObject(objQnAMakerQuestion);
            var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
            // Call Function 2 
            HttpClient newClient = new HttpClient();
            HttpResponseMessage responseFromAnotherFunction = await newClient.PostAsync("http://localhost:7073/api/Function2FromApp2", stringContent);
            dynamic response = "";

            if (responseFromAnotherFunction.IsSuccessStatusCode)
            {
                response = responseFromAnotherFunction.Content.ReadAsStringAsync().Result;
            }

            validationMessage = new OkObjectResult(response);
            return (IActionResult)validationMessage;
        }
        catch (Exception ex)
        {
            dynamic validationMessage = new OkObjectResult(string.Format("Something went wrong, please try agian! Reason:{0}", ex.Message));
            return (IActionResult)validationMessage;
        }
    }

函数 2 示例:

  [FunctionName("Function2FromApp2")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            try
            {
                var content = await new StreamReader(req.Body).ReadToEndAsync();
                QnAMakerQuestion objQnAMakerQuestion = JsonConvert.DeserializeObject<QnAMakerQuestion>(content);

                //Global Variable for containing message

                dynamic validationMessage;

                // Validate param

                if (string.IsNullOrEmpty(objQnAMakerQuestion.question))
                {
                    validationMessage = new OkObjectResult("Question is required!");
                    return (IActionResult)validationMessage;
                }
                validationMessage = new OkObjectResult(objQnAMakerQuestion);
                return (IActionResult)validationMessage;
            }
            catch (Exception ex)
            {

                dynamic validationMessage = new OkObjectResult(string.Format("Something went wrong, please try agian! Reason:{0}", ex.Message));
                return (IActionResult)validationMessage;
            }
        }

使用的类:

   public class QnAMakerQuestion
    {
        public string question { get; set; }

    }

Note: If you run in same project then you wouldn't encounter any problem. But if you run in different project encounter a issue regarding port. To resolve that in local.settings.json file replace below code :

"Host": {

    "LocalHttpPort": 7073

  }

并且更新项目属性 -> 调试到以下内容

host start --port 7073 --pause-on-error 请参阅下面的屏幕截图:

enter image description here

postman 测试:

我在 PostMan 上调用了函数 1,它调用了函数 1 作为函数 1 调用了函数 2 并发送从函数 2函数 1 的响应,反之亦然。请参阅下面的屏幕截图:

enter image description here

即插即用,如果您有任何其他问题,请告诉我。

关于c# - 如何从本地调用另一个函数应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60227945/

相关文章:

C# 泛型与 C++ 模板的比较

c# - #CDC "Send failure: 501 out of bounds exception"向服务器发送数据时

c# - 使用 C# 的暂停事件处理

azure - 卡夫卡与 SignalR

.net - Azure Function .net 框架

c# - 是否可以根据另一个属性通知 C# 编译器另一个属性将是非空的?

deployment - 自动化 Windows Azure 部署的最佳方法是什么?

azure - Nifi java.lang.NoSuchMethodError : org. apache.hadoop.conf.Configuration.reloadExistingConfigurations

Azure Functions - 找不到文件

azure - 如何禁用默认的azure功能日志