Exemplo n.º 1
0
        public virtual void RpcTimeout()
        {
            RpcFactory anRpcFactory = new RpcFactory(mySerializer);

            anRpcFactory.RpcTimeout = TimeSpan.FromSeconds(1);

            IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(new HelloService());
            IRpcClient <IHello>  anRpcClient  = anRpcFactory.CreateClient <IHello>();

            try
            {
                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));


                IHello aServiceProxy = anRpcClient.Proxy;
                Assert.Throws <TimeoutException>(() => aServiceProxy.Timeout());
            }
            finally
            {
                if (anRpcClient.IsDuplexOutputChannelAttached)
                {
                    anRpcClient.DetachDuplexOutputChannel();
                }

                if (anRpcService.IsDuplexInputChannelAttached)
                {
                    anRpcService.DetachDuplexInputChannel();
                }
            }
        }
Exemplo n.º 2
0
        public void RpcCallError()
        {
            RpcFactory           anRpcFactory = new RpcFactory(mySerializer);
            IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(new HelloService());
            IRpcClient <IHello>  anRpcClient  = anRpcFactory.CreateClient <IHello>();

            try
            {
                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));


                IHello aServiceProxy = anRpcClient.Proxy;
                Assert.Throws <RpcException>(() => aServiceProxy.Fail());
            }
            finally
            {
                if (anRpcClient.IsDuplexOutputChannelAttached)
                {
                    anRpcClient.DetachDuplexOutputChannel();
                }

                if (anRpcService.IsDuplexInputChannelAttached)
                {
                    anRpcService.DetachDuplexInputChannel();
                }
            }
        }
Exemplo n.º 3
0
        public void DynamicRpcCall()
        {
            RpcFactory           anRpcFactory = new RpcFactory(mySerializer);
            IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(new HelloService());
            IRpcClient <IHello>  anRpcClient  = anRpcFactory.CreateClient <IHello>();

            try
            {
                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));


                int k = (int)anRpcClient.CallRemoteMethod("Sum", 1, 2);

                Assert.AreEqual(3, k);
            }
            finally
            {
                if (anRpcClient.IsDuplexOutputChannelAttached)
                {
                    anRpcClient.DetachDuplexOutputChannel();
                }

                if (anRpcService.IsDuplexInputChannelAttached)
                {
                    anRpcService.DetachDuplexInputChannel();
                }
            }
        }
Exemplo n.º 4
0
        public void RpcCall_NullArgument()
        {
            RpcFactory           anRpcFactory = new RpcFactory(mySerializer);
            IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(new HelloService());
            IRpcClient <IHello>  anRpcClient  = anRpcFactory.CreateClient <IHello>();

            try
            {
                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));


                IHello aServiceProxy = anRpcClient.Proxy;
                string k             = aServiceProxy.CreateString(null);

                Assert.AreEqual(null, k);
            }
            finally
            {
                if (anRpcClient.IsDuplexOutputChannelAttached)
                {
                    anRpcClient.DetachDuplexOutputChannel();
                }

                if (anRpcService.IsDuplexInputChannelAttached)
                {
                    anRpcService.DetachDuplexInputChannel();
                }
            }
        }
Exemplo n.º 5
0
        public void SubscribeBeforeAttachOutputChannel()
        {
            RpcFactory           anRpcFactory = new RpcFactory(mySerializer);
            HelloService         aService     = new HelloService();
            IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(aService);
            IRpcClient <IHello>  anRpcClient  = anRpcFactory.CreateClient <IHello>();

            try
            {
                IHello aServiceProxy = anRpcClient.Proxy;

                AutoResetEvent aClientConnected = new AutoResetEvent(false);
                anRpcClient.ConnectionOpened += (x, y) =>
                {
                    aClientConnected.Set();
                };

                AutoResetEvent             anEventReceived = new AutoResetEvent(false);
                Action <object, EventArgs> anEventHandler  = (x, y) =>
                {
                    anEventReceived.Set();
                };

                // Subscribe before the connection is open.
                aServiceProxy.Close += anEventHandler.Invoke;

                // Open the connection.
                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));

                // Wait until the client is connected.
                aClientConnected.WaitOne();

                // Raise the event in the service.
                aService.RaiseClose();

                Assert.IsTrue(anEventReceived.WaitOne());

                // Unsubscribe.
                aServiceProxy.Close -= anEventHandler.Invoke;

                // Try to raise again.
                aService.RaiseClose();

                Assert.IsFalse(anEventReceived.WaitOne(1000));
            }
            finally
            {
                if (anRpcClient.IsDuplexOutputChannelAttached)
                {
                    anRpcClient.DetachDuplexOutputChannel();
                }

                if (anRpcService.IsDuplexInputChannelAttached)
                {
                    anRpcService.DetachDuplexInputChannel();
                }
            }
        }
Exemplo n.º 6
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();
                }
            }
        }
Exemplo n.º 7
0
        public void RpcNonGenericEvent_10000()
        {
            RpcFactory           anRpcFactory = new RpcFactory(mySerializer);
            HelloService         aService     = new HelloService();
            IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(aService);
            IRpcClient <IHello>  anRpcClient  = anRpcFactory.CreateClient <IHello>();

            try
            {
                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));

                IHello aServiceProxy = anRpcClient.Proxy;

                int                        aCounter        = 0;
                AutoResetEvent             anEventReceived = new AutoResetEvent(false);
                Action <object, EventArgs> anEventHandler  = (x, y) =>
                {
                    ++aCounter;
                    if (aCounter == 10000)
                    {
                        anEventReceived.Set();
                    }
                };

                // Subscribe.
                aServiceProxy.Close += anEventHandler.Invoke;

                Stopwatch aStopWatch = new Stopwatch();
                aStopWatch.Start();

                // Raise the event in the service.
                for (int i = 0; i < 10000; ++i)
                {
                    aService.RaiseClose();
                }

                Assert.IsTrue(anEventReceived.WaitOne());

                aStopWatch.Stop();
                Console.WriteLine("Remote event. Elapsed time = " + aStopWatch.Elapsed);

                // Unsubscribe.
                aServiceProxy.Close -= anEventHandler.Invoke;
            }
            finally
            {
                if (anRpcClient.IsDuplexOutputChannelAttached)
                {
                    anRpcClient.DetachDuplexOutputChannel();
                }

                if (anRpcService.IsDuplexInputChannelAttached)
                {
                    anRpcService.DetachDuplexInputChannel();
                }
            }
        }
Exemplo n.º 8
0
        public void RpcCall_10000()
        {
            RpcFactory           anRpcFactory = new RpcFactory(mySerializer);
            IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(new HelloService());
            IRpcClient <IHello>  anRpcClient  = anRpcFactory.CreateClient <IHello>();

            try
            {
                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));

                IHello aServiceProxy = anRpcClient.Proxy;

                Stopwatch aStopWatch = new Stopwatch();
                aStopWatch.Start();

                //EneterTrace.StartProfiler();

                for (int i = 0; i < 10000; ++i)
                {
                    aServiceProxy.Sum(1, 2);
                }

                //EneterTrace.StopProfiler();

                aStopWatch.Stop();
                Console.WriteLine("Rpc call. Elapsed time = " + aStopWatch.Elapsed);


                HelloService aService = new HelloService();

                Stopwatch aStopWatch2 = new Stopwatch();
                aStopWatch2.Start();

                for (int i = 0; i < 10000; ++i)
                {
                    aService.Sum(1, 2);
                }

                aStopWatch2.Stop();
                Console.WriteLine("Local call. Elapsed time = " + aStopWatch2.Elapsed);
            }
            finally
            {
                if (anRpcClient.IsDuplexOutputChannelAttached)
                {
                    anRpcClient.DetachDuplexOutputChannel();
                }

                if (anRpcService.IsDuplexInputChannelAttached)
                {
                    anRpcService.DetachDuplexInputChannel();
                }
            }
        }
Exemplo n.º 9
0
        public void DynamicRpcGenericEvent()
        {
            RpcFactory           anRpcFactory = new RpcFactory(mySerializer);
            HelloService         aService     = new HelloService();
            IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(aService);
            IRpcClient <IHello>  anRpcClient  = anRpcFactory.CreateClient <IHello>();

            try
            {
                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));

                AutoResetEvent          anEventReceived = new AutoResetEvent(false);
                string                  aReceivedEvent  = "";
                EventHandler <OpenArgs> anEventHandler  = (x, y) =>
                {
                    aReceivedEvent = y.Name;
                    anEventReceived.Set();
                };

                // Subscribe.
                anRpcClient.SubscribeRemoteEvent("Open", anEventHandler);

                // Raise the event in the service.
                OpenArgs anOpenArgs = new OpenArgs()
                {
                    Name = "Hello"
                };
                aService.RaiseOpen(anOpenArgs);

                Assert.IsTrue(anEventReceived.WaitOne());
                Assert.AreEqual("Hello", aReceivedEvent);

                // Unsubscribe.
                anRpcClient.UnsubscribeRemoteEvent("Open", anEventHandler);

                // Try to raise again.
                aService.RaiseOpen(anOpenArgs);

                Assert.IsFalse(anEventReceived.WaitOne(1000));
            }
            finally
            {
                if (anRpcClient.IsDuplexOutputChannelAttached)
                {
                    anRpcClient.DetachDuplexOutputChannel();
                }

                if (anRpcService.IsDuplexInputChannelAttached)
                {
                    anRpcService.DetachDuplexInputChannel();
                }
            }
        }
Exemplo n.º 10
0
        public void RpcGenericEvent()
        {
            RpcFactory           anRpcFactory = new RpcFactory(mySerializer);
            HelloService         aService     = new HelloService();
            IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(aService);
            IRpcClient <IHello>  anRpcClient  = anRpcFactory.CreateClient <IHello>();

            try
            {
                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));

                IHello aServiceProxy = anRpcClient.Proxy;

                AutoResetEvent            anEventReceived = new AutoResetEvent(false);
                Action <object, OpenArgs> anEventHandler  = (x, y) =>
                {
                    anEventReceived.Set();
                };

                // Subscribe.
                aServiceProxy.Open += anEventHandler.Invoke;

                // Raise the event in the service.
                OpenArgs anOpenArgs = new OpenArgs()
                {
                    Name = "Hello"
                };
                aService.RaiseOpen(anOpenArgs);

                Assert.IsTrue(anEventReceived.WaitOne());

                // Unsubscribe.
                aServiceProxy.Open -= anEventHandler.Invoke;

                // Try to raise again.
                aService.RaiseOpen(anOpenArgs);

                Assert.IsFalse(anEventReceived.WaitOne(1000));
            }
            finally
            {
                if (anRpcClient.IsDuplexOutputChannelAttached)
                {
                    anRpcClient.DetachDuplexOutputChannel();
                }

                if (anRpcService.IsDuplexInputChannelAttached)
                {
                    anRpcService.DetachDuplexInputChannel();
                }
            }
        }
Exemplo n.º 11
0
 public void init()
 {
     try
     {
         IRpcFactory rpcFactory = new RpcFactory();
         RpcObj = rpcFactory.CreateClient<ILeiloeiro>();
         TcpMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
         IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://" + IP + ":" + Porta + "/");//ip local e porta do servidor
         RpcObj.AttachDuplexOutputChannel(anOutputChannel);
     }
     catch (Exception ex)
     {
         throw new Exception("Erro: É necessario iniciar o servidor - " + ex.Message);
     }
 }
Exemplo n.º 12
0
        public void DynamicRpcNonGenericEvent()
        {
            RpcFactory           anRpcFactory = new RpcFactory(mySerializer);
            HelloService         aService     = new HelloService();
            IRpcService <IHello> anRpcService = anRpcFactory.CreateSingleInstanceService <IHello>(aService);
            IRpcClient <IHello>  anRpcClient  = anRpcFactory.CreateClient <IHello>();

            try
            {
                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId));

                AutoResetEvent           anEventReceived = new AutoResetEvent(false);
                EventHandler <EventArgs> anEventHandler  = (x, y) =>
                {
                    anEventReceived.Set();
                };

                // Subscribe.
                anRpcClient.SubscribeRemoteEvent <EventArgs>("Close", anEventHandler);

                // Raise the event in the service.
                aService.RaiseClose();

                Assert.IsTrue(anEventReceived.WaitOne());

                // Unsubscribe.
                anRpcClient.UnsubscribeRemoteEvent("Close", anEventHandler);

                // Try to raise again.
                aService.RaiseClose();

                Assert.IsFalse(anEventReceived.WaitOne(1000));
            }
            finally
            {
                if (anRpcClient.IsDuplexOutputChannelAttached)
                {
                    anRpcClient.DetachDuplexOutputChannel();
                }

                if (anRpcService.IsDuplexInputChannelAttached)
                {
                    anRpcService.DetachDuplexInputChannel();
                }
            }
        }
Exemplo n.º 13
0
        public void RpcGenericEvent_SerializerPerClient()
        {
            string aFirstClientId = null;

            RpcFactory anRpcClientFactory1 = new RpcFactory(new XmlStringSerializer());
            RpcFactory anRpcClientFactory2 = new RpcFactory(new BinarySerializer());
            RpcFactory anRpcServiceFactory = new RpcFactory()
            {
                SerializerProvider = x => (x == aFirstClientId) ? (ISerializer) new XmlStringSerializer() : (ISerializer) new BinarySerializer()
            };

            HelloService         aService     = new HelloService();
            IRpcService <IHello> anRpcService = anRpcServiceFactory.CreateSingleInstanceService <IHello>(aService);

            anRpcService.ResponseReceiverConnected += (x, y) => aFirstClientId = aFirstClientId ?? y.ResponseReceiverId;

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

            try
            {
                anRpcService.AttachDuplexInputChannel(myMessaging.CreateDuplexInputChannel(myChannelId));
                anRpcClient1.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId, "Client1"));
                anRpcClient2.AttachDuplexOutputChannel(myMessaging.CreateDuplexOutputChannel(myChannelId, "Client2"));

                AutoResetEvent            anEvent1Received = new AutoResetEvent(false);
                Action <object, OpenArgs> anEventHandler1  = (x, y) =>
                {
                    anEvent1Received.Set();
                };

                AutoResetEvent            anEvent2Received = new AutoResetEvent(false);
                Action <object, OpenArgs> anEventHandler2  = (x, y) =>
                {
                    anEvent2Received.Set();
                };

                // Subscribe.
                anRpcClient1.Proxy.Open += anEventHandler1.Invoke;
                anRpcClient2.Proxy.Open += anEventHandler2.Invoke;

                // Raise the event in the service.
                OpenArgs anOpenArgs = new OpenArgs()
                {
                    Name = "Hello"
                };
                aService.RaiseOpen(anOpenArgs);

                anEvent1Received.WaitIfNotDebugging(1000);
                anEvent2Received.WaitIfNotDebugging(1000);

                // Unsubscribe.
                anRpcClient1.Proxy.Open -= anEventHandler1.Invoke;
                anRpcClient2.Proxy.Open -= anEventHandler2.Invoke;

                // Try to raise again.
                aService.RaiseOpen(anOpenArgs);

                Assert.IsFalse(anEvent1Received.WaitOne(1000));
                Assert.IsFalse(anEvent2Received.WaitOne(1000));
            }
            finally
            {
                if (anRpcClient1.IsDuplexOutputChannelAttached)
                {
                    anRpcClient1.DetachDuplexOutputChannel();
                }

                if (anRpcClient2.IsDuplexOutputChannelAttached)
                {
                    anRpcClient2.DetachDuplexOutputChannel();
                }

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