ArgumentNullException, httpContext cannot be null, with EmailProfile.Save() and EmailType.Save()

Dear Umbraco Folk,

In upgrading from Umbraco version 7.5.10 with uCommerce 7.5.0.17040 to Umbraco 8 with UCommerce 9 I am experiencing 2 similar problems, which I expect will have a very similar solution.

In both cases the code works fine with Umbraco 7 but fails with Umbraco 8.

(1) In the first case this EmailProfile.Save(); causes a System.ArgumentNullException exception saying
Message=Value cannot be null. Parameter name: httpContext.

(2) In the second case this EmailType.Save(); causes a System.ArgumentNullException exception saying
Message=Value cannot be null. Parameter name: httpContext.

I have similar problems saving other uCommerce entities.

I am a little confused because httpContext is not a parameter of either thisEmailProfile or thisEmailType, so how can it be a null “parameter”?

Note however that the code is running in a class that is called from a WebAPI call which calls the class asynchronously on a different thread using this call:

var t = Task.Run(() => tT_UCommerceAdmin.TTBuildStore(request_TTBuildStore)); 

So it is reasonable that the httpContext related to the WebAPI call might me gone by the time this code runs.
Here is more specifics on each of the problems:

PROBLEM 1:

Code: (note Line causing exception indicated with “// <<<<< Exception happens here”)
EmailProfile thisEmailProfile = EmailProfile.FirstOrDefault(x => x.Name == thisEmailProfileName && x.Deleted == false);
if (thisEmailProfile == null)
{
thisEmailProfile = new EmailProfile
{
Name = thisEmailProfileName
};

                thisEmailProfile.Save();   // <<<<< Exception happens here

                string message = "CheckCreate_TT_Cats: Created new Email Profile: " + thisEmailProfileName;
                TT_UCAdminStatic.RequestStatusStatic.EventLogIn += message + " <br/> ";
                iTTLog4Net_Log.Info(message);
            }

Exception:

System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null.
Parameter name: httpContext
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>     

Value of The Properties and Attributes of thisEmailProfile:

PROBLEM 2:

Code: (note: the line causing exception indicated with “// <<<<< Exception happens here”)

       EmailType thisEmailType = EmailType.FirstOrDefault(x => x.Name == emailProfileArray[1] && x.Deleted == false);
            if (thisEmailType == null)
            {
                thisEmailType = new EmailType();
                thisEmailType.Name = emailProfileArray[1];
                thisEmailType.Description = emailProfileArray[2];
                thisEmailType.Save();   //  <<<<< Exception happens here
            }

Exception:

System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null.
Parameter name: httpContext
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

Value of The Properties and Attributes of thisEmailType:

Any assistance in understanding or resolving this issue would be greatly appreciated.

Terry Clancy

ClanceZ


This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/103990-argumentnullexception-httpcontext-cannot-be-null-with-emailprofilesave-and-emailtypesave