コード例 #1
0
        public void TestProxyRegisterMethod()
        {
            JsonRpcServiceProxyWrapper wrapper = new JsonRpcServiceProxyWrapper();

            wrapper.RegisterMethod("m1", req => { return(Task.FromResult <JsonRpcResponse>(new JsonRpcResponse())); });
            Assert.IsTrue(wrapper.HasMethod("m1"));
        }
コード例 #2
0
        public void TestProxyExecuteMethodRequestIsNull()
        {
            JsonRpcServiceProxyWrapper wrapper = new JsonRpcServiceProxyWrapper();

            wrapper.RegisterMethod("m1", req => { return(Task.FromResult <JsonRpcResponse>(new JsonRpcResponse
                {
                    Id = Guid.NewGuid().ToString(),
                    Result = "Result"
                })); });
            var resp = wrapper.ServeAsync(null).GetAwaiter().GetResult();

            Assert.IsNotNull(resp);
            Assert.IsNotNull(resp.Error);
            Assert.AreEqual((int)ErrorCode.InvalidRequest, resp.Error.Code);
        }
        /// <summary>
        /// Initializes a new base instance of the <see cref="NatsSubscriptionBackgroundService" /> class.
        /// </summary>
        /// <param name="natsUrl">The nats URL.</param>
        /// <param name="group">The queue group.</param>
        /// <param name="logger">The logger.</param>
        /// <param name="registerMethods">The registrarion action.</param>
        protected JsonRpcSubscriptionBackgroundService(string natsUrl, string group,
                                                       ILoggerProvider logger, Action <JsonRpcServiceProxyWrapper> registerMethods)
        {
            _logger  = logger.CreateLogger(MethodBase.GetCurrentMethod().DeclaringType.FullName);
            _natsUrl = natsUrl;

            _wrapper = new JsonRpcServiceProxyWrapper();
            registerMethods(_wrapper);

            foreach (string channel in _wrapper.Methods)
            {
                _logger?.Log(LogLevel.Information, $"Create subscription {channel}");
                _subscriptions.Add(new JsonRpcSubscription(channel, group, _wrapper, _logger));
            }
        }
コード例 #4
0
        public void TestProxyExecuteMethodSync()
        {
            JsonRpcServiceProxyWrapper wrapper = new JsonRpcServiceProxyWrapper();

            wrapper.RegisterMethod("m1", req => { return(Task.FromResult <JsonRpcResponse>(new JsonRpcResponse
                {
                    Id = Guid.NewGuid().ToString(),
                    Result = "Result"
                })); });
            var resp = wrapper.Serve(new JsonRpcRequest
            {
                Method = "m1"
            });

            Assert.IsNotNull(resp);
            Assert.AreEqual("Result", resp.Result);
        }
コード例 #5
0
        public void TestProxyExecuteMethodExceptionAsync()
        {
            JsonRpcServiceProxyWrapper wrapper = new JsonRpcServiceProxyWrapper();

            wrapper.RegisterMethod("m1", req =>
            {
                throw new Exception("Error");
            });
            var resp = wrapper.ServeAsync(new JsonRpcRequest
            {
                Method = "m1"
            }).GetAwaiter().GetResult();

            Assert.IsNotNull(resp);
            Assert.IsNotNull(resp.Error);
            Assert.AreEqual((int)ErrorCode.ServerError, resp.Error.Code);
        }