简体   繁体   English

Azure EventGridEvent 忽略 JsonProperty

[英]Azure EventGridEvent ignores JsonProperty

I send an Event to an Azure EventGrid Topic:我将事件发送到 Azure EventGrid 主题:

public void SendMsg(string eventType, string dataVersion, object payload) {
var cloudEvent = new EventGridEvent("myevent", eventType, dataVersion, payload)
{
  Topic = _configuration.Topic
};
await _client.SendEventAsync(cloudEvent);
}

(_client is of Type EventGridPublisherClient ) (_client 是EventGridPublisherClient类型)

payload has the following Content: payload具有以下内容:

using NewtonSoft.Json;
public class CuData {
[JsonConverter(typeof(StringEnumConverter))]
public enum CancellationTypes
{
  Regular,
  Special,
  Relocation
}

[JsonProperty("type")]  
public CancellationTypes CancellationType {get;set;}
}

Now the event contains something like this:现在该事件包含如下内容:

{
"CancellationType":0
}

but should be something like应该是这样的

{
"type":"Regular"
}

so is ignoring the Json-Properties from the NewtonSoft.Json-NameSpace.忽略 NewtonSoft.Json-NameSpace 中的 Json-Properties 也是如此。 How can I tell the EventGridEvent to use my Json-Configuration?如何告诉 EventGridEvent 使用我的 Json-Configuration?

I also tried [DataMember(Name ="type")] instead of [JsonProperty] with no success either我也试过[DataMember(Name ="type")]而不是[JsonProperty]也没有成功

The solution was to use another constructor on EventGridEvent using BinaryData and therefore being able to provide JsonSerialize-Options:解决方案是使用 BinaryData 在 EventGridEvent 上使用另一个构造函数,因此能够提供 JsonSerialize-Options:

var serOptions = new JsonSerializerOptions()
{
  PropertyNamingPolicy = null
};
serOptions.Converters.Add(new JsonStringEnumConverter());
var binaryData=new BinaryData(payload, serOptions);
var cloudEvent = new EventGridEvent("lichtblick/serviceView", eventType, dataVersion, binaryData)
{
  Topic = _configuration.Topic
};

(the null forcing PascalCasing instead of camelCasing) (null 强制使用 PascalCasing 而不是 camelCasing)

Alternatively the properties from System.Text.Json.Serialization can be used ( [JsonPropertyName] etc)或者,可以使用System.Text.Json.Serialization中的属性( [JsonPropertyName]等)

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

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