Copied to clipboard

Flag this post as spam?

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


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Anders Daniel Thomsen 2 posts 22 karma points
    Jun 26, 2012 @ 12:31
    Anders Daniel Thomsen
    0

    Doing database work in a background thread

    Hello,

    I have made a background thread running once a day that is responsible for doing some work on PurchaseOrders in the database - ie. it is changing order statuses and adding audit trails.

    The method I have to do the job throws an exception when running in the background. The error says that HttpContext.Current is null (of course).

    HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net

    I'm using

    var OrderList = PurchaseOrder.Find(po => po.OrderNumber != null && po.OrderStatus == OrderStatus.Get(2)); 

    to query the entries in the table.

    Why is this depending on the HttpContext, and can I get around it somehow?

  • Søren Spelling Lund 1797 posts 2786 karma points
    Jul 05, 2012 @ 11:01
    Søren Spelling Lund
    0

    By default the components configured in uCommerce assume a web context, but you can change that to match your environment by editing the components.config file placed with your console app.

    You want to change the lifestyle of a couple of data access components to "Thread" instead of "PerWebRequest".

    The services in question are:

    • ISessionProvider
    • IRepository
    • ISoftDeletableReposity
    • IUserService
  • Anders Daniel Thomsen 2 posts 22 karma points
    Jul 31, 2012 @ 13:39
    Anders Daniel Thomsen
    0

    Thanks Søren (and sorry for my late reply).

    Is there a way to make the change dynamically at runtime? The thing is, I need to do some maintenance work on the database and I would like to have that done in a System.Timers.Timer and not have it done with a page request - since I don't want users to experience slow load times because I have some maintenance to do.

  • Søren Spelling Lund 1797 posts 2786 karma points
    Aug 07, 2012 @ 10:38
    Søren Spelling Lund
    0

    There's not.

    But you could instantiate the session provider yourself and use that:

    using System.Linq;
    using NHibernate;
    using NHibernate.Linq;

    var sessionProvider = new SessionProvider();
    using (var session = sessionProvider.GetSession())
    {
    //Select() is like All()
    session.Query<Product>().Select();
    }

    From here you can do all the queries you're used to just with a different starting point.

Please Sign in or register to post replies

Write your reply to:

Draft