Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Armitage 236 posts 881 karma points
    Apr 27, 2016 @ 10:05
    David Armitage
    0

    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.

    1. Now I am using Umbraco 7.4.3 assembly: 1.0.5948.18141

    2. Previously on other projects I have been using version 7.1.8 and I don't believe the problem existed there

    3. 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);

    4. I have tested and the content service seems to get data ok but it throws errors simply on any SaveAndPublishWithStatus call.

    5. 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

  • David Armitage 236 posts 881 karma points
    Apr 27, 2016 @ 10:31
    David Armitage
    0

    Hi,

    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.

    Thanks

    David

  • Angelo 81 posts 182 karma points
    Jun 03, 2016 @ 16:22
    Angelo
    0

    Hello ... any news on this ?

    thank you

    Angelo

  • Michael 2 posts 71 karma points
    Jun 28, 2016 @ 23:39
    Michael
    0

    Also a problem for me.

  • David Armitage 236 posts 881 karma points
    Jun 29, 2016 @ 07:32
    David Armitage
    0

    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

  • Lars-Erik Aabech 337 posts 1077 karma points MVP 3x c-trib
    Jul 02, 2016 @ 21:19
    Lars-Erik Aabech
    1

    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.

  • David Armitage 236 posts 881 karma points
    Jul 07, 2016 @ 03:34
    David Armitage
    0

    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

  • David Armitage 236 posts 881 karma points
    Jul 07, 2016 @ 05:18
    David Armitage
    0

    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

  • Lars-Erik Aabech 337 posts 1077 karma points MVP 3x c-trib
    Jul 07, 2016 @ 06:22
    Lars-Erik Aabech
    0

    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?

  • David Armitage 236 posts 881 karma points
    Jul 07, 2016 @ 06:51
    David Armitage
    0

    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

  • Lars-Erik Aabech 337 posts 1077 karma points MVP 3x c-trib
    Jul 07, 2016 @ 06:54
    Lars-Erik Aabech
    0

    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.

  • James Wilkinson 1 post 20 karma points
    Jul 17, 2016 @ 14:21
    James Wilkinson
    0

    HI,

    This has been a problem for me also. Has there been any updates with this?

    Thanks

  • Paul Griffiths 334 posts 933 karma points
    Nov 16, 2016 @ 15:22
    Paul Griffiths
    0

    Hi David,

    Did you manage to come up with a better way than faking the httpContext?

    Many thanks Paul

  • David Armitage 236 posts 881 karma points
    Nov 16, 2016 @ 15:26
    David Armitage
    1

    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

  • Paul Griffiths 334 posts 933 karma points
    Nov 16, 2016 @ 15:40
    Paul Griffiths
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft