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

  1. If you are using Internal .Net MemoryCache API 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 to handle the appropriate Error. In programming, you just don't trust what the User is going to input into the function.

  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.
    - Returning as soon as possible from the function also saves Computer Memory and CPU Threads. If the input or any one of the criteria not met that allows the function to execute, return to the caller when this
       constraint is known. The best way to visualize this is when using a Recursion Algorithm.

    public string MyFunction(int n)
        if (n < = 1) // base case
            return 1;//return as soon as possible.
            return n*fact(n-1);    

  4. Demian over there at Microsoft explains how using a Form API in Asp.Net Core 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 the best way to access the form content is by using ReadFormAsync():
        var form = await httpContext.Request.ReadFormAsync()
      He also mentioned that:
       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 hardcode 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.


1. Read the 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.

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

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

© 2023 - ErnesTech LLC- Privacy