0

I have two duplicates ASP.NET restful api instants ( worker count = 2) on Azure web service. I need to crash one of them by code for testing Health Check feature purposes. So far what I have tried, which none of them worked

  1. Throw unhandled exceptions
  2. Load testing
  3. Use static variable in controller + url trigger to mark one of them to return bad requests for health check end point, but the static variable is not persist during the lifetime of the instance

The #3 could work if there is a way to make this variable modifiable by URL request, and make the modified value persists during the lifetime of the application.

How to define an application life time variable to achieve the goal of crashing one of two identical app instants? Or making one of them return bad request while the other one return good request?

VariableTao
  • 171
  • 8
  • What do you mean by "crash" would you try to make that app instance to completely goes down. Or are you trying to make all controllers in that instance return errors? And to confirm, you are using the standard Restful API template from microsoft with Startup.cs files right? – qkhanhpro Mar 10 '23 at 05:59
  • Make the instance completely goes down. We used a App_start folder without Startup.cs – VariableTao Mar 10 '23 at 06:02
  • Try this on a controller https://stackoverflow.com/a/2782367/3844353 The instance should not be able to recover OOM exception – qkhanhpro Mar 10 '23 at 06:05

1 Answers1

0

Thanks to qkhanhpro for the comments.

To have a static variable during the lifetime of the instance, you can store the value in a database or in a file. And also you can use the environment variable to determine the value of the static variable. For example, in .NET, you can use the

Environment.GetEnvironmentVariable method to retrieve the value of the environment variable.

using System;
String envVar = Environment.GetEnvironmentVariable("MY_VAR");

Sample code for Garbage collection

References taken from the MSDoc.

private const long maxGarbage = 1000;

        static void Main()
        {
            Program myGCCol = new Program();

            Console.WriteLine("The highest generation is {0}", GC.MaxGeneration);

            myGCCol.MakeSomeGarbage();

            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));

            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));

            GC.Collect(0);

            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));

            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));

            GC.Collect(2);

            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
            Console.Read();
        }

        void MakeSomeGarbage()
        {
            Version vt;

            for (int i = 0; i < maxGarbage; i++)
            {
                vt = new Version();
            }
        }

Garbage collection

enter image description here

Sample code to check Out of Memory exception.

        try
            {
                try
                {
                    string s = "This";
                    s = s.Insert(2, "is ");

                    throw new OutOfMemoryException();
                }
                catch (ArgumentException)
                {
                    Console.WriteLine("ArgumentException in String.Insert");
                }

            }
            catch (OutOfMemoryException e)
            {
                Console.WriteLine("Terminating application unexpectedly...");
                Console.ReadLine();

                Environment.FailFast(String.Format("Out of Memory: {0}",
                                                   e.Message));
            }

OOM Exception enter image description here

For further information refer to on GlobalMemoryStatusEx function and OOM exception.

Rajesh Mopati
  • 1,329
  • 1
  • 2
  • 7
  • The solution here, can it solve the problem that “ The #3 could work if there is a way to make this variable modifiable by URL request, and make the modified value persists during the lifetime of the application.” ? – VariableTao Mar 10 '23 at 13:09
  • Yes, to make the static variable modifiable by URL request and persist during the lifetime of the application, you can also store the value in a database or a shared cache. Like you can use Azure Redis Cache to store the value of the static variable. And you can then modify the value of the static variable by sending a request to the cache. And the modified value will persist in the cache and be available to all instances of the application. – Rajesh Mopati Mar 10 '23 at 13:17