我是Azure Event Grid和Webhooks的新手。
如何将我的.net mvc web api应用程序绑定到Microsoft Azure Event Grid?
简而言之,我想,只要将新文件添加到blob存储,Azure Event网格就应该通知我的web api应用程序。
我试过以下文章,但没有运气 https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-event-quickstart
答案 0 :(得分:3)
如何将我的.net mvc web api应用程序绑定到Microsoft Azure Event Grid? 简而言之,我想,只要将新文件添加到blob存储,Azure Event网格就应该通知我的web api应用程序。
我做了一个演示,它在我身边正常工作。您可以参考以下步骤:
1.使用函数
创建一个演示RestAPI项目 public string Post([FromBody] object value) //Post
{
return $"value:{value}";
}
2.如果我们想要使用Azure Event Grid集成azure存储,我们需要在位置 West US2 或 West Central US 中创建blob storage account。更多细节可以参考屏幕截图。
2.创建存储帐户键入事件订阅并绑定自定义API端点
3.将blob上载到blob存储并从Rest API检查。
答案 1 :(得分:2)
您可以通过创建一个自定义端点来完成此操作,该端点将订阅从Event Grid发布的事件。您引用的文档使用Request Bin作为订阅者。而是在MVC应用程序中创建Web API端点以接收通知。您必须支持验证请求才能使您拥有有效的订阅者,然后您就可以开始运行了。
示例:
public async Task<HttpResponseMessage> Post()
{
if (HttpContext.Request.Headers["aeg-event-type"].FirstOrDefault() == "SubscriptionValidation")
{
using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
{
var result = await reader.ReadToEndAsync();
var validationRequest = JsonConvert.DeserializeObject<GridEvent[]>(result);
var validationCode = validationRequest[0].Data["validationCode"];
var validationResponse = JsonConvert.SerializeObject(new {validationResponse = validationCode});
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(validationResponse)
};
}
}
// Handle normal blob event here
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}
答案 2 :(得分:0)
以下是如何使用Web API处理它的最新示例。您还可以从此处查看和部署工作示例:https://github.com/dbarkol/azure-event-grid-viewer
[HttpPost]
public async Task<IActionResult> Post()
{
using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
{
var jsonContent = await reader.ReadToEndAsync();
// Check the event type.
// Return the validation code if it's
// a subscription validation request.
if (EventTypeSubcriptionValidation)
{
var gridEvent =
JsonConvert.DeserializeObject<List<GridEvent<Dictionary<string, string>>>>(jsonContent)
.First();
// Retrieve the validation code and echo back.
var validationCode = gridEvent.Data["validationCode"];
return new JsonResult(new{
validationResponse = validationCode
});
}
else if (EventTypeNotification)
{
// Do more here...
return Ok();
}
else
{
return BadRequest();
}
}
}
public class GridEvent<T> where T: class
{
public string Id { get; set;}
public string EventType { get; set;}
public string Subject {get; set;}
public DateTime EventTime { get; set; }
public T Data { get; set; }
public string Topic { get; set; }
}
答案 3 :(得分:0)
您还可以使用Microsoft.Azure.EventGrid nuget包。
来自以下文章(贷记为gldraphael):https://gldraphael.com/blog/creating-an-azure-eventgrid-webhook-in-asp-net-core/
[Route("/api/webhooks"), AllowAnonymous]
public class WebhooksController : Controller
{
// POST: /api/webhooks/handle_ams_jobchanged
[HttpPost("handle_ams_jobchanged")] // <-- Must be an HTTP POST action
public IActionResult ProcessAMSEvent(
[FromBody]EventGridEvent[] ev, // 1. Bind the request
[FromServices]ILogger<WebhooksController> logger)
{
var amsEvent = ev.FirstOrDefault(); // TODO: handle all of them!
if(amsEvent == null) return BadRequest();
// 2. Check the eventType field
if (amsEvent.EventType == EventTypes.MediaJobStateChangeEvent)
{
// 3. Cast the data to the expected type
var data = (amsEvent.Data as JObject).ToObject<MediaJobStateChangeEventData>();
// TODO: do your thing; eg:
logger.LogInformation(JsonConvert.SerializeObject(data, Formatting.Indented));
}
// 4. Respond with a SubscriptionValidationResponse to complete the
// event subscription handshake.
if(amsEvent.EventType == EventTypes.EventGridSubscriptionValidationEvent)
{
var data = (amsEvent.Data as JObject).ToObject<SubscriptionValidationEventData>();
var response = new SubscriptionValidationResponse(data.ValidationCode);
return Ok(response);
}
return BadRequest();
}
}