SaaS CMS has officially launched! Learn more now.

Sequence contains more than one matching element at Mediachase.Commerce.Orders.OrderContext.GetCart(String name, Guid customerId, MarketId marketId)

Vote:
 

Hi Team,

sorry, i mistakenly posted the same issue in cms forum also.  

I have been getting this strange error intermittently in my Episerver cms 7 mvc site with ecommerce application. 

Error description : 


Requested path : http://xxx:80/xx/Menu/xxx/
Exception : Sequence contains more than one matching element|Sequence contains more than one matching element - at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at Mediachase.Commerce.Orders.OrderContext.GetCart(String name, Guid customerId, MarketId marketId)
at Mediachase.Commerce.Website.Helpers.CartHelper.LoadCart(String name, Guid userId, MarketId marketId)
at Mediachase.Commerce.Website.Helpers.CartHelper..ctor(String name, Guid userId, IMarket market)
at EPiServer.Business.Commerce.HttpModules.ProfileModule.Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe)
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

 I could not trace out the root cause of this error. But I have observed the following things related to it.

1) when browse the any page that calls getCart() method, It throws the above error for the first time but if i refresh the page it loads correctly

2) If i disable cache and browse the page the error keep on coming until i enable the cache in browser

3) And it is observed that this error is not consistant for all the users

 I feel it is strange because error could not be catched anywhere in the code. ( its not hitting catch block even while debugging also) 

Please help me to find the root cause of this error.

Thanks in advance ,

Nani

 

#81643
Feb 21, 2014 13:19
Vote:
 

Hi,

The error indicates that there're two or more carts which have same customer, same name and same market. This should be avoided. I suspect that you have some code that creates cart whenever customer browses to the page. 

Please go through your page code to see. You also can post it here or send it to me (quma at episerver.com ) for further analysis.

Regards.

/Q

#81677
Feb 24, 2014 6:52
Vote:
 

Hi Quan Mai,

Thank you so much for the response.

Please find below the additional details related to the issue.

the method that causing this unhandled exception is

-----------------------------------------------------------------------------------------------------------------------

protected string cartName = Cart.DefaultName;

private void GetCart()
        {
            Cart cart = this.orderContext.GetCart(this.cartName, (this.contactID.HasValue) ? contactID.Value : securityContext.CurrentUserId);

            this.cartHelper = new CartHelper(cart);
        }

------------------------------------------------------------------------------------------------------------------------------

Here i have a question, if it is the problem with code error should persist even after refresh of page.

But it is not happening in that way when page loads and this method gets called "Sequence contains more that one matching element" error in GetCart() method was being thrown,

but if i refresh the page in browser it is loading correctly the same code is executing perfectly, this is the strange thing.

and as i mentioned before, one more observation is if i disable cache in browser it is continuosly throwing the same error.

So i suspect there is some thing to do with the browser cache also.

Please help me.

Thanks in advance,

nani

 

#81728
Feb 25, 2014 9:24
Vote:
 

Hi,

I suggest to use this code, which should be simpler:

this.cartHelper = new CartHelper(this.cartName, this.contactID.HasValue ? contactID.Value : securityContext.CurrentUserId);

This also make sure to store the Cart into the cache instead of loading it everytime.

As I mentioned, you should check if there is any other code in your page which unintentionally creates the cart. You can send me the page for further analysis.

Regards.

/Q

 

#81732
Feb 25, 2014 9:32
Vote:
 

The problem can be reproduced like this:

Guid customerId = Guid.NewGuid();

Cart cart1 = OrderContext.Current.GetCart(Cart.DefaultName, customerId, MarketId.Default);
Cart cart2 = OrderContext.Current.GetCart(Cart.DefaultName, customerId, MarketId.Default);

cart1.AcceptChanges();
cart2.AcceptChanges();

Now there are 2 carts in the database for the same user.

If I call "OrderContext.Current.GetCart" again with the same customerId, the error will happen.

OrderContext cannot accept the existence of multiple carts, but it will happily create them.

#87026
Jun 07, 2014 13:30
Vote:
 

Hi,

Excellent finding! Event we don't recommend to write code like this, it's still possible in a race condition in real world scenario. I'll file a bug to see what we can do to improve the situation.

Thank you & regards.

/Q

#87079
Jun 09, 2014 16:02
Vote:
 

Hi,  

Thank you so much Mads Storm Hansen for your reply, It is correct that, issue is with multiple carts but still i have a question. the exception should persist on multiple page refresh , but after refreshing one time i am able to see the page with out any error. Could you help me to find why it happens.

Thanks in advance, 

Nani

#88053
Edited, Jun 30, 2014 16:34
Vote:
 

Hi,

After the first quest, the cart is saved to your database (therefore the second save request will fail with exception).

For the later time, because the cart is existing, then you can call GetCart n times and it'll still be one cart (the one which got saved), thus no more exception.

We decided that any tries to do this:

Cart cart1 = OrderContext.Current.GetCart(Cart.DefaultName, customerId, MarketId.Default);
Cart cart2 = OrderContext.Current.GetCart(Cart.DefaultName, customerId, MarketId.Default);

cart1.AcceptChanges();
cart2.AcceptChanges();

will throw exception on the second AcceptChanges call, indicating that the cart with same name, customer id and market id already existed.

Regards.

/Q

#88150
Jul 03, 2014 8:42
Vote:
 

thank you so much Quan Mai for explanation , I have posted one issue related to security in Episerver commerece manager. please take a look at 

http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=88060&epslanguage=en

#88153
Edited, Jul 03, 2014 9:30
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.