Ejemplo n.º 1
0
        public static void LaunchInExistingInstance(string[] args)
        {
            IpcChannel channel = new IpcChannel();
            ChannelServices.RegisterChannel(channel, false);
            string url = String.Format("ipc://{0}/{1}", ChannelName, SingleInstanceServiceName);

            // Register as client for remote object.
            WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(typeof(SingleInstanceHelper), url);
            RemotingConfiguration.RegisterWellKnownClientType(remoteType);

            // Create a message sink.
            string objectUri;
            IMessageSink messageSink = channel.CreateMessageSink(url, null, out objectUri);

            /*
            Console.WriteLine("The URI of the message sink is {0}.", objectUri);
            if (messageSink != null)
            {
                Console.WriteLine("The type of the message sink is {0}.", messageSink.GetType().ToString());
            }
            */

            SingleInstanceHelper helper = new SingleInstanceHelper();
            helper.Run(args);
        }
        public override void Open()
        {
            IpcChannel channel = new IpcChannel();

            System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel, false);

            string host = string.Format("ipc://localhost:{0}", Port);

            System.Runtime.Remoting.WellKnownClientTypeEntry remoteType =
                new System.Runtime.Remoting.WellKnownClientTypeEntry(
                    typeof(FARemoteObject), host + "/RemoteObject.rem");

            System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownClientType(remoteType);

            string objectUri;
            System.Runtime.Remoting.Messaging.IMessageSink messageSink = channel.CreateMessageSink(
                    host + "/RemoteObject.rem", null,
                    out objectUri);

            if (messageSink == null)
            {
                throw new Exception(string.Format("{0} Open fail. fail create message sink", Name));
            }

            _service = new FARemoteObject();

            try
            {
                _service.ConnectTest(); // 최초 접속을 시도한다.
            }
            catch
            {
                // 접속에 실패해도 넘어간다. 이후 SetData(), GetData() 가 호출될 때 접속이 되는지 확인한다.
            }
        }