public async void TestSendAndReceive()
        {
            var resolver = new DependencyResolver();

            using (var broker = new ZmqBroker(this.zmqContext, ClientInboundAddress, ClientOutboundAddress, ServerInboundAddress, ServerOutboundAddress))
            {
                broker.Listen();

                using (var server = new ZmqServer(this.zmqContext, ServerInboundAddress, ServerOutboundAddress, new ServiceFactory(resolver)))
                {
                    server.Listen();

                    using (var client = new ZmqClient(this.zmqContext, ClientInboundAddress, ClientOutboundAddress))
                    {
                        var clientFactory = new ServiceClientFactory(client);

                        var serviceClient = clientFactory.CreateServiceClient<ITestService2>();

                        Assert.That(serviceClient.GetPerson(1), Is.Not.Null);

                        var persons = await serviceClient.ListPersonsAsync(5);
                        Assert.That(persons, Is.Not.Null);
                        Assert.AreEqual(5, persons.Count());

                        var nullCollection = await serviceClient.ListPersonsAsync(-1);
                        Assert.IsNull(nullCollection);

                        var nullObject = serviceClient.GetPerson(-1);
                        Assert.IsNull(nullObject);
                    }
                }
            }
        }
        public async void TestSendAndReceive()
        {
            var resolver = new DependencyResolver();

            using (var broker = new ZmqBroker(this.zmqContext, ClientInboundAddress, ClientOutboundAddress, ServerInboundAddress, ServerOutboundAddress))
            {
                broker.Listen();

                using (var server = new ZmqServer(this.zmqContext, ServerInboundAddress, ServerOutboundAddress, new ServiceFactory(resolver)))
                {
                    server.Listen();

                    using (var client = new ZmqClient(this.zmqContext, ClientInboundAddress, ClientOutboundAddress))
                    {
                        var clientFactory = new ServiceClientFactory(client);

                        var serviceClient = clientFactory.CreateServiceClient <ITestService2>();

                        Assert.That(serviceClient.GetPerson(1), Is.Not.Null);

                        var persons = await serviceClient.ListPersonsAsync(5);

                        Assert.That(persons, Is.Not.Null);
                        Assert.AreEqual(5, persons.Count());

                        var nullCollection = await serviceClient.ListPersonsAsync(-1);

                        Assert.IsNull(nullCollection);

                        var nullObject = serviceClient.GetPerson(-1);
                        Assert.IsNull(nullObject);
                    }
                }
            }
        }
        public async void TestSendAndReceiveExceptions()
        {
            var resolver = new DependencyResolver();

            using (var broker = new ZmqBroker(this.zmqContext, ClientInboundAddress, ClientOutboundAddress, ServerInboundAddress, ServerOutboundAddress))
            {
                broker.Listen();

                using (var server = new ZmqServer(this.zmqContext, ServerInboundAddress, ServerOutboundAddress, new ServiceFactory(resolver)))
                {
                    server.Listen();

                    using (var client = new ZmqClient(this.zmqContext, ClientInboundAddress, ClientOutboundAddress))
                    {
                        var clientFactory = new ServiceClientFactory(client);

                        var serviceClient = clientFactory.CreateServiceClient<ITestService>();

                        //Synchronous
                        var err = Assert.Catch(async () => await serviceClient.FailAsync());
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf<AggregateException>(err);

                        //Asynchronous task based
                        err = Assert.Catch(() => serviceClient.Fail());
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf<AggregateException>(err);

                        //Asynchronous IAsyncResult based , awaiting with Task
                        err = Assert.Catch(async () => await Task.Factory.FromAsync(serviceClient.BeginFail, serviceClient.EndFail, null));
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf<AggregateException>(err);

                        //Timeout exceptions
                        var factoryWithTimeout = new ServiceClientFactory(client);
                        var serviceClientWithTimeout = factoryWithTimeout.CreateServiceClient<ITestService>(50); //50ms

                        Assert.Throws<TimeoutException>(async () => await serviceClientWithTimeout.ReplyAfter(1000));
                    }
                }
            }
        }
        public async void TestSendAndReceiveExceptions()
        {
            var resolver = new DependencyResolver();

            using (var broker = new ZmqBroker(this.zmqContext, ClientInboundAddress, ClientOutboundAddress, ServerInboundAddress, ServerOutboundAddress))
            {
                broker.Listen();

                using (var server = new ZmqServer(this.zmqContext, ServerInboundAddress, ServerOutboundAddress, new ServiceFactory(resolver)))
                {
                    server.Listen();

                    using (var client = new ZmqClient(this.zmqContext, ClientInboundAddress, ClientOutboundAddress))
                    {
                        var clientFactory = new ServiceClientFactory(client);

                        var serviceClient = clientFactory.CreateServiceClient <ITestService>();

                        //Synchronous
                        var err = Assert.Catch(async() => await serviceClient.FailAsync());
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf <AggregateException>(err);

                        //Asynchronous task based
                        err = Assert.Catch(() => serviceClient.Fail());
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf <AggregateException>(err);

                        //Asynchronous IAsyncResult based , awaiting with Task
                        err = Assert.Catch(async() => await Task.Factory.FromAsync(serviceClient.BeginFail, serviceClient.EndFail, null));
                        Assert.IsNotNull(err);
                        Assert.IsNotInstanceOf <AggregateException>(err);

                        //Timeout exceptions
                        var factoryWithTimeout       = new ServiceClientFactory(client);
                        var serviceClientWithTimeout = factoryWithTimeout.CreateServiceClient <ITestService>(50); //50ms

                        Assert.Throws <TimeoutException>(async() => await serviceClientWithTimeout.ReplyAfter(1000));
                    }
                }
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            var serverInboundAddr  = "tcp://localhost:8003";
            var serverOutboundAddr = "tcp://localhost:8004";

            var builder = new ContainerBuilder();

            builder.RegisterType <LogInterceptor>().AsSelf().As <IInterceptor>();

            builder.RegisterType <CatalogService>().As <ICatalogService>()
            .EnableInterfaceInterceptors()
            .InterceptedBy(typeof(LogInterceptor));;
            builder.RegisterType <FooService>().As <IFooService>()
            .EnableInterfaceInterceptors()
            .InterceptedBy(typeof(LogInterceptor));

            builder.RegisterType <AutofacDependencyResolver>().As <IDependencyResolver>();
            builder.RegisterType <ServiceFactory>().As <IServiceFactory>();

            var container = builder.Build();

            ZmqServer server = null;

            try
            {
                server = new ZmqServer(new ZMQ.Context(), serverInboundAddr, serverOutboundAddr, container.Resolve <IServiceFactory>());
                server.Listen();

                Console.WriteLine("Press enter to quit");
                Console.ReadLine();
            }
            finally
            {
                if (server != null)
                {
                    server.Dispose();
                }
            }
        }