简体   繁体   English

在我的 MVS Web 应用程序中为 Azure EventGrid Webhook 设置 HTTP 终结点?

[英]Setting up an HTTP endpoint for Azure EventGrid Webhook in my MVS Webapp?

I'm trying to create an Azure Eventgrid and Webhook response to send me inbound text messages from Azure communications services to my Azure hosted WebApp.我正在尝试创建 Azure Eventgrid 和 Webhook 响应,以将来自 Azure 通信服务的入站文本消息发送到我的 Azure 托管 Web 应用程序。 Right now I'm having a really hard time finding documentation that shows how to create an endpoint within a Webapp controller to get the Azure webhook response to handshake and validate.现在,我很难找到说明如何在 Webapp 控制器中创建端点以获取 Azure Webhook 响应以进行握手和验证的文档。 I've created an endpoint within my controller that I believe should be catching the the data and processing it in a POST method, but it fails because of the arguments I'm trying to mimic.我在我的控制器中创建了一个端点,我认为它应该捕获数据并在 POST 方法中处理它,但由于我试图模仿的参数而失败。 Any insight on this topic is appreciated.对这个主题的任何见解表示赞赏。

I tried integrating a lot of what I found in these Docs into my app controller to try and get it to work, but I think I might be doing this all the wrong way since it says this code is for an Azure function???我尝试将我在这些文档中找到的许多内容集成到我的应用程序控制器中以尝试使其工作,但我认为我可能以错误的方式执行此操作,因为它说此代码用于 Azure 函数??? I'm not entirely sure how those are used, but I tried integrating the same C# code into my controller.我不完全确定这些是如何使用的,但我尝试将相同的 C# 代码集成到我的控制器中。 See Docs below: https://docs.microsoft.com/en-us/azure/event-grid/receive-events请参阅下面的文档: https : //docs.microsoft.com/en-us/azure/event-grid/receive-events

And here is the controller I have that is trying to imitate what I read in the docs I linked这是我的控制器,它试图模仿我在链接的文档中阅读的内容

        [HttpPost("incoming")]
    public async Task<IActionResult> GetFlightInfo([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest incoming,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        string response = string.Empty;
        BinaryData events = await BinaryData.FromStreamAsync(incoming.Body);
        log.LogInformation($"Received events: {events}");

        EventGridEvent[] egEvents = EventGridEvent.ParseMany(events);

        foreach (EventGridEvent eventGridEvent in egEvents)
        {
            // Handle system events
            if (eventGridEvent.TryGetSystemEventData(out object eventData))
            {
                // Handle the subscription validation event
                if (eventData is SubscriptionValidationEventData subscriptionValidationEventData)
                {
                    log.LogInformation($"Got SubscriptionValidation event data, validation code: {subscriptionValidationEventData.ValidationCode}, topic: {eventGridEvent.Topic}");
                    // Do any additional validation (as required) and then return back the below response

                    var responseData = new SubscriptionValidationResponse()
                    {
                        ValidationResponse = subscriptionValidationEventData.ValidationCode
                    };
                    return new OkObjectResult(responseData);
                }
            }
        }
                return new OkObjectResult(response);
    }

I'd suggest to start by deploying and exploring the Azure Event Grid Viewer sample application according to the Handle SMS events for Delivery Reports and Inbound Messages tutorial.我建议首先根据Handle SMS events for Delivery Reports and Inbound Messages教程部署和探索 Azure 事件网格查看器示例应用程序。 This app is designed to consume any events generated by the Event Grid, including the SMS ones.此应用程序旨在使用事件网格生成的任何事件,包括 SMS 事件。 The app utilizes SignalR, just as @roman-kiss suggests in his answer , to push the events in near real time to the user.正如@roman-kiss在他的回答中所建议的那样,该应用程序利用 SignalR 将事件近乎实时地推送给用户。

Once you get a good grasp of the whole flow, you can start adjusting the code to match your use case.一旦您很好地掌握了整个流程,您就可以开始调整代码以匹配您的用例。 A good first step would be adjusting the deserialization logic to take advantage of more specific models.一个好的第一步是调整反序列化逻辑以利用更具体的模型。 You can get the sample JSON models for SMS events here and convert them to C# .您可以在此处获取 SMS 事件的示例 JSON 模型并将它们转换为C#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM