locked
Asp.Net Core Razor Pages Keeps Logging out RRS feed

  • Question

  • User-1237764778 posted

    I've been developing an Asp.Net Core Razor Pages app.I put the first released version on a production server a couple of months ago,and been updating it regularly.Everything was working well until yesterday when I noticed the app keeps logging out after a minute more or less.After searching quite a bit,I realized the problem is that server has limited my app to only use 256 MB of memory,and the reason was,as they said,the app was using too much memory! As far as I know,.Net Core comes with a feature called Garbage Collection which releases memory automatically! It detects unused objects by itself and disposes memory of them.So,What am I doing wrong? What should I do? Any tip would be greatly appreciated as our school work can't be done because of this problem!

    Sunday, June 16, 2019 6:06 AM

All replies

  • User-37275327 posted

    I have no idea of the memory quota, However it would be better to profile your app using a tool like "glimpse", 

    Check this

    Sunday, June 16, 2019 8:04 AM
  • User-474980206 posted
    You could have a memory leak. Check that all objects that implement .Dispose() are called with a using statement. There are many tools for determining memory usage. Also the 2.2 runtime does not have an aggressive gc, nor memory limit support. You might want to update core 3.0 which has added support for docker memory limits.
    Sunday, June 16, 2019 3:47 PM
  • User-1237764778 posted

    I used the Diagnostic Tools provided by Visual Studio to keep track of memory usage,and it appears that the memory gets added without Garbage Collection disposing of any memory! I don't know if it should be the case! I use .Net Framework Core to access data,and the way it works in Asp.Net Core is Dependency Injection providing an instance of Datacontext in Razor Page Class.Any idea what I should do?

    Sunday, June 16, 2019 8:32 PM
  • User-474980206 posted
    Are you disposing the context in the controller dispose?
    Sunday, June 16, 2019 10:40 PM
  • User-37275327 posted

    Scope would be broader to investigate, some similar issues are discussed here,

    Monday, June 17, 2019 4:41 AM
  • User585649674 posted

    When you say "app kepping logging out" do you mean application is crashing and exiting ? Or is the user getting logged out ?

    When you say 256 MB it is hard disk right ? My application hardly uses 1 - 1.2 MB of RAM. There must be some code which is running in a never ending loop or some code which is creating huge number of objects. Like a 1 million record taken from database and applying a sorting or grouping in backend. or performing an another set of logic on huge set of data. In development environment with single user and 100s of data it may not be an issue. But in production with more data and more concurrent users it will cause problem. Try to find the difference in data between development and production database

    Are you hosting in IIS or simply using the inbuilt kestrel in run command window. Because IIS recovers from unhandled exception, but standalone console window will crash on unhandled exception.

    Monday, June 17, 2019 7:26 AM
  • User-821857111 posted

    If you are using Session to manage logins, stop and use authentication cookies instead. At least that way, your users won't get logged out if the app pool recycles because of memory pressure.

    https://www.mikesdotnetting.com/article/335/simple-authentication-in-razor-pages-without-a-database

    That should give you some breathing space to learn about profiling to try to identify the source of your memory leak (if that is in fact the root of the problem).

    Monday, June 17, 2019 1:52 PM
  • User-1237764778 posted

    I mean the user keeps getting logged out! I have uploaded the released version in IIS.The server hosting my app has put a 256 limit quota on my app,which has caused the issue!

    Without the limit,the app was working fine.By the way,I have at most 12K rows to query over,not millions,and to access data,I'm using LINQ and Entity Framework Core.

    Monday, June 17, 2019 9:21 PM
  • User-1237764778 posted

    Actually,I'm using cookies for athentication.Here's the code:

     private void Authenticate(string userName, string role)
            {
                var identity = new ClaimsIdentity(new[] {
                        new Claim(ClaimTypes.Name, userName),
                        new Claim(ClaimTypes.NameIdentifier, userName),
                        new Claim(ClaimTypes.Role, role)
                    }, CookieAuthenticationDefaults.AuthenticationScheme);
                var principal = new ClaimsPrincipal(identity);
                HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal,
                                              new AuthenticationProperties { IsPersistent = LoginData.RememberMe });
            }

    Monday, June 17, 2019 9:26 PM
  • User-1237764778 posted

    No,because as far as I know,It's the Garbage Collection to dispose of the data context or any other objects that aren't needed.Am I right?

    Monday, June 17, 2019 9:29 PM
  • User475983607 posted

    Looks like custom authentication. I’m guessing you have a bug.

    Try using Identity.
    Monday, June 17, 2019 10:03 PM
  • User-37275327 posted

    deh_meisam

    No,because as far as I know,It's the Garbage Collection to dispose of the data context or any other objects that aren't needed.Am I right?

    Garbage collection is handled automatically by .NET framework in most cases, Having said that, you might require of disposing connection objects properly, However you better switch on IIS logs to investigate further.

    https://www.loganalyzer.net/log-analysis/iis-log-files.html

    Tuesday, June 18, 2019 2:15 PM
  • User-474980206 posted

    the GC only tracks managed memory. if the object, like a database context,  uses unmanaged memory, the GC does not know about it. You must call Dispose() to free the unmanaged memory. also the GC favors alloc over compact (at least until core 3.0 with memory constraint support). 

    most likely you are not calling Dispose(). if you inject a dbcontext into a controller, the controller dispose should call dispose on the dbcontext. be sure you are doing this.

    Tuesday, June 18, 2019 3:29 PM
  • User-1237764778 posted

    Thanks for your reply,but as far as I know,Dependency Injection in Asp.Net Core is supposed to dispose the data-context object when the request finishes.It injects the service when the page Model is created and disposes the memory of the object when the request is served with the service. Isn't it right?

    Thursday, June 20, 2019 6:13 PM
  • User-1465089886 posted

    run as admin

    Wednesday, January 13, 2021 7:51 AM