Azure Event Grid publish EventGridTopicEvent

chagantivamsi87

New member
Joined
Dec 24, 2021
Messages
3
Programming Experience
10+
HI All,

I have scenario where I need to publish "EventGridTopicEvent" data and at the same time I need to store this event information in the Azure Storage Table. I would like to know is there any way to get the information(like T/F) if Publish event successfully done or not(any error), If it is published successfully then only I should save the data into Table storage.

I need to handle the above logic via C# Event Handler(Mediator), I tried with C# TransactionScope but it is not having mechanism like Rollback. If event is not published then rollback the event as well as not store anything in the table storage or I should not make a call to handle the database transaction.

I have also tried _mediator.Publish(request).IsCompletedSuccessfully but it is giving true always even though I pass invalid request data.

code snippet
C#:
public async Task<Result<Response<XYZ>>> Handle(EventGridTopicEvent request, CancellationToken cancellationToken)
        {
            using TransactionScope transactionScope = new TransactionScope();
            {
                try
                {
                   bool eventPublishStatus =  _mediator.Publish(request).IsCompletedSuccessfully;        
                    if (eventPublishStatus)
                    {
                        //Insert request data to Table storage
                    }}}}
 
Last edited by a moderator:
I tried with C# TransactionScope but it is not having mechanism like Rollback
There is no need for a Rollback() method. If you don't call Complete() on the transaction scope, when the Dispose() on the scope gets called, then the operations within the transactions scope should be rolled back.
If no exception occurs within the transaction scope (that is, between the initialization of the TransactionScope object and the calling of its Dispose method), then the transaction in which the scope participates is allowed to proceed. If an exception does occur within the transaction scope, the transaction in which it participates will be rolled back.

When your application completes all work it wants to perform in a transaction, you should call the Complete method only once to inform that transaction manager that it is acceptable to commit the transaction. Failing to call this method aborts the transaction.
 
I don't use the Azure Event Grid, but my understanding of it is that it just another message queue service built on the pub-sub pattern. Your event handlers are merely subscribers. There is no "rollingback the event". This is because you may not be the only subscriber to the event. You can't tell the publisher "regarding that event you just sent me, don't send that event to other people." Pub-sub pattern is not like chain of responsibility pattern where you can interrupt passing information down the chain.

I'm not quite sure what kind of object is hiding behind your _mediator, but my understanding of various pub-sub pattern implementations is that the publisher considers data that is passed on to it as opaque -- all it knows it that it got a blob of data that should be published to subscribers. If it got the blob data completely, then it's successful regardless of whether it can deliver the data or not; and whether there are any subscribers who can understand the data or not. So on the surface, I'm not surprised that you are getting success all on your _mediator.Publish() call.
 

Latest posts

Back
Top Bottom