通过命名管道从 Windows 服务(会话 #0)连接到桌面应用程序(会话 #1) [英] Connecting via named pipe from windows service (session#0) to desktop app (session #1)

查看:28
本文介绍了通过命名管道从 Windows 服务(会话 #0)连接到桌面应用程序(会话 #1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定:
- 应用程序 - 桌面 GUI (WPF) .NET 应用程序
- Windows 服务监视应用程序(.NET 也是)

Given:
- the application - desktop GUI (WPF) .NET app
- windows service watching for application (.NET also)

Windows 服务会定期ping"应用程序以确保其健康(如果不是,则 winservice 将重新启动它).
我打算通过命名管道实现ping".为了让事情更简单,我决定用 WCF 来做.该应用程序承载一个 WCF 服务(一个操作 Ping 返回一些东西).Windows 服务是此 WCF 服务的客户端,根据计时器定期调用它.

The windows service periodically "pings" application to get sure it's healthy (and if it's not winservice will restart it).
I was going to implement "pinging" via named pipes. To make things simpler I decided to do it with WCF. The application hosts a WCF-service (one operation Ping returning something). The windows service is a client for this WCF-service, invokes it periodically based on a timer.

这就是 Windows 7 的全部内容.
Windows 服务在 LocalService 下运行(在会话#0 中).
桌面应用程序在当前登录的用户下运行(在会话#1 中).

That's all in Windows 7.
Windows service is running under LocalService (in session#0).
Desktop application is running under currently logged in user (in session#1).

问题:
Windows 服务无法看到在桌面应用程序中创建和正在侦听的 WCF 端点(使用 NetNamedPipeBinding).这意味着在通过 wcf 代理调用时,我收到此异常:在您的本地计算机上找不到管道端点‘net.pipe://localhost/HeartBeat’"

The problem:
Windows service can't see WCF endpoint (with NetNamedPipeBinding) created in and being listened in desktop application. That means that on call via wcf proxy I get this exception: "The pipe endpoint 'net.pipe://localhost/HeartBeat' could not be found on your local machine"

我确定代码没问题,因为另一个桌面应用程序(在会话#1 中)可以看到端点.

I'm sure code is ok, because another desktop application (in session#1) can see the endpoint.

显然,这里我正在处理一些用于 Win32 系统对象隔离的安全问题.但我相信应该有办法解决我遇到的限制.
我可以牺牲 WCF 方法并采用原始的 NamedPipe 方法.

Obviously here I'm dealing with some security stuff for Win32 system object isolation. But I believe there should be a way to workaround restrictions I've encountered with.
I can sacrifice WCF approach and go the raw NamedPipe way.

推荐答案

一个更简单的解决方案可能是对承载 WCF 服务的 Windows 服务使用 WCF 双工契约.客户端应用程序将在服务启动时调用该服务的操作来注册自己.然后,Ping 将是由客户端回调合约上的服务定期调用的操作,应用将对此做出响应.

An easier solution might be to use a WCF duplex contract with the Windows service hosting the WCF service. The client App would call an operation on the service to register itself, when it starts up. The Ping would then be an operation invoked periodically by the service on the client's callback contract, to which the App would respond.

服务可见性是这样工作的,因为 Windows 服务可以使用 SeCreateGlobalPrivilege 运行,因此可以在全局内核命名空间中创建共享内存对象,服务通过该对象发布管道名称,对其他会话可见.交互式应用程序无法在 Windows7 中轻松获得该权限,因此此类应用程序中的 WCF 服务回退到在本地内核命名空间中发布管道,仅在其自己的会话中可见.

Service visibility works this way round, because the Windows service can run with SeCreateGlobalPrivilege, and so the shared memory object via which the pipe name is published by the service can be created in the Global kernel namespace, visible to other sessions. Interactive applications can't easily get that privilege in Windows7, so WCF services in such applications fall back to publishing the pipe in the Local kernel namespace, visible only within their own session.

这篇关于通过命名管道从 Windows 服务(会话 #0)连接到桌面应用程序(会话 #1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆