【发布时间】:2011-02-06 05:10:17
【问题描述】:
我有一个名为“MyService”的简单 Windows 服务,它使用 WCF 构建,在启动时会向“HKEY_CLASSES_ROOT\Folder\Shell\{ContextMenuEntryName}”添加一个注册表项,从而允许在 Windows 中的任何位置进行右键单击菜单一个名为:{ContextMenuEntryName} 的条目。单击此条目时执行的目标是另一个程序,例如“{路径}\serviceclient.exe %1”。 “%1”为我提供了调用该上下文菜单的 windows 文件夹的路径。然后,该程序获取该值并通过创建服务代理并调用其方法将其传递给我的服务。
其 OnStart 方法的 WindowsService 代码如下:
protected override void OnStart(string[] args)
{
if (_serviceHost != null)
{
_serviceHost.Close();
}
// Add registry entry for context menu option System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
string contextMenuCommandPath =
Environment.CurrentDirectory.Substring(0, Environment.CurrentDirectory.IndexOf("MyService")) +
"serviceclient\\bin\\Debug\\serviceclient.exe %1";
_contextMenu.AddContextMenu(ContextMenuName, contextMenuCommandPath, "Folder");
_contextMenu.AddContextMenu(ContextMenuName, contextMenuCommandPath, "*");
// Create a ServiceHost for the CalculatorService type and
// provide the base address.
_serviceHost = new ServiceHost(typeof(MyService));
// Open the ServiceHostBase to create listeners and start
// listening for messages.
_serviceHost.Open();
}
serviceclient.exe 添加一个名为“MyService”的服务引用后有以下代码:
// Instantiate service proxy
var myServiceClient = new MyService.MyServiceClient();
// Execute monitor target based on path specified (or default).
myServiceClient .Monitor(args.Length > 0 ? args[0] : string.Empty);
我想在单击上下文菜单条目时直接将参数传递给 Windows 服务本身,而不是调用另一个执行相同操作的单独程序。
是否可以让上下文菜单条目在 WCF 运行时直接将信息传递给 WCF 中的 Windows 服务?
【问题讨论】:
-
@TrueWill:Windows 服务可以托管 WCF 服务。这似乎是 op 正在使用的。他启动的 .exe 无疑会调用 WCF 服务。
-
@John:感谢 TrueWill 的澄清。是的,WCF 服务可以是 Windows 或 Web 服务。我已经建立了前者。我有一个 WCF 服务包装在一个 Windows 服务中,这是我正在尝试使用的。
-
实际上,WCF 服务可以两者兼而有之。您可以在 Windows 服务中托管使用 HTTP 绑定的 WCF 服务。
-
@John - 谢谢,不知道。我仍然看不到将它与 Windows 菜单系统集成的意义。托盘图标应用是一种更常见的服务控制方式。
-
@True:他并没有试图控制服务。他试图将有关 shell 文件系统的信息传递给 服务。它将是有关所选文件或文件夹的信息。
标签: wcf windows-services registry