.Net Windows 服务:从引用的程序集安装

发布于 2024-10-05 08:24:12 字数 485 浏览 0 评论 0原文

有很多如何在一行中安装 Windows 服务的示例:

    ManagedInstallClass.InstallHelper(
      new[] { Assembly.GetExecutingAssembly().Location });

在 exe 模块中声明服务类之前,这可以正常工作。 但如果服务类位于引用的程序集中(未在可执行文件中声明,而是在链接的 dll 中声明),则相同的代码对我不起作用。

在这种情况下,服务也会注册,但无法启动,因为它是使用 dll 路径注册并指向 dll(当我尝试启动该服务时,事件日志中会出现“服务不是 win32 可执行文件”消息)

如果我更改GetExecutingAssembly().Location 到可执行路径,则找不到安装程序,并且根本不会注册服务。

是否可以将服务类放入引用的程序集中,并且仍然能够以最小的努力注册服务?

先感谢您!

There are lots of examples how to install windows service in one line:

    ManagedInstallClass.InstallHelper(
      new[] { Assembly.GetExecutingAssembly().Location });

That works fine until service class is declared in exe module.
But the same code doesn't work for me if service class is in referrenced assembly (not declared in executable, but in linked dll).

In such a case service is registered as well but can't be started as it is registered with dll path and points to dll ("service is not a win32 executable" message appears in event log when I try to start that)

If I change GetExecutingAssembly().Location to executable path, then no installers are found and service is not registered at all.

Is it possible to put service class into referenced assembly and still have ability to register service with minimum effort?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ花落谁相伴 2024-10-12 08:24:12

下面是一些 C# 代码,允许您“手动”安装/卸载服务(无需声明自定义 RunInstaller 属性):

static void InstallService(string path, string name, string displayName, string description)
{
    ServiceInstaller si = new ServiceInstaller();
    ServiceProcessInstaller spi = new ServiceProcessInstaller();
    si.Parent = spi;
    si.DisplayName = displayName;
    si.Description = description;
    si.ServiceName = name;
    si.StartType = ServiceStartMode.Manual;

    // update this if you want a different log
    si.Context = new InstallContext("install.log", null);
    si.Context.Parameters["assemblypath"] = path;

    IDictionary stateSaver = new Hashtable();
    si.Install(stateSaver);
}

static void UninstallService(string name)
{
    ServiceInstaller si = new ServiceInstaller();
    ServiceProcessInstaller spi = new ServiceProcessInstaller();
    si.Parent = spi;
    si.ServiceName = name;

    // update this if you want a different log
    si.Context = new InstallContext("uninstall.log", null);
    si.Uninstall(null);
}

here is some C# code that allows you to install/uninstall a service" manually" (without the need to declare custom RunInstaller attributes):

static void InstallService(string path, string name, string displayName, string description)
{
    ServiceInstaller si = new ServiceInstaller();
    ServiceProcessInstaller spi = new ServiceProcessInstaller();
    si.Parent = spi;
    si.DisplayName = displayName;
    si.Description = description;
    si.ServiceName = name;
    si.StartType = ServiceStartMode.Manual;

    // update this if you want a different log
    si.Context = new InstallContext("install.log", null);
    si.Context.Parameters["assemblypath"] = path;

    IDictionary stateSaver = new Hashtable();
    si.Install(stateSaver);
}

static void UninstallService(string name)
{
    ServiceInstaller si = new ServiceInstaller();
    ServiceProcessInstaller spi = new ServiceProcessInstaller();
    si.Parent = spi;
    si.ServiceName = name;

    // update this if you want a different log
    si.Context = new InstallContext("uninstall.log", null);
    si.Uninstall(null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文