示例#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");
            }
        }
示例#3
0
 private void BuildGenericManager<T>(){
 
 if (systemLogger == null)
             {
                 LoggerSetup.Configure(Constants.ModuleNameForLogging);
                 systemLogger = new Log4NetLoggingAdapter(LogManager.GetLogger(typeof(RequestManager)));
             }
             proxy = WcfClientProxy.Create<T>(c =>
             {
                 c.SetEndpoint("BasicHttpBinding_IRequestService");
 
                 c.OnCallBegin += (sender, args) => { };
 
                 c.OnBeforeInvoke += (sender, args) =>
                 {
                     systemLogger.Write(string.Format("{0}.{1} called with parameters: {2}", args.ServiceType.Name, args.InvokeInfo.MethodName, string.Join(", ", args.InvokeInfo.Parameters)), Core.EventSeverity.Verbose);
                 };
 
                 c.OnAfterInvoke += (sender, args) =>
                 {
                     systemLogger.Write(string.Format("{0}.{1} returned value: {2}", args.ServiceType.Name, args.InvokeInfo.MethodName, args.InvokeInfo.ReturnValue), Core.EventSeverity.Verbose);
                 };
 
                 c.OnCallSuccess += (sender, args) =>
                 {
                     systemLogger.Write(string.Format("{0}.{1} completed successfully", args.ServiceType.Name, args.InvokeInfo.MethodName));
                 };
 
                 c.OnException += (sender, args) =>
                 {
                     systemLogger.Write(string.Format("Exception during service call to {0}.{1}: {2}", args.ServiceType.Name, args.InvokeInfo.MethodName, args.Exception.Message), args.Exception, Core.EventSeverity.Error);
                 };
             });
         }
示例#4
0
        public async Task CallAsyncTest()
        {
            var client = WcfClientProxy.CreateProxy <ITestSvc>();
            var text   = await client.GetMessageAsync("zz");

            Assert.AreEqual("hello zz", text);
        }
示例#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");
            }
        }
示例#6
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));
            }
        }
示例#7
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");
            }
        }
示例#8
0
        public void CallTest()
        {
            var client = WcfClientProxy.CreateProxy <ITestSvc>();
            var text   = client.GetMessageSync("zz");

            Assert.AreEqual("hello zz", text);
        }
示例#9
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");
            }
        }
示例#10
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"));
        }
示例#11
0
        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)));
        }
    public override T CreateChannel(EndpointAddress address, Uri via)
    {
        // This is where the magic happens. We don't really return a channel here;
        // we return WcfClientProxy.GetTransparentProxy(). That class will now
        // have the chance to intercept calls to the service.
        this.Endpoint.Address = address;
        var proxy = new WcfClientProxy <T>(this);

        return(proxy.GetTransparentProxy() as T);
    }
        public void UsingEndpointConfigurationName_BuildServiceEndpointForChannelFactory_UsingDynamicallyGeneratedAsyncInterface()
        {
            var proxy = WcfClientProxy.CreateAsyncProxy <ITestService>("ITestService");

            var exception = Assert.ThrowsAsync <EndpointNotFoundException>(
                () => proxy.CallAsync(m => m.IntMethod()),
                message: "Expected EndpointNotFoundException (since ITestService address is not running)");

            Assert.That(exception.Message, Does.StartWith("There was no endpoint listening at "));
        }
        public static IAsyncProxy <TServiceInterface> StartHostAndAsyncProxy <TServiceInterface>(this TServiceInterface serviceInstance, Action <IRetryingProxyConfigurator> configurator = null)
            where TServiceInterface : class
        {
            var host = serviceInstance.StartHost();

            return(WcfClientProxy.CreateAsyncProxy <TServiceInterface>(c =>
            {
                c.SetEndpoint(host.Binding, host.EndpointAddress);
                configurator?.Invoke(c);
            }));
        }
示例#15
0
        public void CallIn100Test()
        {
            var client1 = WcfClientProxy.CreateProxy <ITestSvc>();

            for (int i = 0; i < 100; i++)
            {
                var text = client1.GetMessageSync("zz");
                Assert.AreEqual("hello zz", text);
            }
            for (int i = 0; i < 100; i++)
            {
                var client2 = WcfClientProxy.CreateProxy <ITestSvc>();
                var text    = client2.GetMessageSync("zz");
                Assert.AreEqual("hello zz", text);
            }
        }
示例#16
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"));
        }
示例#17
0
        public async Task CallInAsync100Test()
        {
            var client1 = WcfClientProxy.CreateProxy <ITestSvc>();

            for (int i = 0; i < 100; i++)
            {
                var text = await client1.GetMessageAsync("zz");

                Assert.AreEqual("hello zz", text);
            }
            for (int i = 0; i < 100; i++)
            {
                var client2 = WcfClientProxy.CreateProxy <ITestSvc>();
                var text    = await client2.GetMessageAsync("zz");

                Assert.AreEqual("hello zz", text);
            }
        }
示例#18
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>());
        }
示例#19
0
        public void CallInExceptionTest()
        {
            bool isFault = false;

            try
            {
                var client = WcfClientProxy.CreateProxy <ITestSvc>();
                var text   = client.GetMessageSync("fault");
            }
            catch (FaultException <MyFault> ex) when(ex.Detail.Text == "fault")
            {
                isFault = true;
            }
            catch (Exception ex)
            {
            }
            Assert.IsTrue(isFault);
        }
示例#20
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"));
        }
        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");
            }
        }
示例#22
0
        private static void RunClientTB1()
        {
            bool   bContinue = true;
            string command   = "";

            IApplicationInterfaceService proxy = WcfClientProxy.Create <IApplicationInterfaceService>(c =>
            {
                c.SetEndpoint("netNamedPipeBinding_SvpApi");
            });

            while (bContinue)
            {
                Console.WriteLine("コマンドを入力してください");
                Console.Write("> ");
                command = Console.ReadLine();

                switch (command)
                {
                case "login":
                    proxy.Login();
                    Console.WriteLine("Loginコマンドを実行しました");
                    break;

                case "cc1":
                    var newCategory = new SVP.CIL.Domain.Category
                    {
                        Name    = "新規カテゴリ",
                        Comment = "コメントです"
                    };
                    var rsp_cc1 = proxy.CategoryCrud(new SVP.CIL.Request.RequestCategoryCrud
                    {
                        Crud   = SVP.CIL.Request.CrudType.CREATE,
                        Target = newCategory
                    });
                    Console.WriteLine("カテゴリ({0})を作成しました。", rsp_cc1.Data.Id);
                    break;

                case "cd1":
                    Console.Write("削除ID:");
                    string deleteId = Console.ReadLine();
                    var    rsp_cd1  = proxy.CategoryCrud(new SVP.CIL.Request.RequestCategoryCrud
                    {
                        Crud   = SVP.CIL.Request.CrudType.DELETE,
                        Target = new SVP.CIL.Domain.Category {
                            Id = long.Parse(deleteId)
                        }
                    });
                    if (rsp_cd1.Success)
                    {
                        Console.WriteLine("削除しました");
                    }
                    else
                    {
                        Console.WriteLine("削除できませんでした");
                    }
                    break;

                case "cu1":
                    var rsp_cu1_1 = proxy.CategoryCrud(new SVP.CIL.Request.RequestCategoryCrud
                    {
                        Crud   = SVP.CIL.Request.CrudType.READ,
                        Target = new SVP.CIL.Domain.Category {
                            Id = 2L
                        }
                    });

                    var updatedDomainCategory = rsp_cu1_1.Data;
                    Console.Write("新しいタイトル名:");
                    string newTitle = Console.ReadLine();
                    updatedDomainCategory.Name = newTitle;
                    var rsp_cu1 = proxy.CategoryCrud(new SVP.CIL.Request.RequestCategoryCrud
                    {
                        Crud   = SVP.CIL.Request.CrudType.UPDATE,
                        Target = updatedDomainCategory
                    });
                    break;

                case "clist1":
                    var rsp_clist1 = proxy.CategoryLoadList(new SVP.CIL.Request.RequestCategoryLoadList
                    {
                        ParentTarget = new SVP.CIL.Domain.Category {
                            Id = 10L
                        }
                    });

                    if (rsp_clist1.Success)
                    {
                        foreach (var item in rsp_clist1.Datas)
                        {
                            Console.WriteLine("ID:{0} Name:{1}", item.Id, item.Name);
                        }
                    }
                    else
                    {
                        Console.WriteLine("API実行に失敗しました");
                    }
                    break;

                case "e":
                    bContinue = false;
                    break;
                }
            }
        }
示例#23
0
文件: ServiceHelper.cs 项目: wpmyj/LC
        public async static Task <IAsyncProxy <IRightModelService> > GetRightService()
        {
            IAsyncProxy <IRightModelService> asyncProxy = await Task.Run(() => WcfClientProxy.CreateAsyncProxy <IRightModelService>(c => c.SetEndpoint(CreateBinding(), new EndpointAddress(ClientHelper.GetEndpointAddress(ServiceCode.SysRightService)))));

            return(asyncProxy);
        }