cloudevents / sdk-csharp

CSharp SDK for CloudEvents
Apache License 2.0
271 stars 76 forks source link

AMQP Rabbit #235

Open avinashs433 opened 1 year ago

avinashs433 commented 1 year ago

Hi,

Can you please provide an example/documentation how to dispatch and consume a message to and from rabbitMQ using ToAmqpMessage() ?

 var formatter = new JsonEventFormatter();

 var message = cloudEvent.ToAmqpMessage(ContentMode.Binary, formatter);

 var encoded = message.Encode();

//This message is then published to rabbit

Consumer

// Reading the message
Task<bool> MessageProcessor(BasicDeliverEventArgs eventArgs)
{
            var formatter = new JsonEventFormatter();
            var byteBuffer = new ByteBuffer(eventArgs.Body.ToArray(), 0, eventArgs.Body.Length, eventArgs.Body.Length);
            var message = Message.Decode(byteBuffer);
            var cloudEvent = message.ToCloudEvent(formatter);

// also tried
           var byteBuffer = JsonSerializer.Deserialize<ByteBuffer>(eventArgs.Body.ToArray());
            var message1 = Message.Decode(byteBuffer);
}
jskeet commented 1 year ago

I've never used Rabbit - it would really help if you'd say what happens with the code that you've already tried. Does it throw an exception? What happens?

avinashs433 commented 1 year ago

It throws an exception "Structured mode content does not represent a CloudEvent" on the consumer.

Not sure if I an sending it correctly across to rabbit. Was referring to this test

                CloudEvent cloudEvent = new CloudEvent(CloudEventsSpecVersion.Default)
                {
                    Id = Guid.NewGuid().ToString(),
                    Type = "test.game.updated",
                    Time = DateTime.UtcNow,
                    Source = new Uri($"/Games/{notification.GameId}"),
                    DataContentType = "application/cloudevents+json",
                    Data = notification,
                };
jskeet commented 1 year ago

Not sure if I an sending it correctly across to rabbit.

As I said, I've never used Rabbit, so I don't know on that front.

Are you able to decode the result of calling Encode, i.e. getting everything to work from a CloudEvent to an encoded message and back again without actually sending it to Rabbit?

Fundamentally you're encoding this using binary content mode, so I wouldn't expect it to try to deserialize using structured content mode, unless you're setting a content type somewhere that makes it look like it's structured.

avinashs433 commented 1 year ago

Yeah I am able to encode and decode on the publisher side.

Tried using both binary and structured modes.

Will give a another try tomorrow.

On Wed, 17 Aug 2022, 18:39 Jon Skeet, @.***> wrote:

Not sure if I an sending it correctly across to rabbit.

As I said, I've never used Rabbit, so I don't know on that front.

Are you able to decode the result of calling Encode, i.e. getting everything to work from a CloudEvent to an encoded message and back again without actually sending it to Rabbit?

Fundamentally you're encoding this using binary content mode, so I wouldn't expect it to try to deserialize using structured content mode, unless you're setting a content type somewhere that makes it look like it's structured.

— Reply to this email directly, view it on GitHub https://github.com/cloudevents/sdk-csharp/issues/235#issuecomment-1218255849, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABBEX5SQJKOS47IDEFE523DVZUITFANCNFSM56Z76SAQ . You are receiving this because you authored the thread.Message ID: @.***>

jskeet commented 1 year ago

Yeah I am able to encode and decode on the publisher side.

In that case it's presumably something to do with how you're sending and receiving the message - which I won't be able to help with, I'm afraid (and which we can't even see completely in terms of the code you've provided).

I notice that you're only using the body of BasicDeliverEventArgs - I don't know whether that's the problem, but it's worth looking into. Fundamentally if the receiving side doesn't get back the same data that the publisher side sent, that's going to be a problem - you might want to ask in a Rabbit-specific repo/forum.

avinashs433 commented 1 year ago

Ok cool.

Usually with rabbit if I send a serialized model to rabbit. The consumer is able to deserialize the body of the basicDeliverEventArgs into the published model type.

Its only now that I'm trying to use the cloud event model that is converted to an AMQP encoded message that I'm having trouble with converting back into a cloud event message on the consumer side.

Il try play around alittle more and il post find findings.

On Wed, 17 Aug 2022, 18:50 Jon Skeet, @.***> wrote:

Yeah I am able to encode and decode on the publisher side.

In that case it's presumably something to do with how you're sending and receiving the message - which I won't be able to help with, I'm afraid (and which we can't even see completely in terms of the code you've provided).

I notice that you're only using the body of BasicDeliverEventArgs - I don't know whether that's the problem, but it's worth looking into. Fundamentally if the receiving side doesn't get back the same data that the publisher side sent, that's going to be a problem - you might want to ask in a Rabbit-specific repo/forum.

— Reply to this email directly, view it on GitHub https://github.com/cloudevents/sdk-csharp/issues/235#issuecomment-1218267169, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABBEX5WNESPWUOXJO4DF3YTVZUJ5LANCNFSM56Z76SAQ . You are receiving this because you authored the thread.Message ID: @.***>