我試圖設置WebHookHandler
來接收.Net Core
項目的Json數據。我知道Webhook
理論上是如何工作的。如何爲.Net Core手動設置WebHookReceiver?
有大量的信息可用於接收WebHooks,但sample source code。但我需要一個.Net Core
的例子嗎?
我試圖設置WebHookHandler
來接收.Net Core
項目的Json數據。我知道Webhook
理論上是如何工作的。如何爲.Net Core手動設置WebHookReceiver?
有大量的信息可用於接收WebHooks,但sample source code。但我需要一個.Net Core
的例子嗎?
我們要支持ASP.NET核心網絡掛接支持網絡掛接,但它仍然是在作品中。與此同時,您可能可以查看處理程序代碼併爲ASP.NET Core執行類似的操作。
希望這會有所幫助!
亨裏克
這裏,我得到了與Azure的警報工作非常簡單的例子:
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ApplicationInsightsMonitor.Controllers
{
[Produces("application/json")]
[Route("api/alerts")]
public class AlertController : Controller
{
[HttpPost]
public async Task<ActionResult> Post([FromBody] AIPayloadModel payload)
{
if (payload == default(AIPayloadModel))
{
return NotFound();
}
// Save to database
return Ok();
}
}
public class AIPayloadModel
{
public enum AIPayloadStatus
{
Activated,
Resolved
}
public class AIPayloadContextModel
{
public enum AIConditionType
{
Metric,
Event
}
public enum AIMetricUnit
{
Bytes,
BytesPerSecond,
Count,
CountPerSecond,
Percent,
Seconds
}
public enum AIAggregation
{
Average,
Last,
Maximum,
Minimum,
None,
Total
}
public class AICondition
{
[JsonProperty("metricName")]
public String Name { get; set; }
[JsonProperty("metricUnit")]
public AIMetricUnit Units { get; set; }
[JsonProperty("metricValue")]
public Decimal Value { get; set; }
[JsonProperty("threshold")]
public Decimal Threshold { get; set; }
[JsonProperty("windowSize")]
public TimeSpan WindowSize { get; set; }
[JsonProperty("timeAggregation")]
public AIAggregation Aggregation { get; set; }
[JsonProperty("operator")]
public String Operator { get; set; }
}
[JsonProperty("timestamp")]
public DateTime Time { get; set; }
[JsonProperty("id")]
public String Id { get; set; }
[JsonProperty("name")]
public String Name { get; set; }
[JsonProperty("description")]
public String Description { get; set; }
[JsonProperty("conditionType")]
public AIConditionType ConditionType { get; set; }
[JsonProperty("condition")]
public AICondition Condition { get; set; }
[JsonProperty("subscriptionId")]
public String SubscriptionId { get; set; }
[JsonProperty("resourceGroupName")]
public String ResourceGroupName { get; set; }
[JsonProperty("resourceGroupType")]
public String ResourceGroupType { get; set; }
[JsonProperty("resourceName")]
public String ResourceName { get; set; }
[JsonProperty("resourceType")]
public String ResourceType { get; set; }
[JsonProperty("resourceRegion")]
public String ResourceRegion { get; set; }
[JsonProperty("portalLink")]
public String PortalLink { get; set; }
}
[JsonProperty(PropertyName = "status")]
public AIPayloadStatus Status { get; set; }
[JsonProperty(PropertyName = "context")]
public AIPayloadContextModel Context { get; set; }
[JsonProperty(PropertyName = "properties")]
public Dictionary<String, String> Properties { get; set; } = new Dictionary<String, String>();
}
}
的關鍵是在參數使用[FromBody]和具有正好在你的模型正確的JSON反序列化。
.Net核心是未來計劃:https://github.com/aspnet/WebHooks/issues/5 –
當然,我在GitHub主題中提出了同樣的問題。無論如何,我現在知道答案。謝謝! –
。GitHub提供了核心支持:https://github.com/aspnet/WebHooks/blob/dev/README.md –