示例#1
0
        public void PerClientInstanceService()
        {
            RpcFactory           anRpcFactory = new RpcFactory(mySerializer);
            IRpcService <IHello> anRpcService = anRpcFactory.CreatePerClientInstanceService <IHello>(() => new HelloService());

            IRpcClient <IHello> anRpcClient1 = anRpcFactory.CreateClient <IHello>();
            IRpcClient <IHello> anRpcClient2 = anRpcFactory.CreateClient <IHello>();

            try
            {
                ManualResetEvent aClient2Connected = new ManualResetEvent(false);

                string aService1IdFromEvent = null;
                anRpcClient1.Proxy.Open += (x, y) =>
                {
                    aService1IdFromEvent = y.InstanceId;
                };

                string aService2IdFromEvent = null;
                anRpcClient2.Proxy.Open += (x, y) =>
                {
                    aService2IdFromEvent = y.InstanceId;
                    aClient2Connected.Set();
                };

                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient1.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));
                anRpcClient2.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));

                aClient2Connected.WaitOne(1000);

                string aServiceId1 = anRpcClient1.Proxy.GetInstanceId();
                string aServiceId2 = anRpcClient2.Proxy.GetInstanceId();

                Assert.IsFalse(string.IsNullOrEmpty(aServiceId1));
                Assert.IsFalse(string.IsNullOrEmpty(aService1IdFromEvent));
                Assert.IsFalse(string.IsNullOrEmpty(aServiceId2));
                Assert.IsFalse(string.IsNullOrEmpty(aService2IdFromEvent));
                Assert.AreEqual(aServiceId1, aService1IdFromEvent);
                Assert.AreEqual(aServiceId2, aService2IdFromEvent);
                Assert.AreNotEqual(aServiceId1, aServiceId2);
            }
            finally
            {
                if (anRpcClient1.IsDuplexOutputChannelAttached)
                {
                    anRpcClient1.DetachDuplexOutputChannel();
                }
                if (anRpcClient2.IsDuplexOutputChannelAttached)
                {
                    anRpcClient2.DetachDuplexOutputChannel();
                }

                if (anRpcService.IsDuplexInputChannelAttached)
                {
                    anRpcService.DetachDuplexInputChannel();
                }
            }
        }