Example #1
0
        public async Task <object> MethodCall(MethodCallParams instanceMethodCallParams)
        {
            var instance = simpleInstanceService.instances[instanceMethodCallParams.InstanceId].Instance;
            var lambda   = this.options.ExpressionSerializer.Deserialize(instanceMethodCallParams.SerializedExpression)
                           as LambdaExpression;
            var dynamicDelegate = lambda.Compile();
            var result          = dynamicDelegate.DynamicInvoke(instance);

            if (!instanceMethodCallParams.AwaitResult)
            {
                return(result);
            }

            var taskResult = result as Task;

            if (taskResult != null)
            {
                await taskResult;
            }
            else
            {
                throw new InvalidOperationException($"Unexpected return type. Expected {nameof(Task)}, found '{result.GetType()}'");
            }

            var resultType = taskResult.GetType();

            if (!resultType.IsGenericType)
            {
                // Task without result
                return(null);
            }

            // Task<T>
            return(resultType.GetProperty(nameof(Task <object> .Result)).GetValue(result));
        }
Example #2
0
        public object MethodCall(MethodCallParams instanceMethodCallParams)
        {
            var instance = SimpleInstanceService.Instance.instances[instanceMethodCallParams.InstanceId].Instance;
            var lambda   = this.options.ExpressionSerializer.Deserialize(instanceMethodCallParams.SerializedExpression)
                           as LambdaExpression;
            var dynamicDelegate = lambda.Compile();

            return(dynamicDelegate.DynamicInvoke(instance));
        }
Example #3
0
        private void HandleMethodCall(MethodCallParams methodCallMessage)
        {
            void handleError(Exception e)
            {
                PostObject(
                    new MethodCallResult()
                {
                    CallId      = methodCallMessage.CallId,
                    IsException = true,
                    Exception   = e
                });
            }

            try
            {
                Task.Run(async() =>
                         await MethodCall(methodCallMessage))
                .ContinueWith(t => {
                    if (t.IsFaulted)
                    {
                        handleError(t.Exception);
                    }
                    else
                    {
                        PostObject(
                            new MethodCallResult
                        {
                            CallId        = methodCallMessage.CallId,
                            ResultPayload = this.serializer.Serialize(t.Result)
                        }
                            );
                    }
                });
            }
            catch (Exception e)
            {
                handleError(e);
            }
        }
Example #4
0
 private void HandleMethodCall(MethodCallParams methodCallMessage)
 {
     try
     {
         var result = MethodCall(methodCallMessage);
         PostObject(
             new MethodCallResult()
         {
             CallId        = methodCallMessage.CallId,
             ResultPayload = this.serializer.Serialize(result)
         }
             );
     }
     catch (Exception e)
     {
         PostObject(
             new MethodCallResult()
         {
             CallId      = methodCallMessage.CallId,
             IsException = true,
             Exception   = e
         });
     }
 }