Coding in Asp.Net 6 Best Practices and Performance Tips and Tricks

  1. If you are using MemoryCache in your C-Sharp Application, use the AddOrCreate API to minimize code. This API checks to see if the Cache has the Key specified if not it adds it.

  2. Make sure every Function has Error Handling. For Example, check if the input is valid or not handle the appropriate Error.
  3. Make sure you return from the function as soon as possible. You can do this by first trying to retrieve or check whether the record exists if not return without executing further Code Instructions.

    - This speeds up the Application as the User will be notified with further instructions on how to proceed.

  4. Demian explains how using Form would potentially affect the application performance. This is because the HttpContext does not know upfront that there is a Form attached to the incoming request until you try to access the IHttpFeature to get the values of the form. The HttpRequest Items are lazily loaded into the Context hence they are expensive to access.

    David Fowler illustrated how to Get a Form value from Asp.Net Core HttpContext

      He said  do this:
        var form = await httpContext.Request.ReadFormAsync()
      He said do not do this:
       var form = httpContext.Request.Form;//This is bad

  5. The Dotnet Team is planning to phase out the Out-Of-Process hosting in IIS, so if you host your app in IIS start looking into hosting with In-Process.
  6. Be careful using Sessions in Asp.Net Core, it might bring a bottleneck into the application.
  7. JIT in Asp.Net Core becomes self-optimized after the Application runs for a while. The Just In Time Compilation underlying Software becomes Operating System aware and then optimizes the Hot Path of the code execution based on the running System or Hardware (Linux or Windows).


    The question becomes though, does this optimization end when you deploy a new build? If the JIT saves the Optimization metadata to disk and then loads them back into Memory on the next application deployment that would be great. If you know to answer this question, please comment below.

    I guess this is the question of AOT and PGO, if you could do some research on that you will be able to answer the question above.
  8. Personal Lessons Learned Developing Asp.Net Core Applications

    Do not hard code the environment variables in the code, those credentials change from time to time and you do not want to refactor the code every time they change. 

   Instead, put all your secret credentials like User Names and Password in the secret file or Configuration file for none secret credentials.


[Reference]

1. Read Article Here, it is very helpful to know the tips and tricks.

2. Watch this video of David Fowler and Damian Edwards explaining the Asp.Net Core Architecture. This is important because you would want to know how things work under the hood.


Write an Article
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.


© 2022 - ErnesTech - Privacy