Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnsureCustomerCreationAndConvertBasket throws NullReferenceException #2266

Closed
ronaldbarendse opened this issue Jun 26, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@ronaldbarendse
Copy link

commented Jun 26, 2019

As discussed in https://our.umbraco.com/packages/collaboration/merchello/merchello/85584-ysod-after-deleting-logged-in-customer and also reported earlier https://our.umbraco.com/packages/collaboration/merchello/merchello/74547-ensurecustomercreationandconvertbasket-error-on-preview, EnsureCustomerCreationAndConvertBasket() throws a NullReferenceException when the member can't be found:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Merchello.Web.CustomerContext.<>c__DisplayClass1.<EnsureCustomerCreationAndConvertBasket>b__0(String x)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Merchello.Web.CustomerContext.EnsureCustomerCreationAndConvertBasket(ICustomerBase customer)
   at Merchello.Web.Pluggable.CustomerContextBase.TryGetCustomer(Guid key)
   at Merchello.Web.Pluggable.CustomerContextBase.Initialize()
   at Merchello.Web.Pluggable.CustomerContextBase..ctor(IMerchelloContext merchelloContext, UmbracoContext umbracoContext)
   at Merchello.Web.CustomerContext..ctor(UmbracoContext umbracoContext)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Merchello.Core.ActivatorHelper.CreateInstance[T](Type type, Object[] constructorArgumentValues)

The affected method is:

protected override void EnsureCustomerCreationAndConvertBasket(ICustomerBase customer)
{
if (!customer.IsAnonymous) return;
var memberId = Convert.ToInt32(this.MembershipProviderKey(customer.Key));
var member = _memberService.GetById(memberId);
if (MerchelloConfiguration.Current.CustomerMemberTypes.Any(x => x == member.ContentTypeAlias))
{
base.EnsureCustomerCreationAndConvertBasket(customer);
}
}

Looking at this code, it does not handle the following cases correctly:

  • customer is null - although this probably won't happen, as this method is only called from TryGetCustomer(), which already does a null check;
  • MembershipProviderKey() might return a string that can't be converted to an int - as GetMembershipProviderKey() can return an empty string, Convert.ToInt32() should be changed to int.TryParse();
  • member is null - as the memberId might be wrong (or just deleted), so just add a null check before accessing member.ContentTypeAlias;

So the method would become:

protected override void EnsureCustomerCreationAndConvertBasket(ICustomerBase customer)
{
	if (!customer.IsAnonymous)
	{
		return;
	}

	var membershipProviderKey = this.MembershipProviderKey(customer.Key);
	if (!int.TryParse(membershipProviderKey, NumberStyles.None, CultureInfo.InvariantCulture, out var memberId))
	{
		return;
	}

	var member = _memberService.GetById(memberId);
	if (member != null && MerchelloConfiguration.Current.CustomerMemberTypes.Any(x => x == member.ContentTypeAlias))
	{
		base.EnsureCustomerCreationAndConvertBasket(customer);
	}
}
@YodasMyDad

This comment has been minimized.

Copy link
Contributor

commented Jul 12, 2019

Thanks

@YodasMyDad YodasMyDad closed this Jul 12, 2019

@ronaldbarendse

This comment has been minimized.

Copy link
Author

commented Jul 12, 2019

Just for reference: this is fixed in commit 6591429 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.