예제 #1
0
        public bool Init(RegisterServiceLocation registerServiceLocation)
        {
            //获取服务地址
            var netclient = NetClientPool.CreateClient(this.ServiceTransaction.ProxyAddress, this.ServiceTransaction.GatewayAddress, this.ServiceTransaction.GatewayClientCertificate);

            netclient.ReadTimeout = this.ServiceTransaction.Timeout;
            try
            {
                netclient.WriteServiceData(new GatewayCommand()
                {
                    Type    = CommandType.GetServiceProvider,
                    Header  = ServiceTransaction.GetCommandHeader(),
                    Content = new GetServiceProviderRequest
                    {
                        ServiceName = _serviceName,
                        Arg         = _arg
                    }.ToJsonString()
                });
                var serviceLocation = netclient.ReadServiceObject <RegisterServiceLocation>();
                if (registerServiceLocation != null)
                {
                    serviceLocation.Host           = registerServiceLocation.Host;
                    serviceLocation.Port           = registerServiceLocation.Port;
                    serviceLocation.ServiceAddress = registerServiceLocation.ServiceAddress;
                }

                if (serviceLocation.Host == "not master")
                {
                    throw new MissMasterGatewayException("");
                }

                if (string.IsNullOrEmpty(ServiceTransaction.TransactionId))
                {
                    ServiceTransaction.TransactionId = serviceLocation.TransactionId;
                }

                if (serviceLocation.Port == 0)
                {
                    return(false);
                }
                _serviceLocation = serviceLocation;

                NetClientPool.AddClientToPool(netclient);
            }
            catch (SocketException ex)
            {
                netclient.Dispose();
                throw new MissMasterGatewayException(ex.Message);
            }
            catch (Exception)
            {
                netclient.Dispose();
                throw;
            }


            return(true);
        }
예제 #2
0
        public T Invoke <T>(string method, JMSClient tran, params object[] parameters)
        {
            if (tran == null)
            {
                throw new ArgumentNullException("tran");
            }
            this.InvokingInfo.MethodName = method;
            this.InvokingInfo.Parameters = parameters;

            var netclient = NetClientPool.CreateClient(tran.ProxyAddress, this.InvokingInfo.ServiceLocation.Host, this.InvokingInfo.ServiceLocation.Port, tran.ServiceClientCertificate);

            try
            {
                var cmd = new InvokeCommand()
                {
                    Header     = tran.GetCommandHeader(),
                    Service    = this.InvokingInfo.ServiceName,
                    Method     = method,
                    Parameters = parameters.Length == 0 ? null :
                                 parameters.GetStringArrayParameters()
                };


                netclient.WriteServiceData(cmd);
                var result = netclient.ReadServiceObject <InvokeResult <T> >();
                if (result.Success == false)
                {
                    throw new RemoteException(tran.TransactionId, result.Error);
                }
                NetClient = netclient;

                if (result.SupportTransaction)
                {
                    tran.AddConnect(this);
                }
                else
                {
                    NetClientPool.AddClientToPool(netclient);
                }

                return(result.Data);
            }
            catch (ConvertException ex)
            {
                InvokeResult <string> otherObj = null;
                try
                {
                    otherObj = ex.Source.FromJson <InvokeResult <string> >();
                }
                catch
                {
                }

                if (otherObj != null)
                {
                    throw new ConvertException(otherObj.Data, $"无法将{otherObj.Data}实例化为{typeof(T).FullName}");
                }

                throw ex;
            }
            catch (Exception)
            {
                netclient.Dispose();
                throw;
            }
        }