// [InvokeService]
        // [MethodImpl(MethodImplOptions.NoInlining)]
        public static T InvokeNetTcp <R, T>(string hostName, int port, string endPoint, string functionName, params object[] args) where R : ICommunicationObject, IRemoteInvoke
        {
            // MethodBase method = MethodBase.GetCurrentMethod();
            // InvokeService serviceAttrs = (InvokeService)method.GetCustomAttributes(typeof(InvokeService), true)[0];
            // string hostName = serviceAttrs.HostName;
            // int port = serviceAttrs.Port;
            // string endPoint = serviceAttrs.EndPoint;

            T result;

            // using (var cc = NetTcpWcfClient<R>.Create($@"net.tcp://{cache.ServerHostName}:{cache.ServicePort}/{endPoint}/"))
            // {
            //   Type cacheServiceType = cc.Service.GetType();
            //   MethodInfo functionToInvoke = cacheServiceType.GetMethod(functionName);
            //   var functionResult = functionToInvoke.Invoke(cc.Service, BindingFlags.InvokeMethod, null, args, null);
            //   result = functionResult;
            // }

            using (var cc = NetTcpWcfClient <R> .Create($@"net.tcp://{hostName}:{port}/{endPoint}/"))
            {
                result = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(cc.Service.RemoteInvoke(functionName, args));
            }

            return(result);
        }
        // [InvokeService]
        // [MethodImpl(MethodImplOptions.NoInlining)]
        public static T InvokeHttp <R, T>(string schema, string hostName, int port, string endPoint, string functionName, params object[] args) where R : ICommunicationObject, IRemoteInvoke
        {
            T result;

            using (var cc = NetTcpWcfClient <R> .Create($@"{schema}://{hostName}:{port}/{endPoint}/"))
            {
                result = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(cc.Service.RemoteInvoke(functionName, args));
            }

            return(result);
        }
        private static NetTcpWcfClient <T> Create(bool duplex, object instance, bool reliable, TimeSpan operationTimeout, string endPointFormat, params Object[] args)
        {
            if (duplex && instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (endPointFormat == null || args == null)
            {
                throw new ArgumentNullException((endPointFormat == null) ? "format" : "args");
            }

            var endPoint = string.Format(endPointFormat, args);

            var clientBinding = new NetTcpBinding
            {
                MaxReceivedMessageSize = int.MaxValue,
                MaxBufferSize          = int.MaxValue,
                MaxBufferPoolSize      = int.MaxValue,
                ReaderQuotas           =
                {
                    MaxArrayLength        = int.MaxValue,
                    MaxBytesPerRead       = int.MaxValue,
                    MaxNameTableCharCount = int.MaxValue,
                    MaxDepth               = int.MaxValue,
                    MaxStringContentLength = int.MaxValue
                },
                Security =
                {
                    Mode      = SecurityMode.None,
                    Transport =
                    {
                        ClientCredentialType = TcpClientCredentialType.None,
                    },
                    Message                  =
                    {
                        ClientCredentialType = MessageCredentialType.None
                    }
                },
            };

            if (operationTimeout != null)
            {
                clientBinding.SendTimeout = operationTimeout;
            }

            var clientEndpoint = new EndpointAddress(endPoint);

            var rv = new NetTcpWcfClient <T>();

            try
            {
                if (duplex)
                {
                    rv.Factory = new DuplexChannelFactory <T>(new InstanceContext(instance), clientBinding, clientEndpoint);
                }
                else
                {
                    rv.Factory = new ChannelFactory <T>(clientBinding, clientEndpoint);
                }

                rv.Service = rv.Factory.CreateChannel();
                return(rv);
            }
            catch (Exception /*e*/)
            {
                throw;
            }
        }