-1

I am implementing a console application to build application pool status.I referred check the status of IIS Application Pool of getting the username and password from database,I wanted to get them from appsettings.json.

The problem I am having is the value (the username jandi) doesn't pass to the program.cs, it passes a null value.

I have included my code.

program.cs

using Serilog;
using System;
using System.Configuration;
using System.DirectoryServices;
using System.Timers;

namespace processstatus
{
    class Program
    {
       

        static void Main(string[] args)
        {
            const double interval60Minutes = 5 * 5 * 1000; // milliseconds to one hour
            Timer checkForTime = new Timer(interval60Minutes);
            checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
            checkForTime.Enabled = true;
            Console.WriteLine("Waiting..");
            Console.ReadLine();
        }

        public static void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
        {
            GetApplicationPoolNames();
        }

        public static string GetApplicationPoolNames()
        {
            string UName = ConfigurationManager.AppSettings["User"];
            string Pwd = ConfigurationManager.AppSettings["Pass"];
            string ServerName = ConfigurationManager.AppSettings["Server"];
            DirectoryEntries appPools = null;
            try
            {
                appPools = new DirectoryEntry("IIS://" + ServerName + "/W3SVC/AppPools", UName, Pwd).Children;
            }
            catch (Exception ex)
            {
                Log.Error("serviceLogic -> InsertStatus() -> IIS Pool App Region -> DirectoryEntries -> Error: ", ex.Message.ToString());
            }

            Log.Information("IIS App Pool Section Started for " + System.Environment.MachineName.ToString());

            try
            {
                foreach (DirectoryEntry appPool in appPools)
                {
                    Log.Information("App Pool : " + appPool.Name.ToString());
                    int intStatus = 0;
                    string status = "";
                    try
                    {
                        if (appPool.Name.ToString().ToLower().Trim() == ConfigurationManager.AppSettings["Server"].ToString().ToLower().Trim())
                        {
                            Log.Information("Process Started for App Pool : " + appPool.Name.ToString());

                            intStatus = (int)appPool.InvokeGet("AppPoolState");
                            switch (intStatus)
                            {
                                case 2:
                                    status = "Running";
                                    break;
                                case 4:
                                    status = "Stopped";
                                    break;
                                default:
                                    status = "Unknown";
                                    break;
                            }

                            //Store status info to db or file Logic goes here..

                            //Start App pool, If any application pool status is not Running.
                            if (status != "Running")
                                appPool.Invoke("Start", null);

                            Log.Information("Process Completed for App Pool : " + appPool.Name.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("serviceLogic -> InsertStatus() -> IIS Pool App Region -> Error: ", ex.Message);

                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("serviceLogic -> InsertStatus() -> IIS Pool App Region -> DirectoryEntries -> Error: ", ex.Message);
            }
            return "Message";
        }
    }
}

Appsettings.json file

   {
  "AppSettings": {
    "User": "jandi",
    "Pass": "jandi123",
    "Server": "abc"
  }
}

Can someone please let me know the error in the above code?

5
  • 1
    Is this .NET Framework, .NET Core or .NET 5? Also, ConfigurationManager is for .NET Framework's app.config file, not for .NET Core's appsettings.json file Commented Aug 1, 2021 at 8:41
  • @ProgmanI refereed it already.So inorder to do this is it a must to use mvc architecture?Is there a way to accomplish the requirment without using mvc? Commented Aug 1, 2021 at 8:57
  • You don't need to use ASP.NET Core. You can just install the Microsoft.Extensions.Configuration packages (nuget.org/packages?q=microsoft.extensions.configuration), I think the Json one should be enough Commented Aug 1, 2021 at 9:05
  • 1
    ConfigurationManager reads the app.config file not an appsettings.json. You need to add a proper app.config with the appSettings section and then your code should work (provided that it compiles correctly) Commented Aug 1, 2021 at 9:15
  • This blog post seems to address the point How to read custom configurations from appsettings.json from a console application in net core Commented Aug 1, 2021 at 12:47

1 Answer 1

0

For .NET Core console apps, you can use Microsoft.Extensions.Configuration to read appsettings.json files, as has been already pointed out in the comments. I've used this a couple of times and it seems to work just fine. You can then use something like this in your Program class:

private static IConfiguration _configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", true, true)
    .Build();

You can then read the appsettings variables via the _configuration variable.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.