protected object InvokeServiceMethod(ServiceMethod serviceMethod, object[] arguments)
        {
            switch (serviceMethod.ResponseType)
            {
            case MethodResponseType.None:
                return(null);

            case MethodResponseType.Sync:
                return(serviceMethod.Invoke(arguments));

            case MethodResponseType.Async:
                var task = Task.Run(() => (Task)serviceMethod.Invoke(arguments));
                if (task.Wait(InvocationTimeout))
                {
                    return(0);
                }
                else
                {
                    HandleError(task.Exception as Exception ?? new InvocationTimeoutException(InvocationTimeout, serviceMethod));
                    return(null);
                }

            case MethodResponseType.AsyncWithResult:
                return(_InvokeServiceMethodAsyncWithResult
                       .MakeGenericMethod(serviceMethod.ReturnType.GetGenericArguments())
                       .Invoke(this, new object[] { serviceMethod, arguments }));

            default:
                HandleError(new ResponseTypeNotSupportedException(serviceMethod.ResponseType));
                return(null);
            }
        }
 protected object TryInvokeServiceMethod(ServiceMethod serviceMethod, object[] arguments)
 {
     try {
         return(InvokeServiceMethod(serviceMethod, arguments));
     } catch (Exception e) {
         HandleError(e);
         return(null);
     }
 }
 protected void RegisterService(object service, byte[] serviceSignature, MethodInfo[] methods)
 {
     CheckService(service, serviceSignature);
     foreach (var method in methods)
     {
         var methodSignature = ServiceUtils.GetMethodSignature(method);
         var serviceMethod   = new ServiceMethod(method, service);
         CheckServiceMethod(serviceMethod);
         RegisterService(serviceSignature, methodSignature, serviceMethod);
     }
 }
        private T InvokeServiceMethodAsyncWithResult <T>(ServiceMethod serviceMethod, object[] arguments)
        {
            var taskWithResult = Task.Run(new Func <Task <T> >(() => (Task <T>)serviceMethod.Invoke(arguments)));

            if (taskWithResult.Wait(InvocationTimeout))
            {
                return(taskWithResult.Result);
            }
            else
            {
                HandleError(taskWithResult.Exception as Exception ?? new InvocationTimeoutException(InvocationTimeout, serviceMethod));
                return(default);
 protected virtual void RegisterService(byte[] serviceSignature, byte[] methodSignature, ServiceMethod serviceMethod) =>
 Services.Add(ServiceUtils.CombineSignatures(serviceSignature, methodSignature), serviceMethod);