Exemplo n.º 1
0
        public void TestEnumSerializing()
        {
            MemoryStream   stream = new MemoryStream();
            CHessianOutput out1   = new CHessianOutput(stream);

            TestDto obj = new TestDto();

            out1.StartReply();
            out1.WriteObject(obj);
            out1.CompleteReply();
            byte[] data = stream.ToArray();

            File.WriteAllBytes(@"C:\Users\disap\Desktop\Projects\hessianscharp\HessianTestProject\test.txt", data);
            CHessianInput input  = new CHessianInput(new MemoryStream(data));
            var           result = input.ReadReply(typeof(TestDto));

            Assert.IsTrue(true);
        }
Exemplo n.º 2
0
        public async Task <ReturnT> InvokeService(MethodBase method, List <string> paramTypes, params object[] parameters)
        {
            var methodName = method.Name.Substring(0, 1).ToLower() + method.Name.Substring(1);
            var request    = new RpcRequest()
            {
                createMillisTime = DateTimeExtensions.CurrentTimeMillis(),
                accessToken      = _executorOption.Value.AccessToken,
                className        = "com.xxl.job.core.biz.AdminBiz",
                methodName       = methodName,
                parameterTypes   = new ArrayList(paramTypes.Select(item => new Class(item)).ToArray()),
                parameters       = new ArrayList(parameters)
            };

            var ms         = new MemoryStream();
            var serializer = new CHessianOutput(ms);

            serializer.WriteObject(request);
            var responseBody = ms.ToArray();

            int triedTimes = 0;

            while (triedTimes++ < _addresses.Count)
            {
                var item = _addresses[_currentAdminIndex];
                _currentAdminIndex = (_currentAdminIndex + 1) % _addresses.Count;
                if (!item.CheckAccessable())
                {
                    continue;
                }

                Stream responseStream;
                try
                {
                    var content = new ByteArrayContent(responseBody);
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    var responseMessage = await _client.PostAsync(item.RequestUri, content);

                    responseMessage.EnsureSuccessStatusCode();
                    responseStream = await responseMessage.Content.ReadAsStreamAsync();

                    item.Reset();
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "request admin error.");
                    item.SetFail();
                    continue;
                }

                var rpcResponse = (RpcResponse) new CHessianInput(responseStream).ReadObject();
                if (rpcResponse == null)
                {
                    throw new Exception("xxl-rpc response not found.");
                }
                if (rpcResponse.IsError)
                {
                    throw new Exception(rpcResponse.error);
                }
                else
                {
                    return(rpcResponse.result as ReturnT);
                }
            }

            throw new Exception("xxl-rpc server address not accessable.");
        }