[英]Installing a windows service programmatically
我正在尝试通过C#以编程方式安装服务,但是遇到了无法解决的问题。
阅读了大量文档后,我相信微软存在一个错误,(但是我们都知道并非如此)。
所以这是我的应用程序的Main
部分。
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
if (System.Environment.UserInteractive)
{
string parameter = string.Concat(args);
switch (parameter)
{
case "/install":
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
Console.Read();
break;
case "/uninstall":
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
else
{
ServiceBase.Run(new ProxyMonitor());
}
}
在CMD内以诸如ProxyMonitor /install
类的管理特权执行时,进入的步骤将向下进行:
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
如预期的那样,然后像这样跳入我的安装类:
namespace Serco.Services.ProxyMonitor
{
[RunInstaller(true)]
public class ManagedInstallation : ServiceInstaller
{
public ManagedInstallation()
{
var ProcessInstaller = new ServiceProcessInstaller();
var ServiceInstaller = new ServiceInstaller();
//set the information and privileges
ProcessInstaller.Account = ServiceConfiguration.AccountType;
ServiceInstaller.DisplayName = ServiceConfiguration.DisplayName;
ServiceInstaller.StartType = ServiceConfiguration.StartType;
ServiceInstaller.Description = ServiceConfiguration.Description;
ServiceInstaller.ServiceName = ServiceConfiguration.ServiceName;
Installers.Add(ProcessInstaller);
Installers.Add(ServiceInstaller);
}
}
}
检查调试文件后,我得到以下信息:
Installing assembly 'C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe'.
Affected parameters are:
logtoconsole =
logfile = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.InstallLog
assemblypath = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe
Installing service ...
Creating EventLog source in log Application...
Rolling back assembly 'C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe'.
Affected parameters are:
logtoconsole =
logfile = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.InstallLog
assemblypath = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe
Restoring event log to previous state for source .
我还会在以下调用中引发异常:
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
说明:
安装失败,并且已执行回滚。 必须指定来源值。
更新:
namespace Serco.Services.ProxyMonitor
{
class ServiceConfiguration
{
public static string DisplayName
{
get { return "Serco Proxy Monitor"; }
}
public static string ServiceName
{
get { return "Serco Proxy Monitor"; }
}
public static string Description
{
get
{
return "Serco ProxyMonitor is a helper developed to manage the state of the proxy for the employess whilst of the internal network.";
}
}
public static ServiceStartMode StartType
{
get{return ServiceStartMode.Automatic;}
}
public static ServiceAccount AccountType
{
get{return ServiceAccount.LocalSystem;}
}
/*.. /Snip/ ..*/
}
}
我想通了,以为我会发布一些其他人可能遇到的相同问题。
这是几件事情的结合,但是生病了才迅速向您展示:
public static string ServiceName
{
get { return "Serco Proxy Monitor"; }
}
return "SercoProxyMonitor";
由于空间 UnhandledException
,该UnhandledException
随后显示了更多的深度堆栈跟踪 我认为主要的问题是, ServiceInstaller
使用的ServiceName
创建和EventLogSource
,而且因为有在内部空间EventLogSource
它被扔一个合适的。
日志源似乎为空; 您确定ServiceConfiguration.ServiceName
已定义并具有值吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.