Web Application -Vijay
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Momentum_App.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Threading.Tasks;
namespace Momentum_App.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IConfiguration configuration;
ProcessLog processLog = null;
ErrorLog errorLog = null;
public HomeController(ILogger<HomeController> logger, IConfiguration config)
{
_logger = logger;
configuration = config;
}
public ActionResult Index()
{
processLog = new ProcessLog();
try
{
processLog.ProcessLogFile("Index Method Called");
ViewBag.status = ServiceStatus().ToString();
List<ServerDetails> lstServerDetails = new List<ServerDetails>()
{
new ServerDetails(){ serverName="srv1.bbh.com"},
new ServerDetails(){ serverName="srv2.bbh.com"},
new ServerDetails(){ serverName="srv3.bbh.com"},
new ServerDetails(){ serverName="srv4.bbh.com"},
};
ViewBag.data = lstServerDetails;
return View();
}
catch (Exception ex)
{
errorLog = new ErrorLog();
errorLog.ErrorlogFile(ex.Message);
}
return View();
}
public ActionResult GetServerDetail()
{
processLog = new ProcessLog();
try
{
processLog.ProcessLogFile("GetServerDetail Method Called");
SqlConnection connection = new SqlConnection(configuration.GetConnectionString("DefaultConnection"));
SqlCommand command = new SqlCommand("select * from ServerDetails", connection);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "ServerDetails");
//ViewBag.ServerData = dataSet;
return View(dataSet);
}
catch (Exception ex)
{
errorLog = new ErrorLog();
errorLog.ErrorlogFile(ex.Message);
}
return View();
}
[HttpPost]
public ActionResult StartService()
{
Start();
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult StopService()
{
Stop();
return RedirectToAction("Index");
}
public void Stop()
{
processLog = new ProcessLog();
try
{
ServiceController serviceController = new ServiceController("MyWindowsService");
var Status = serviceController.Status.ToString();
processLog.ProcessLogFile("Window App Run : Status :- " + Status);
if (serviceController.Status == ServiceControllerStatus.Running)
{
TimeSpan timeout = TimeSpan.FromMilliseconds(5000);
serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
}
catch (Exception ex)
{
errorLog = new ErrorLog();
errorLog.ErrorlogFile(ex.Message);
}
}
public void Start()
{
processLog = new ProcessLog();
try
{
ServiceController serviceController = new ServiceController("MyWindowsService");
var Status = serviceController.Status.ToString();
processLog.ProcessLogFile("Window App Run : Status :- " + Status);
if (serviceController.Status != ServiceControllerStatus.Running && serviceController.Status != ServiceControllerStatus.StartPending)
{
TimeSpan timeout = TimeSpan.FromMilliseconds(5000);
serviceController.Start();
serviceController.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
catch (Exception ex)
{
errorLog = new ErrorLog();
errorLog.ErrorlogFile(ex.Message);
}
}
public ServiceControllerStatus ServiceStatus()
{
ServiceController serviceController = new ServiceController("MyWindowsService");
return serviceController.Status;
}
}
}