如何在没有本地管理员的情况下使用IPC?

问题描述 投票:0回答:2

我目前在 .Net 中维护一个内部应用程序。应用程序使用 IPC 一次维护一个正在运行的会话;如果另一个会话尝试打开(有人再次单击该图标,有人打开保存的结果文件),第二个会话会将其传达给第一个会话,然后终止,第一个会话然后启动请求的操作。

我目前使用 System.ServiceModel 命名空间执行此操作,如下所示:

命名空间EventLogViewer.IPCServer { //为进程之间的通信提供通用框架。

[ServiceContract(Namespace = "http://ELV.domain")]
interface IELVLauncher
{
    [OperationContract]
    bool LaunchTabs(String[] fileNames);
}
public delegate void TabLauncher(String[] fileNames);
class ELVLauncherServer : IDisposable
{
    ServiceHost ipcService;
    public ELVLauncherServer()
    {
        Uri baseAddress = new Uri("Http://localhost:45600/elvlauncher/service");
        String address = "net.pipe://localhost/elvlauncher/tablauncher";

        ipcService = new ServiceHost(typeof(ELVLauncher), baseAddress);

        NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
        ipcService.AddServiceEndpoint(typeof(IELVLauncher), binding, address);

        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
        behavior.HttpGetEnabled = true;
        behavior.HttpGetUrl = new Uri("http://localhost:45601/elvlauncher");
        ipcService.Description.Behaviors.Add(behavior);
        Utilities.WriteMemoryDebugStatement(new DebugStatement(DebugStatement.StatementType.INFO, "Registering IPC Service"));
        ipcService.Open();

    }

    #region IDisposable Members

    public void Dispose()
    {
        ipcService.Close();
    }

    #endregion
}
public class ELVLauncher : IELVLauncher
{
    #region IELVLauncher Members

    public bool LaunchTabs(string[] fileNames)
    {
        try
        {
            Utilities.WriteMemoryDebugStatement(new DebugStatement(DebugStatement.StatementType.INFO, String.Format("IPC request received, files: {0}", String.Join(", ", fileNames))));
            Program.mainWindow.backgroundListener_OnLaunchRequestReceived(fileNames);
        }
        catch (Exception exception)
        {
            Utilities.WriteMemoryDebugStatement(new DebugStatement(exception));
        }
        return (true);
    }

    #endregion

}

此代码最初是在 Windows XP 下开发的,与大多数公司一样,我们没有迁移到 Vista。由于我们都以本地管理员身份运行,因此这段代码运行没有问题。

但是,我们现在正在过渡到 Win 7,此代码在启动时会产生异常,因为它无法注册 IPC 端点。更多信息在这里:http://mark.michaelis.net/Blog/WindowsCommunicationFoundationWithWindowsVistaAndUAC.aspx

给出的解决方法是将应用程序升级为需要管理员权限。这不起作用有两个原因:

  1. 此应用程序不需要管理员权限,否则
  2. 我们使用 ClickOnce 来部署我们的内部工具,ClickOnce 除了作为进程启动器运行之外不支持任何其他功能。

因此,此时我需要找到一个不需要管理员权限的 IPC 解决方案,让我实现最初的目标:检测已在运行的代码实例并告诉它要做什么,否则自行启动。任何有关做什么的建议(在 .Net 框架内,请不要使用第 3 方解决方案)将不胜感激。

c# .net-3.5 ipc servicehost
2个回答
3
投票

这是确保应用程序的单个会话的极其困难的方式(正如您所发现的,这是有问题的方式)。您应该重构/重做该功能以使用互斥体

这是另一个示例。 还有关于互斥体的必备 wiki 文章

编辑

为了绕过通信位,您可以在操作系统提供的临时文件夹中生成一个带有您自己的特殊前缀和/或扩展名的临时文件,单个应用程序将轮询该临时文件(过滤您的特殊前缀和/或扩展名)以查看是否有更多请求制成。参见:

System.IO.Path.GetTempFileName 

System.IO.Path.GetTempPath

2
投票

一定要使用 named mutex 将应用程序限制为单个实例,然后您可以使用 NamedPipeServerStreamNamedPipeClientStream 来执行不需要管理员权限的 IPC。

© www.soinside.com 2019 - 2024. All rights reserved.