I am writing a program in asp.net framework version 4.6.1 mvc. The goal is to be able to start and stop a windows service. I am trying to run it on windows 10 but get this error:
Server Error in '/' Application.
Unhandled Execution Error
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileLoadException:
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
I have ensured that i reference the System.ServiceProcess assembly in my controller and added 'identity impersonate="true" userName="myusername" password="mypassword" ' inside system.web in web.config
My controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceProcess;
namespace WebServiceApplication.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
//get status of service to reference in view
ViewBag.status = serviceStatus().ToString();
return View();
}
[HttpPost]
public ActionResult StartService()
{
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult StopService()
{
return RedirectToAction("Index");
}
private void stop()
{
//testing service with mongodb
ServiceController sc = new ServiceController("MongoDB");
//check if service is running
if (sc.Status == ServiceControllerStatus.Running)
{
TimeSpan timeOut = TimeSpan.FromMilliseconds(5000);//create 5 seconds timeout
sc.Stop();//initiate stop
sc.WaitForStatus(ServiceControllerStatus.Stopped, timeOut);//stop service before or after timeout
}
}
private void start()
{
ServiceController sc = new ServiceController("MongoDB");
if (sc.Status != ServiceControllerStatus.Running && sc.Status != ServiceControllerStatus.StartPending)
{
TimeSpan timeOut = TimeSpan.FromMilliseconds(5000);
sc.Start();//initiate start
sc.WaitForStatus(ServiceControllerStatus.Running, timeOut);//run service before or after timeout
}
}
public ServiceControllerStatus serviceStatus()
{
ServiceController sc = new ServiceController("MongoDB");
return sc.Status;//return the status of the service
}
}
}
And this is my view:
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
<h1>Service Management</h1>
</div>
@{ string status = ViewBag.status; }
@{ if (status.Equals("Running"))
{
<h2>Service Control</h2>
<p>The monitored service is: @ViewBag.status</p>
using(Html.BeginForm("StartService","Home"))
{
<input type="submit" class="btn btn-success" value="Start" disabled />
}
using(Html.BeginForm("StopService","Home"))
{
<input type="submit" class="btn btn-danger" value="Stop" />
}
}
else
{
<h2>Service Control</h2>
<p>The monitored service is: @ViewBag.status</p>
using(Html.BeginForm("StartService","Home"))
{
<input type="submit" class="btn btn-success" value="Start" />
}
using(Html.BeginForm("StopService","Home"))
{
<input type="submit" class="btn btn-danger" value="Stop" disabled />
}
}
}
Exception stack trace:
[FileLoadException]
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +36
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +152
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +77
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +21 System.Reflection.Assembly.Load(String assemblyString) +28
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +38[ConfigurationErrorsException: Could not load the assembly. The property 'assembly' must be a valid assembly.]
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +738
System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +57
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +170
System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +92 System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +290
System.Web.Compilation.BuildManager.ExecutePreAppStart() +157
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +549[HttpException (0x80004005): Could not load the assembly. The property 'assembly' must be a valid assembly.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10075124 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
User contributions licensed under CC BY-SA 3.0