コード例 #1
0
ファイル: Program.cs プロジェクト: knightfall/writeasync
        private static async Task RunClientAsync(Uri address, CancellationToken token)
        {
            ClientEventSource eventSource = ClientEventSource.Instance;

            NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
            binding.OpenTimeout = TimeSpan.FromSeconds(1.0d);
            binding.SendTimeout = TimeSpan.FromSeconds(1.0d);
            binding.ReceiveTimeout = TimeSpan.FromSeconds(1.0d);
            binding.CloseTimeout = TimeSpan.FromSeconds(1.0d);

            CalculatorChannelFactory factory = new CalculatorChannelFactory(binding, new EndpointAddress(address), eventSource);
            await factory.OpenAsync();

            ConnectionManager<ICalculatorClientAsync> connectionManager = new ConnectionManager<ICalculatorClientAsync>(factory);

            using (ProxyInvoker<ICalculatorClientAsync> proxy = new ProxyInvoker<ICalculatorClientAsync>(connectionManager))
            {
                Random random = new Random();

                while (!token.IsCancellationRequested)
                {
                    try
                    {
                        await proxy.InvokeAsync(c => InvokeRandomAsync(random, c));
                    }
                    catch (Exception)
                    {
                    }

                    await Task.Delay(TimeSpan.FromMilliseconds(250.0d));
                }
            }

            await factory.CloseAsync();
        }
コード例 #2
0
        public void Dispose_invalidates_connection()
        {
            ConnectionManagerStub   connectionManagerStub = new ConnectionManagerStub();
            ProxyInvoker <IMyProxy> invoker = new ProxyInvoker <IMyProxy>(connectionManagerStub);

            invoker.Dispose();

            Assert.Equal(1, connectionManagerStub.InvalidateCount);
        }
コード例 #3
0
        public void Invoke_connects_and_invokes_method_with_result()
        {
            ConnectionManagerStub connectionManagerStub = new ConnectionManagerStub();

            connectionManagerStub.Proxy = new MyProxyStub();
            ProxyInvoker <IMyProxy> invoker = new ProxyInvoker <IMyProxy>(connectionManagerStub);
            object input = new object();

            Task <object> task = invoker.InvokeAsync(p => p.DoAsync(input));

            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            Assert.Same(input, task.Result);
            Assert.Equal(1, connectionManagerStub.ConnectCount);
        }
コード例 #4
0
        public void Invoke_connects_and_invokes_method_with_exception_and_invalidates()
        {
            ConnectionManagerStub connectionManagerStub = new ConnectionManagerStub();
            MyProxyStub           proxyStub             = new MyProxyStub();

            connectionManagerStub.Proxy = proxyStub;
            ProxyInvoker <IMyProxy> invoker = new ProxyInvoker <IMyProxy>(connectionManagerStub);

            InvalidTimeZoneException expectedException = new InvalidTimeZoneException("Expected.");

            proxyStub.Exception = expectedException;
            Task <object> task = invoker.InvokeAsync(p => p.DoAsync(new object()));

            Assert.Equal(TaskStatus.Faulted, task.Status);
            AggregateException ae = Assert.IsType <AggregateException>(task.Exception);

            Assert.Equal(1, ae.InnerExceptions.Count);
            Assert.Same(expectedException, ae.InnerExceptions[0]);
            Assert.Equal(1, connectionManagerStub.InvalidateCount);
        }
コード例 #5
0
        public static I Create(I instance)
        {
            var proxy = new ProxyInvoker(instance).GetTransparentProxy();

            return(proxy as I);
        }