我有一个名为“MyService”的简单Windows服务,使用WCF构建,在启动时将注册表项添加到“HKEY_CLASSES_ROOT \ Folder \ Shell \ {ContextMenuEntryName}”,从而允许在Windows中的任何位置使用右键菜单名为{ContextMenuEntryName}的条目。单击此条目时执行的目标是另一个程序,例如“{Path} \ 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();
}
在向其添加名为“MyService”的服务引用后,serviceclient.exe具有以下代码:
// 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服务?
答案 0 :(得分:1)
乍一看看起来并不像,但这是一个复杂的主题。请参阅Creating Shortcut Menu Handlers。
答案 1 :(得分:0)
如果您在Windows服务中实现了命名管道服务器,则可以直接从上下文菜单命令发送文件路径数据,如下所示:
echo %1 > \\.\pipe\{pipe-name}
但是,您无法使用标准WCF NetNamedPipeBinding来实现管道服务器,因为all the .NET-specific protocol requirements已绑定到该绑定中,因此您需要使用System.IO.Pipes
类来执行此操作,或者编写一个自定义WCF传输,它只接收来自管道的原始消息。