How to send list in a command

nservicebus
sagas
nhibernate

(Bheesham Kumar) #1

I am creating a application using NserviceBus Saga.
Application reading a values from a file. We collected all the value in list. Now we want to send the list with the help of command to the handler.

await context.Send(new PriceListCommand()
{
PriceCommandList = lstSend,
TotalCount = lstPrm.Count(),
}).ConfigureAwait(false);

In Saga how retrieve the list elements. in IAmStartedByMessages


(Andreas Öhlund) #2

How many items would you expect to be in that list and how “big” are they?

If the number is reasonably low you could just add them to the PriceListCommand as a public IList<ListItem> Items;. The risk here would be that the message size will exceed the size limit of the queuing system you are using.

https://docs.particular.net/transports/

Another option would be to have your import process write the values to some storage (file, database, AzureBlobStorage, Amazon S3 etc) and just pass the “link” to those values in the message.

Nitpick: PriceListCommand might deserve a better name, perhaps something like RegisterPriceList or something along those lines?

Cheers,

Andreas


(Bheesham Kumar) #3

Thanks @andreasohlund for quick reply.

I created the list like public IList RegisterPriceListList { get; set; } and this list contains 34 items
I created a saga. When i am trying to access the saga

IAmStartedByMessages,

RegisterPriceListList it not showing the list items.

What is right step, i have to follow?


(Andreas Öhlund) #4

So the list with the items is empty when the handler on the saga is invoked?

You need to use IList<T> and not just an IList for this to work. (At least
for most serializers)


(Bheesham Kumar) #5

Yes i am using as u suggested IList<RegisterPriceListList> { get; set; }
But the problem is coming like
I am sending the list item as

await context.Send(new PriceListCommand()
{
PriceCommandList = lstSend,
TotalCount = lstPrm.Count(),
}).ConfigureAwait(false);

I created a command class

 public  class CalculatePriceListCommand : IMessage
    {
        public IList<RegisterPriceListList> CalculatePriceCommandList { get; set; }
    }

Then i created the handlar

 public class CalculatePriceSagaHandler : Saga<CalculatePriceGroupSagaData>, IAmStartedByMessages<CalculatePriceListCommand>,
                                                  IHandleMessages<SendCalculatePriceDataCommand>
    {
        protected override void ConfigureHowToFindSaga(SagaPropertyMapper<CalculatePriceGroupSagaData> mapper)
        {
            // mapper.ConfigureMapping<RegisterPriceListList>(message => message.CalculatePriceCommandList).ToSaga(sagaData => sagaData.GroupId);
            // mapper.ConfigureMapping<SendCalculatePriceDataCommand>(message => message.NoOfElementsInGroup).ToSaga(sagaData => sagaData.GroupId);
        }

        public async Task Handle(CalculatePriceListCommand message, IMessageHandlerContext context)
        {
            CalculatePriceSagaData calculatepriceSagaData = null;
            if (this.Data.CalculatePriceSagaDatas == null)
            {
                this.Data.CalculatePriceSagaDatas = new List<CalculatePriceSagaData>();
            }

            **calculatepriceSagaData = this.Data.CalculatePriceSagaDatas.Where(x => x.GroupCountForPriceMaster == message.PriceMasterGroupId).FirstOrDefault();**
            if (calculatepriceSagaData != null)
            {
                this.Data.CalculatePriceSagaDatas.Remove(calculatepriceSagaData);
            }

            calculatepriceSagaData = new CalculatePriceSagaData
            {
                //AveragePrice = message.AveragePrice
            };

        }

But i am not able to access the list items. what is the right process i have to follow.


(Andreas Öhlund) #6

What happens here is that when you start your saga using the CalculatePriceListCommand a new instance is created in the saga datastore that you’re using, all good so far. When the SendCalculatePriceDataCommand comes in you need to tell NServiceBus how to find the instance that was previously created. That is done using the ConfigureHowToFindSaga method. I can see that you have commented out the code in there. You need to put that code back in and find some “id” that you can use to correlate on.

Perhaps you can add some kind of PriceListId to your messages and use that to correlate?

You can read more on saga correlation here:

https://docs.particular.net/nservicebus/sagas/message-correlation

Hope this helps!

Cheers,

Andreas


(Bheesham Kumar) #7

Thanks its working for me