sdk-csharp: ContentType decode exception

Hello,

I receive this expcetion while I try to deserialize event which I receive from Azure Service Bus. Excaption:

The 'contenttype' attribute value must be a content-type expression compliant with RFC2046
   at CloudNative.CloudEvents.CloudEventAttributes.ValidateAndNormalize(String key, Object& value)
   at CloudNative.CloudEvents.CloudEventAttributes.set_Item(String key, Object value)
   at CloudNative.CloudEvents.JsonEventFormatter.DecodeJObject(JObject jObject, IEnumerable`1 extensions)
   at CloudNative.CloudEvents.JsonEventFormatter.DecodeStructuredEvent(Byte[] data, IEnumerable`1 extensions)

The event looks like this:

{
  "DataContentType": {
    "Boundary": null,
    "CharSet": null,
    "MediaType": "application/json",
    "Name": null,
    "Parameters": []
  },
  "Data": {
    "OrderNumber": "#",
    "TrackingNumber": "#",
    "StateId": 5
  },
  "Id": "dbb05af1-d0fb-46ee-9c8e-df585c02362a",
  "DataSchema": null,
  "Source": "http://localhost:61974/",
  "SpecVersion": 3,
  "Subject": "510589659",
  "Time": "2020-11-20T05:26:56.8365438Z",
  "Type": "trackandtrace.order.statechanged"
}

Event creation looks like this:

var message = new CloudEvent(Types.OrderStateChanged, _configuration.BaseUri)
{
	Data = new TransportStateChanged
	{
		OrderNumber = orderInfo.OrderNumber,
		TrackingNumber = orderInfo.TrackingNumber,
		StateId = orderInfo.StateId
	},
	DataContentType = new ContentType("application/json"),
	Subject = orderInfo.OrderNumber
};

Event deserialization is done by JsonEventFormatter with byte[] parameter.

Is there anything that I miss when I create or deserialize message?

I check JsonEventFormatter code and maybe this is the problem

switch (item.Value.Type)
{
	case JTokenType.String:
		attributes[item.Key] = item.Value.ToObject<string>();
		break;
	case JTokenType.Date:
		attributes[item.Key] = item.Value.ToObject<DateTime>();
		break;
	case JTokenType.Uri:
		attributes[item.Key] = item.Value.ToObject<Uri>();
		break;
	case JTokenType.Null:
		attributes[item.Key] = null;
		break;
	case JTokenType.Integer:
		attributes[item.Key] = item.Value.ToObject<int>();
		break;
	default:
		attributes[item.Key] = item.Value;
		break;
}

bacause in CloudEventAttributes is value type compared to string or ContentType but actual type is Newtonsoft.Json.Linq.JObject

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

Seems this is a change in .NET5. This have broken a few APIs here after upgrading.

Also reproduced here: dotnet/aspnetcore#29343