【问题标题】:Have a context menu entry pass parameters to a windows service while it is running让上下文菜单条目在运行时将参数传递给 Windows 服务
【发布时间】: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


【解决方案1】:

乍一看并不像,但这是一个复杂的主题。见Creating Shortcut Menu Handlers

【讨论】:

  • 是的,这确实有点棘手。 Windows 服务的问题在于它只接受启动时的参数(在其 OnStart() 方法中)。我可以让上下文菜单条目与服务对话的唯一其他方法是客户端程序,它创建托管在我的 Windows 服务内的 WCF 端点的代理,并调用它的方法并将上下文菜单传递给它的值传递给它。我希望通过某种方式让 Windows 服务自给自足,而不必依赖客户端程序,该客户端程序只不过是消息传递的促进者。
  • @Cranial:如果不启动某种 .exe,我没有办法做到这一点,但也许如果你阅读整个链接文章,你会看到另一种方式。
【解决方案2】:

如果您在 Windows 服务中实现了命名管道服务器,也许您可​​以直接从上下文菜单命令发送文件路径数据,例如:

echo %1 > \\.\pipe\{pipe-name}

但是,您不能使用标准 WCF NetNamedPipeBinding 来实现管道服务器,因为 all the .NET-specific protocol requirements 已融入该绑定,因此您需要使用 System.IO.Pipes 类来实现,或者编写自定义 WCF仅从管道接收原始消息的传输。

【讨论】:

    猜你喜欢
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 2023-03-14
    • 2011-09-07
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多