我目前在.Net中維護一個內部應用程序。應用程序使用IPC一次維護一個運行會話;如果另一個會話嘗試打開(某人再次單擊該圖標,一些會打開一個已保存的結果文件),第二個會話將此信息傳達給第一個會話,然後終止,第一個會話將啓動請求的操作。如何使用IPC而不是本地管理員?
我目前做這個用的System.ServiceModel命名空間,就像這樣:
命名空間EventLogViewer.IPCServer {// 提供的通用框架,流程傳達給oneanother。
[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
給出的解決方法是將應用程序升級爲需要管理員權限。這並不原因有二:
- 這個應用程序不需要管理員privledges否則
- 我們使用的ClickOnce爲我們的內部工具部署和ClickOnce的不支持大於運行的進程啓動的任何其他。
所以在這一點上,我需要找到一個不需要管理員權限的IPC解決方案,並讓我實現最初的目標:檢測已經運行的代碼實例並告訴它該做什麼,否則啓動它自己。任何有關如何做的建議(在.Net框架內,請不要使用第三方解決方案)將不勝感激。
請不要在標題中重複標記「.Net 3.5 C#:」。 – 2010-06-18 18:27:11