Background Process - Value cannot be null.Parameter name: httpContext
Hi,
I have encountered a bug with inserting and updating data into Umbraco whilst in a background process.
Now I am using Umbraco 7.4.3 assembly: 1.0.5948.18141
Previously on other projects I have been using version 7.1.8 and I don't believe the problem existed there
I am using a background process to do various tasks in Umbraco (inserts and updates). An example of the code failing is....
var contentService = ApplicationContext.Current.Services.ContentService;
var content = contentService.GetById(blogSubscriberId);
content.SetValue("lastSendDate", date);
contentService.SaveAndPublishWithStatus(content);
I have tested and the content service seems to get data ok but it throws errors simply on any SaveAndPublishWithStatus call.
Here are the exception details
Message:
Value cannot be null.Parameter name: httpContext
Stack Trace:
at System.Web.HttpContextWrapper..ctor(HttpContext httpContext)
at Umbraco.Web.SingletonHttpContextAccessor.getValue()
at Umbraco.Web.RequestLifespanMessagesFactory.Get()
at Umbraco.Core.Services.ContentService.SaveAndPublishDo(IContent content, Int32 userId, Boolean raiseEvents)
at Umbraco.Core.Services.ContentService.Umbraco.Core.Services.IContentServiceOperations.SaveAndPublish(IContent content, Int32 userId, Boolean raiseEvents)
at Umbraco.Core.Services.ContentService.SaveAndPublishWithStatus(IContent content, Int32 userId, Boolean raiseEvents)
at CMS.Models.BlogSubscriber.UpdateBlogSubscriberLastSendDate(Int32 blogSubscriberId, DateTime date) in c:\Dropbox\Web\medijobscom\Source Code\Website\AppCode\Models\BlogSubscriber.cs:line 99
at CMS.Models.EmailAlert.InsertBlogAlerts(DateTime lastSentDate) in c:\Dropbox\Web\medijobscom\Source Code\Website\App_Code\Models\EmailAlert.cs:line 267
I got around this issue placing this code before the update or insert code.
HttpContext.Current = new HttpContext(new HttpRequest(null, "https://www.google.com", null),new HttpResponse(null));
Example
HttpContext.Current = new HttpContext(new HttpRequest(null, "https://www.google.com", null),new HttpResponse(null));
var contentService = ApplicationContext.Current.Services.ContentService;
var content = contentService.GetById(blogSubscriberId);
content.SetValue("lastSendDate", date);
contentService.SaveAndPublishWithStatus(content);
I simply faked the httpContext. I guess this isn't the best practice so if your guys know of any Umbraco preferred way to get around this then please let me know.
When you start a background process, it may continue after the HttpRequest that triggered it is done. The HttpContext should then be disposed of and used by a new user. So you shouldn't rely on it in background processes.
However, publishing content requires a HttpContext, so what you did with the fake context is viable.
There is another issue you should be aware of though. When you save and publish content, a new version is created of it. It will grow your database endlessly. It is somewhat of an anti-pattern to do that in Umbraco. I highly recommend you find some other way to store the last sent date. You'd get rid of two problems in one go. ;)
It looks to me like you actually store subscribers to your blog as content? Wouldn't members be a better alternative? You should be able to save properties on those without versioning or HttpContexts too.
That's a massive concern! Is there any way to clear or delete this old (unwanted data)?
This thread lead me to check on a number of my projects to see what resources they are chewing up and its pretty bad.
Here is the problem I face....
I build a lot of standalone projects using umbraco CMS (Maybe 4 a month). Most the projects are very similar so I obviously re-use code. Every time I start a new project and copy the old source code and database, delete what I don't need and work with what I have.
I have now noticed that because of this model each of my Umbraco databases are exceptional big and increasing through each new project since I am re-using the database from the last project.
To give you an idea what sizes I am talking about here are some of the worst.
Obviously I cant continue like this at all. Please can you help me out and let me know the ways I can start clearing this database and at the same time still keep re-using the code I have written in new projects.
I strongly encourage you to move subscribers to members or a custom table, though. There's a saying that "you shouldn't cling to a mistake just because you spent a lot of time making it". ;) Versioning of content is a really nice feature for your content, and publishing is to make information public on the site. Not really sure whether you've got public profiles for all your subscribers?
No sorry but I can confirm that way works fine with no problems. In have had this running on multiple applications for a long time now. I haven't experienced any issues at all doing it this way.
Background Process - Value cannot be null.Parameter name: httpContext
Hi,
I have encountered a bug with inserting and updating data into Umbraco whilst in a background process.
Now I am using Umbraco 7.4.3 assembly: 1.0.5948.18141
Previously on other projects I have been using version 7.1.8 and I don't believe the problem existed there
I am using a background process to do various tasks in Umbraco (inserts and updates). An example of the code failing is....
var contentService = ApplicationContext.Current.Services.ContentService; var content = contentService.GetById(blogSubscriberId); content.SetValue("lastSendDate", date); contentService.SaveAndPublishWithStatus(content);
I have tested and the content service seems to get data ok but it throws errors simply on any SaveAndPublishWithStatus call.
Here are the exception details
Message: Value cannot be null.Parameter name: httpContext
Stack Trace: at System.Web.HttpContextWrapper..ctor(HttpContext httpContext) at Umbraco.Web.SingletonHttpContextAccessor.getValue() at Umbraco.Web.RequestLifespanMessagesFactory.Get() at Umbraco.Core.Services.ContentService.SaveAndPublishDo(IContent content, Int32 userId, Boolean raiseEvents) at Umbraco.Core.Services.ContentService.Umbraco.Core.Services.IContentServiceOperations.SaveAndPublish(IContent content, Int32 userId, Boolean raiseEvents) at Umbraco.Core.Services.ContentService.SaveAndPublishWithStatus(IContent content, Int32 userId, Boolean raiseEvents) at CMS.Models.BlogSubscriber.UpdateBlogSubscriberLastSendDate(Int32 blogSubscriberId, DateTime date) in c:\Dropbox\Web\medijobscom\Source Code\Website\AppCode\Models\BlogSubscriber.cs:line 99 at CMS.Models.EmailAlert.InsertBlogAlerts(DateTime lastSentDate) in c:\Dropbox\Web\medijobscom\Source Code\Website\App_Code\Models\EmailAlert.cs:line 267
Any help would be much appreciated!!!
Kind Regards
David
Hi,
I got around this issue placing this code before the update or insert code.
Example
I simply faked the httpContext. I guess this isn't the best practice so if your guys know of any Umbraco preferred way to get around this then please let me know.
Thanks
David
Hello ... any news on this ?
thank you
Angelo
Also a problem for me.
Hi,
I haven't figured out anything new but I have been using my solution above for a while now on a number of projects and not found any issues so far.
There must be a more accurate solution through. It would be great to hear from some Umbraco staff which way to go on this?
Kind Regards
David
When you start a background process, it may continue after the HttpRequest that triggered it is done. The HttpContext should then be disposed of and used by a new user. So you shouldn't rely on it in background processes.
However, publishing content requires a HttpContext, so what you did with the fake context is viable.
There is another issue you should be aware of though. When you save and publish content, a new version is created of it. It will grow your database endlessly. It is somewhat of an anti-pattern to do that in Umbraco. I highly recommend you find some other way to store the last sent date. You'd get rid of two problems in one go. ;)
It looks to me like you actually store subscribers to your blog as content? Wouldn't members be a better alternative? You should be able to save properties on those without versioning or HttpContexts too.
Hi,
That's a massive concern! Is there any way to clear or delete this old (unwanted data)?
This thread lead me to check on a number of my projects to see what resources they are chewing up and its pretty bad.
Here is the problem I face....
I build a lot of standalone projects using umbraco CMS (Maybe 4 a month). Most the projects are very similar so I obviously re-use code. Every time I start a new project and copy the old source code and database, delete what I don't need and work with what I have.
I have now noticed that because of this model each of my Umbraco databases are exceptional big and increasing through each new project since I am re-using the database from the last project.
To give you an idea what sizes I am talking about here are some of the worst.
12.6 GB, 7.8 GB, 7.1 GB, 4 GB, 3.7 GB, 3.3 GB, 2.7 GB
Obviously I cant continue like this at all. Please can you help me out and let me know the ways I can start clearing this database and at the same time still keep re-using the code I have written in new projects.
Thanks in Advance
David
Hi,
After further investigation I found a suitable package which worked with my Umbraco 7.4 installation.
https://our.umbraco.org/projects/backoffice-extensions/falm-housekeeping
This adds a tree node in the developer section that allows users to check on old version data and log data. It then allows you to clear this data.
Nice Package!
Hope this helps anyone else experiencing the same issue.
Kind Regards
David
FALM will help limit the symptom, yes. There's also https://our.umbraco.org/projects/website-utilities/unversion/.
I strongly encourage you to move subscribers to members or a custom table, though. There's a saying that "you shouldn't cling to a mistake just because you spent a lot of time making it". ;) Versioning of content is a really nice feature for your content, and publishing is to make information public on the site. Not really sure whether you've got public profiles for all your subscribers?
Yes I have public profiles for my subscribers.
Thanks for your assistance. I now better understand the publishing setup so will code accordingly.
Kind Regards
David
Great! :)
I recommend you watch Marc Goodson's session from CodeGarden 16. It shows a couple of similar scenarios and how to go about them.
https://video.twentythree.net/umbraco-anti-patterns
Glad I could help.
HI,
This has been a problem for me also. Has there been any updates with this?
Thanks
Hi David,
Did you manage to come up with a better way than faking the httpContext?
Many thanks Paul
Hi,
No sorry but I can confirm that way works fine with no problems. In have had this running on multiple applications for a long time now. I haven't experienced any issues at all doing it this way.
I think it's pretty safe to say its ok to use.
Kind Regards
David
Hi,
Thanks for the quick response.
So is the falm-housekeeping or unversion package advised when using it or was that just because of the way you were using it with your blog?
Thanks Paul
is working on a reply...