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?