locked
Possible to run C# method after page load? RRS feed

  • Question

  • User-270429056 posted

    Hello, 
    I'm trying to run a C# method after a page is loaded. The method checks the database if a log entry is existing and if not it will write one. The log contains the Id of the page and the email of the current logged in user. This is set to only log once per day so essentially it checks if email, page id, and if the current date is found, if not it will create an entry. 

    I'm currently starting the method at the end of OnGetAsync(), but I'm hoping it a better way to do this so that the loading speed of the page is not affected. Is there a way to do this?

    This is the method that is called. If you see any way to improve this then I would be happy to learn about it. 

    private async Task RegisterUsageAsync(string customerId)
            {
                string email = HttpContext.User.Identity.Name;
                if (email != null && DailyPageViews == null)
                {
                    DailyPageViews dpv;
                    DateTime now = DateTime.Now.Date;
                    dpv = await _context.DailyPageViews
                        .FirstOrDefaultAsync(m => m.ViewerEmail == email && m.ViewedCustomerID == customerId && m.ViewDate == now);
                    if (dpv == null)
                    {
                        //Write to sql.
                        DailyPageViews newdpv = new DailyPageViews
                        {
                            ViewDate = now,
                            ViewerEmail = email,
                            ViewedCustomerID = customerId
                        };
                        //Is this needed? does it work to prevent another db read?
                        DailyPageViews = newdpv;
    
                        _context.DailyPageViews.Add(newdpv);
                        _context.SaveChanges();
                    }
                }
            }

    Thanks.

    Friday, July 24, 2020 8:18 AM

All replies

  • User-474980206 posted

    As it’s a logging method, rather than wait for you could use a thread worker.

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.queueuserworkitem?view=netcore-3.1

    Friday, July 24, 2020 3:31 PM
  • User-270429056 posted

    Thank you, I have managed to put something together that seems to work based on your feedback.
    I know threading could be a bit tricky, do you mind looking over the code and let me know if there are any red flags with this approach? 
    I'm curious if it will be reliable and not crash if two processes try to write to the database at the same time, I also wonder if this will be good performance-wise.

            private void QueueUsageLogger(string id)
            {
                string email = HttpContext.User.Identity.Name;
                if (email != null)
                {
                    DateTime now = DateTime.Now.Date;
                    ThreadPool.QueueUserWorkItem(state => UsageLogger(id,now, email, _context.Options));
                }
            }
            private static void UsageLogger(string id, DateTime now, string email, DbContextOptions<CoreContext> contextOptions)
            {
    CoreContext context = new CoreContext(contextOptions); DailyPageViews dpv; dpv = context.DailyPageViews .FirstOrDefault(m => m.ViewerEmail == email && m.ViewedId == id && m.ViewDate == now); if (dpv == null) { DailyPageViews newdpv = new DailyPageViews { ViewDate = now, ViewerEmail = email, ViewedId = id }; context.DailyPageViews.Add(newdpv); context.SaveChanges(); context.Dispose(); } }

    Thank you!

    Saturday, July 25, 2020 6:44 AM