Review the CartProvider.Delete method

Found in

EPiServer.Commerce 12.10.0

Fixed in

EPiServer.Commerce 13.3.1

Created

May 13, 2019

Updated

Jun 07, 2019

State

Closed, Fixed and tested


Description

Partner gets an exception which possibly seems to happen when CartProvider.Delete tries to delete a cart that is already deleted.

System.NullReferenceException: Object reference not set to an instance of an object.
at EPiServer.Commerce.Order.Internal.CartProvider.Delete(OrderReference orderLink)
at EPiServer.Commerce.Order.DefaultOrderRepository.Delete(OrderReference orderLink)
at EPiServer.Commerce.Order.Internal.CartMigrator.MigrateCarts(Guid sourceCustomerId, Guid destinationCustomerId)
at EPiServer.Commerce.Order.ProfileMigrator.MigrateCarts(Guid anonymousId)
at EPiServer.Business.Commerce.HttpModules.ProfileModule.Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe)
at System.Web.Profile.ProfileModule.OnEnter(Object source, EventArgs eventArgs)
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

They think that it might happen when two requests initiating cart migration at the same time. They think we should implement a null check on 

public void Delete(OrderReference orderLink)
{
Cart cart = CartProvider.EnsureCartType(this.Load(orderLink.OrderGroupId));
cart.Delete();
cart.AcceptChanges();
}