示例#1
0
        public void bindAndCall()
        {
            var       loggerMock    = AutoMockContainer.GetMock <ILogger <RpcServer> >();
            var       aclLoaderMock = AutoMockContainer.GetMock <INetworkAclLoader>();
            RpcServer rpcServer     = new RpcServer(GetRpcConfig(), loggerMock.Object, aclLoaderMock.Object);

            // binding

            rpcServer.BindOperation(null, "checkDifferent", new Func <int, string, bool>((a, b) =>
            {
                int.TryParse(b, out var bInt);
                return(!bInt.Equals(a));
            }));

            rpcServer.BindOperation("math", "checkEquals", new Func <int, string, bool>((a, b) =>
            {
                int.TryParse(b, out var bInt);
                return(bInt.Equals(a));
            }));

            rpcServer.BindController <MathController>();
            rpcServer.BindController(typeof(Foo));

            // inject

            rpcServer.InjectSpecialParameter(context => new RandomObjectForTest()
            {
                RandomProp = "Hello"
            });

            // calling

            var resp1 = (bool)rpcServer.CallOperation(null, null, "checkDifferent", 2, "2");

            Assert.IsFalse(resp1);

            var resp2 = (bool)rpcServer.CallOperation(null, "math", "checkEquals", new Dictionary <string, object>()
            {
                { "a", 2 },
                { "b", "2" }
            });

            Assert.IsTrue(resp2);

            var resp3 = (int)rpcServer.CallOperation(null, "math", "sum", 2, 3);

            Assert.AreEqual(resp3, 5);

            var resp4 = (int)rpcServer.CallOperation(null, "math", "sub", 3, 1);

            Assert.AreEqual(resp4, 2);

            var resp5 = (string)rpcServer.CallOperation(null, "Foo", "Bar");

            Assert.AreEqual(resp5, "foo bar :)");

            var resp6 = (string)rpcServer.CallOperation(null, "Foo", "PrettyMessage", "how are you?");

            Assert.AreEqual(resp6, "Hello, how are you?");
        }