/// <summary> /// コンストラクタ /// </summary> /// <param name="targetAssembly">対象アプリケーションのアセンブリ</param> public ApplicationInstance(Assembly targetAssembly) { // アプリケーションの GUID を取得 var guid = (GuidAttribute)Attribute.GetCustomAttribute(targetAssembly, typeof(GuidAttribute)); if (guid == null) { throw new Exception("アプリケーションにGUID が設定されていません。"); } var portName = guid.Value; // セッションごとの URI となるように、セッション ID を URI として使用 var uri = Process.GetCurrentProcess().SessionId.ToString(CultureInfo.InvariantCulture); // IPC サーバーの作成に成功した場合、指定したポートを使用する IPC 通信が初めて作成されたものとする // 作成に失敗した場合、別のインスタンスが IPC サーバーを作成しているので、クライアントを作成 // で、これらの処理が同時に発生しないよう、Mutex で保護 using (var mutex = new Mutex(true, typeof(ApplicationInstanceMessage).FullName + "_" + portName)) { mutex.WaitOne(); try { // サーバーを作成 this.channel = new IpcServerChannel(portName, portName, new BinaryServerFormatterSinkProvider { TypeFilterLevel = TypeFilterLevel.Full, }); ChannelServices.RegisterChannel(this.channel, true); RemotingConfiguration.RegisterWellKnownServiceType(typeof(ApplicationInstanceMessage), uri, WellKnownObjectMode.Singleton); this.appInstanceMessage = new ApplicationInstanceMessage(); RemotingServices.Marshal(this.appInstanceMessage, uri, typeof(ApplicationInstanceMessage)); // ApplicationInstanceServiceMessage の CommandLineArgsReceived イベントを購読して、 // このクラスに設定された CommandLineArgsReceived イベントを発生させる this.appInstanceMessage.MessageReceived += this.OnMessageReceived; this.IsFirst = true; } catch (RemotingException) { // クライアント作成 this.channel = new IpcClientChannel(); ChannelServices.RegisterChannel(this.channel, true); RemotingConfiguration.RegisterWellKnownClientType(typeof(ApplicationInstanceMessage), $"ipc://{portName}/{uri}"); this.appInstanceMessage = new ApplicationInstanceMessage(); this.IsFirst = false; } } }
public ApplicationInstance(Assembly targetAssembly) { // アプリケーションの GUID を取得 var portName = ((GuidAttribute)Attribute.GetCustomAttribute(targetAssembly, typeof(GuidAttribute))).Value; // セッションごとの URI となるように、セッション ID を URI として使用 var uri = Process.GetCurrentProcess().SessionId.ToString(CultureInfo.InvariantCulture); // IPC サーバーの作成に成功した場合、指定したポートを使用する IPC 通信が初めて作成されたものとする // 作成に失敗した場合、別のインスタンスが IPC サーバーを作成しているので、クライアントを作成 // で、これらの処理が同時に発生しないよう、Mutex で保護 using (var mutex = new Mutex(true, typeof(ApplicationInstanceMessage).FullName + "_" + portName)) { mutex.WaitOne(); try { // サーバーを作成 this._channel = new IpcServerChannel(portName, portName, new BinaryServerFormatterSinkProvider { TypeFilterLevel = TypeFilterLevel.Full, }); ChannelServices.RegisterChannel(this._channel, true); RemotingConfiguration.RegisterWellKnownServiceType(typeof(ApplicationInstanceMessage), uri, WellKnownObjectMode.Singleton); this._appInstanceMessage = new ApplicationInstanceMessage(); RemotingServices.Marshal(this._appInstanceMessage, uri, typeof(ApplicationInstanceMessage)); // ApplicationInstanceServiceMessage の CommandLineArgsReceived イベントを購読して、 // このクラスに設定された CommandLineArgsReceived イベントを発生させる this._appInstanceMessage.MessageReceived += this.OnMessageReceived; this.IsFirst = true; } catch (RemotingException) { // クライアント作成 this._channel = new IpcClientChannel(); ChannelServices.RegisterChannel(this._channel, true); RemotingConfiguration.RegisterWellKnownClientType(typeof(ApplicationInstanceMessage), $"ipc://{portName}/{uri}"); this._appInstanceMessage = new ApplicationInstanceMessage(); this.IsFirst = false; } } }