使用 ServiceController 启动的 Windows 服务抛出“RPC 服务器不可用”执行 WMI 连接时

发布于 2024-12-14 12:46:59 字数 874 浏览 0 评论 0原文

我使用安装程序安装了一个 Windows 服务,然后开始使用 ServiceController

public static int StartService(string serviceName, int timeoutMilliseconds)
{
    ServiceController service = new ServiceController(serviceName);
    try
    {
        TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

        service.Start();
        service.WaitForStatus(ServiceControllerStatus.Running, timeout);

        service.Close();

        return 0;
     }
     catch
     {
        return 1;
     }
}

该服务似乎启动得很好,但是当该服务尝试对远程计算机执行 WMI 调用时,它会抛出异常;

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

我尝试从运行服务的计算机上使用 WBEMTest 连接到服务尝试连接的同一台计算机,并且工作正常。

另外,如果我从 Services.msc 手动启动该服务,它也可以正常工作。我对 ServiceController 缺少什么?

I have a Windows service being installed with my installer, and then started with the use of ServiceController:

public static int StartService(string serviceName, int timeoutMilliseconds)
{
    ServiceController service = new ServiceController(serviceName);
    try
    {
        TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

        service.Start();
        service.WaitForStatus(ServiceControllerStatus.Running, timeout);

        service.Close();

        return 0;
     }
     catch
     {
        return 1;
     }
}

The service seems to start just fine, but when the service tries to perform WMI calls to remote computers, it throws an exception;

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

I've tried connecting with WBEMTest from the machine the service is running on, to the same machine the service tries to connect to, and it works fine.

Also, if I start the service manually from Services.msc, it works perfectly. What am I missing with ServiceController?

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

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

发布评论

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

评论(2

走过海棠暮 2024-12-21 12:46:59

我已经弄清楚了。

配置服务的 .config 文件时,我使用 [UserName] 和 [Password] 等占位符来替换用户在安装程序中给出的实际值。

服务在这些值被换出之前启动,并且服务尝试使用用户名和密码作为 [UserName] 和 [Password] 进行连接。

我一开始没有想到这种可能性,因为我以为我会收到“访问被拒绝”错误,但由于某种原因,当用户名包含 [ 或 ] 时,连接返回“RPC 服务器不可用”。

I've figured it out.

When configuring the .config file of the service I use placeholders like [UserName] and [Password] to replace actual values given by the user in the installer.

The service got started before these values were swapped out, and the service tried connecting with the username and password as [UserName] and [Password].

I didnt think of this possibility at first because I thought I would get an "Access is denied" error, but for some reason when the username contains [ or ] the connection returns "RPC server not available".

溺深海 2024-12-21 12:46:59

我敢打赌,所需的服务(RPC)在调用之前尚未启动。

也就是说,您的服务必须启动,或者至少只有在有能力时才开始处理,这就是 RPC 服务启动时。在 RPC 启动之前进行的任何依赖于 RPC 的调用都将导致失败。

据我所知,依赖信息存储在注册表中;您可以使用您的解决方案部署注册表脚本并在安装时运行它。

因此,例如,您需要在以下位置创建一个值:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\<Service>

它存储您自己所依赖的服务的注册表项名称。

I'd bet on the required services (RPC) not yet being started prior to calls.

That is to say, your service must start, or at least only start processing, when it has the means to do so, which would be when the RPC service is started. Any calls dependent on RPC made prior to RPC starting will result in failure.

Dependency information is stored in the registry, to my knowledge; you can deploy a registry script with your solution and run it upon installation.

So, for instance, you will need to create a value at the following location:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\<Service>

Which stores the registry key names of the services on which your own depends.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文