Beispiel #1
0
        public async Task ServiceContractDefinedAsyncMethod_WithNoReturnValue()
        {
            var resetEvent = new AutoResetEvent(false);

            var mockService = new Mock <IAsyncTestInterface>();

            mockService
            .Setup(m => m.VoidMethodDefinedAsync("test"))
            .Returns(Task.FromResult(true))
            .Callback(() =>
            {
                Console.WriteLine("Callback thread: " + Thread.CurrentThread.ManagedThreadId);
                resetEvent.Set();
            });

            var serviceHost = InProcTestFactory.CreateHost <IAsyncTestInterface>(new AsyncTestInterfaceImpl(mockService));

            var proxy = WcfClientProxy.Create <IAsyncTestInterface>(c =>
                                                                    c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress));

            Console.WriteLine("Caller thread: " + Thread.CurrentThread.ManagedThreadId);

            await proxy.VoidMethodDefinedAsync("test");

            Console.WriteLine("Contination thread: " + Thread.CurrentThread.ManagedThreadId);

            if (!resetEvent.WaitOne(TimeSpan.FromSeconds(2)))
            {
                Assert.Fail("Callback never called");
            }
        }
        public void DuplexService_WithInstanceContext_TriggersCallback()
        {
            var resetEvent = new AutoResetEvent(false);

            var serviceHost = InProcTestFactory.CreateHost <IDuplexService>(new DuplexService());

            var callback = Substitute.For <IDuplexServiceCallback>();

            callback
            .TestCallback(Arg.Any <string>())
            .Returns(m => m.Arg <string>())
            .AndDoes(_ => resetEvent.Set());

            InstanceContext <IDuplexServiceCallback> ctx = new InstanceContext <IDuplexServiceCallback>(callback);
            var proxy = WcfClientProxy.Create <IDuplexService>(c =>
            {
                c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress, ctx);
            });

            proxy.Test("test");

            if (!resetEvent.WaitOne(TimeSpan.FromSeconds(10)))
            {
                Assert.Fail("Callback not entered");
            }
        }
Beispiel #3
0
        public async Task CallAsync_MethodWithReturnValue()
        {
            var mockService = new Mock <ITestService>();

            mockService
            .SetupSequence(m => m.TestMethod("good"))
            .Returns("BAD")
            .Returns("OK");

            mockService.Setup(m => m.TestMethod("second", "two")).Returns("2");

            var serviceHost = InProcTestFactory.CreateHost <ITestService>(new TestServiceImpl(mockService));

            var proxy = WcfClientProxy.CreateAsyncProxy <ITestService>(c =>
            {
                c.MaximumRetries(1);
                c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress);
                c.RetryOnResponse <string>(s => s == "BAD");
            });

            Console.WriteLine("Caller thread: " + Thread.CurrentThread.ManagedThreadId);

            string result = await proxy.CallAsync(m => m.TestMethod("good"));

            string result2 = await proxy.CallAsync(m => m.TestMethod("second", "two"));

            Console.WriteLine("Continuation thread: " + Thread.CurrentThread.ManagedThreadId);

            Assert.That(result, Is.EqualTo("OK"));
            Assert.That(result2, Is.EqualTo("2"));
        }
Beispiel #4
0
        public void CallAsync_MultipleConcurrentCalls()
        {
            int iterations = 20;

            var mockService = new Mock <ITestService2>();

            mockService
            .Setup(m => m.TestMethod(It.IsAny <string>()))
            .Returns((string s) => "Echo: " + s)
            .Callback((string s) => Console.WriteLine("Callback: " + s));

            var serviceHost = InProcTestFactory.CreateHost <ITestService2>(new TestService2Impl(mockService));

            var proxy = WcfClientProxy.CreateAsyncProxy <ITestService2>(c =>
                                                                        c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress));

            var tasks = new List <Task>();

            for (int i = 0; i < iterations; i++)
            {
                int i1 = i;
                tasks.Add(proxy.CallAsync(m => m.TestMethod(i1.ToString())));
                Console.WriteLine("Queued task: " + i);
            }

            Console.WriteLine("Waiting tasks...");

            Task.WaitAll(tasks.ToArray());

            for (int i = 0; i < iterations; i++)
            {
                string result = ((Task <string>)tasks[i]).Result;
                Assert.That(result, Is.EqualTo("Echo: " + i));
            }
        }
Beispiel #5
0
        public async Task CallAsync_OneWayOperation()
        {
            var resetEvent = new AutoResetEvent(false);

            var mockService = new Mock <ITestService>();

            mockService
            .Setup(m => m.OneWay(It.IsAny <string>()))
            .Callback((string input) =>
            {
                Assert.That(input, Is.EqualTo("test"));

                Console.WriteLine("Callback thread: " + Thread.CurrentThread.ManagedThreadId);
                resetEvent.Set();
            });

            var serviceHost = InProcTestFactory.CreateHost <ITestService>(new TestServiceImpl(mockService));

            var proxy = WcfClientProxy.CreateAsyncProxy <ITestService>(c =>
                                                                       c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress));

            await proxy.CallAsync(m => m.OneWay("test"));

            Console.WriteLine("Continuation thread: " + Thread.CurrentThread.ManagedThreadId);

            if (!resetEvent.WaitOne(TimeSpan.FromSeconds(10)))
            {
                Assert.Fail("Callback not entered");
            }
        }
Beispiel #6
0
        public async Task CallAsync_VoidMethod()
        {
            var resetEvent = new AutoResetEvent(false);

            var mockService = new Mock <ITestService>();

            mockService
            .Setup(m => m.VoidMethod("good"))
            .Callback(() =>
            {
                Console.WriteLine("Callback thread: " + Thread.CurrentThread.ManagedThreadId);
                resetEvent.Set();
            });

            var serviceHost = InProcTestFactory.CreateHost <ITestService>(new TestServiceImpl(mockService));

            var proxy = WcfClientProxy.CreateAsyncProxy <ITestService>(c =>
                                                                       c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress));

            Console.WriteLine("Caller thread: " + Thread.CurrentThread.ManagedThreadId);

            await proxy.CallAsync(m => m.VoidMethod("good"));

            Console.WriteLine("Continuation thread: " + Thread.CurrentThread.ManagedThreadId);

            if (!resetEvent.WaitOne(TimeSpan.FromSeconds(2)))
            {
                Assert.Fail("Callback never triggered");
            }
        }
Beispiel #7
0
        public void DuplexService_OneWayOperation_TriggersCallback()
        {
            var resetEvent = new AutoResetEvent(false);

            var serviceHost = InProcTestFactory.CreateHost <IDuplexService>(new DuplexService());

            var callback = new Mock <IDuplexServiceCallback>();

            callback
            .Setup(m => m.OneWayCallback(It.IsAny <string>()))
            .Callback((string input) =>
            {
                resetEvent.Set();
            });

            var proxy = WcfClientProxy.Create <IDuplexService>(c =>
            {
                c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress, callback.Object);
            });

            proxy.OneWay("test");

            if (!resetEvent.WaitOne(TimeSpan.FromSeconds(10)))
            {
                Assert.Fail("Callback not entered");
            }
        }
        public void Test()
        {
            var service     = Substitute.For <ITestService>();
            var serviceHost = InProcTestFactory.CreateHost <ITestService>(service);

            Assert.DoesNotThrow(() => WcfClientProxy.Create <IIssue25Service>(
                                    c => c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress)));
        }
Beispiel #9
0
        public void FaultHappens_WithDefaultChannelProxy()
        {
            var mockService = new Mock <ITestService>();

            mockService.Setup(m => m.TestMethod("good")).Returns("OK");
            mockService.Setup(m => m.TestMethod("bad")).Throws <Exception>();

            var serviceHost = InProcTestFactory.CreateHost <ITestService>(new TestServiceImpl(mockService));

            var proxy = new ChannelFactory <ITestService>(serviceHost.Binding, serviceHost.EndpointAddress).CreateChannel();

            // Will fault the channel
            Assert.That(() => proxy.TestMethod("bad"), Throws.Exception);
            Assert.That(() => proxy.TestMethod("good"), Throws.Exception.TypeOf <CommunicationObjectFaultedException>());
        }
Beispiel #10
0
        public void CallAsync_CanCallIntoSyncProxy()
        {
            var mockService = new Mock <ITestService>();

            mockService
            .Setup(m => m.TestMethod("good"))
            .Returns("OK");

            var serviceHost = InProcTestFactory.CreateHost <ITestService>(new TestServiceImpl(mockService));

            var proxy = WcfClientProxy.CreateAsyncProxy <ITestService>(c =>
                                                                       c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress));

            string result = proxy.Client.TestMethod("good");

            Assert.That(result, Is.EqualTo("OK"));
        }
Beispiel #11
0
        public void CallAsync_CallingMethodWithByRefParams_ThrowsNotSupportedException()
        {
            var mockService = new Mock <IOutParamTestService>();

            byte[] expectedOutParam = { 0x01 };
            mockService
            .Setup(m => m.SingleOutParam(out expectedOutParam))
            .Returns(100);

            var serviceHost = InProcTestFactory.CreateHost <IOutParamTestService>(new OutParamTestServiceImpl(mockService));

            var proxy = WcfClientProxy.CreateAsyncProxy <IOutParamTestService>(c =>
                                                                               c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress));

            byte[] resultingOutParam;

            Assert.That(() => proxy.CallAsync(m => m.SingleOutParam(out resultingOutParam)), Throws.TypeOf <NotSupportedException>());
        }
        public void DuplexService_OneWayOperation_TriggersCallback()
        {
            var resetEvent = new AutoResetEvent(false);

            var serviceHost = InProcTestFactory.CreateHost <IDuplexService>(new DuplexService());

            var callback = Substitute.For <IDuplexServiceCallback>();

            callback
            .When(m => m.OneWayCallback(Arg.Any <string>()))
            .Do(_ => resetEvent.Set());

            var proxy = WcfClientProxy.Create <IDuplexService>(c =>
            {
                c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress, callback);
            });

            proxy.OneWay("test");

            if (!resetEvent.WaitOne(TimeSpan.FromSeconds(10)))
            {
                Assert.Fail("Callback not entered");
            }
        }
Beispiel #13
0
        public async Task CallAsync_MethodWithReturnValue2()
        {
            var request = new Request()
            {
                RequestMessage = "test"
            };

            var mockService = new Mock <ITestService>();

            mockService
            .SetupSequence(m => m.TestMethodComplex(It.IsAny <Request>()))
            .Returns(new Response {
                ResponseMessage = "test", StatusCode = 1
            })
            .Returns(new Response {
                ResponseMessage = "test", StatusCode = 0
            });

            var serviceHost = InProcTestFactory.CreateHost <ITestService>(new TestServiceImpl(mockService));

            var proxy = WcfClientProxy.CreateAsyncProxy <ITestService>(c =>
            {
                c.MaximumRetries(1);
                c.SetEndpoint(serviceHost.Binding, serviceHost.EndpointAddress);
                c.RetryOnResponse <Response>(s => s.StatusCode == 1);
            });

            Console.WriteLine("Caller thread: " + Thread.CurrentThread.ManagedThreadId);

            var result = await proxy.CallAsync(m => m.TestMethodComplex(request));

            Console.WriteLine("Continuation thread: " + Thread.CurrentThread.ManagedThreadId);

            Assert.That(result.StatusCode, Is.EqualTo(0));
            Assert.That(result.ResponseMessage, Is.EqualTo("test"));
        }