private static bool MethodProxyReceiver(string methodProxyJson, bool socketSuccess)
        {
            MethodProxy resultProxy = null;

            try
            {
                resultProxy = BridgeSerializer.Deserialize <MethodProxy>(ref methodProxyJson);
            }
            catch (Exception ex)
            {
                //Actually if we have an exception here that can mean that the user data has seem class not deserializable
                //between native and Blazor side. As we are probably in a waiting Task scenario, we must try to forward
                //the exception to the waiting task by fetching it's TaskId.

                //It will be easier to debug then
                if (TryGetDefaultFallbackMethodProxy(methodProxyJson, ex, out MethodProxy defaultProxy))
                {
                    return(BlazorCommonDispatcher.Receive(defaultProxy, socketSuccess));
                }

                //Otherwise returning false. The task may be blocked in a waiting state forever
                //TODO: We should implement some kind of CancellationToken in the future on interop method signatures
                //forcing the business code to continue
                return(false);
            }

            return(BlazorCommonDispatcher.Receive(resultProxy, socketSuccess));
        }
Example #2
0
        public static bool Receive(string methodProxyJson)
        {
            if (string.IsNullOrEmpty(methodProxyJson))
            {
                return(false);
            }

            InternalHelper.SetTimeout(() =>
            {
                MethodProxy resultProxy = BridgeSerializer.Deserialize <MethodProxy>(methodProxyJson);
                var taskToReturn        = MethodDispatcher.GetTaskDispatcher(resultProxy.TaskIdentity);
                MethodDispatcher.SetTaskResult(resultProxy.TaskIdentity, resultProxy);

                if (taskToReturn == null)
                {
                    return;
                }

                taskToReturn.RunSynchronously();

                MethodDispatcher.ClearTask(resultProxy.TaskIdentity);
            }, 10);

            return(true);
        }
Example #3
0
        /// <summary>
        /// Get resolved type from given AssemblyName & TypeName
        /// </summary>
        /// <returns></returns>
        public Type ResolvedType()
        {
            if (string.IsNullOrEmpty(SerializedData))
            {
                throw new NullReferenceException();
            }

            return(BridgeSerializer.Deserialize <Type>(this));
        }
        public static bool Receive(string methodProxyJson, bool socketSuccess)
        {
            if (string.IsNullOrEmpty(methodProxyJson))
            {
                return(false);
            }

            InternalHelper.SetTimeout(() =>
            {
                MethodProxy resultProxy = BridgeSerializer.Deserialize <MethodProxy>(ref methodProxyJson);
                var taskToReturn        = MethodDispatcher.GetTaskDispatcher(resultProxy.TaskIdentity);

                if (taskToReturn == null)
                {
                    return;
                }

                if (socketSuccess && resultProxy.TaskSuccess)
                {
                    MethodDispatcher.SetTaskResult(resultProxy.TaskIdentity, resultProxy);
                }
                else
                {
                    Exception exception = null;

                    //If success value (from javascript) is false, like unable to connect to websocket
                    //or if the native task failed with an exception, cancel the current task, that will throw
                    if (!socketSuccess)
                    {
                        exception = new InvalidOperationException($"BlazorMobile was unable to connect to native through websocket server to execute task {resultProxy.TaskIdentity}");
                    }
                    else if (resultProxy.ExceptionDescriptor != null)
                    {
                        //We have some message to send in this case
                        exception = new Exception(resultProxy.ExceptionDescriptor.Message);
                    }
                    else
                    {
                        //Sending uncustomized message
                        exception = new InvalidOperationException($"Task {resultProxy.TaskIdentity} has thrown an exception on native side. See log for more info.");
                    }

                    MethodDispatcher.SetTaskAsFaulted(resultProxy.TaskIdentity, exception);
                }

                taskToReturn.RunSynchronously();

                //Clear task from task list. Should then call the task to execute. It will throw if it has been cancelled
                MethodDispatcher.ClearTask(resultProxy.TaskIdentity);
            }, 10);

            return(true);
        }
        public static bool Receive(string methodProxyJson, bool socketSuccess)
        {
            if (string.IsNullOrEmpty(methodProxyJson))
            {
                return(false);
            }

            try
            {
                MethodProxy resultProxy = BridgeSerializer.Deserialize <MethodProxy>(ref methodProxyJson);
                return(BlazorCommonDispatcher.Receive(resultProxy, socketSuccess));
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteException(ex);
                return(false);
            }
        }
Example #6
0
        public static MethodProxy Receive(string methodProxyJson)
        {
            object      defaultValue = default(object);
            MethodProxy methodProxy  = null;

            try
            {
                methodProxy = BridgeSerializer.Deserialize <MethodProxy>(methodProxyJson);

                Type   iface           = methodProxy.InterfaceType.ResolvedType();
                object concreteService = DependencyServiceExtension.Get(iface);

                MethodInfo baseMethod = MethodProxyHelper.GetClassMethodInfo(concreteService.GetType(), iface, methodProxy.MethodIndex);

                //In case of failure, getting Default Return Type
                defaultValue = GetDefault(baseMethod.ReturnType);

                if (methodProxy.GenericTypes != null && methodProxy.GenericTypes.Length > 0)
                {
                    Type[] genericTypes = methodProxy.GenericTypes.Select(p => p.ResolvedType()).ToArray();

                    methodProxy.ReturnValue = baseMethod.MakeGenericMethod(genericTypes).Invoke(concreteService, methodProxy.Parameters);
                    methodProxy.TaskSuccess = true;
                }
                else
                {
                    methodProxy.ReturnValue = baseMethod.Invoke(concreteService, methodProxy.Parameters);
                    methodProxy.TaskSuccess = true;
                }

                if (methodProxy.AsyncTask)
                {
                    methodProxy.ReturnValue = GetResultFromTask(methodProxy.ReturnType.ResolvedType(), (Task)methodProxy.ReturnValue);
                }
            }
            catch (Exception)
            {
                methodProxy.ReturnValue = defaultValue;
                methodProxy.TaskSuccess = false;
            }

            return(methodProxy);
        }
        public static bool ReceiveFromXamarin(string methodProxyJson, bool socketSuccess)
        {
            if (string.IsNullOrEmpty(methodProxyJson))
            {
                return(false);
            }

            try
            {
                ClientMethodProxy resultProxy = BridgeSerializer.Deserialize <ClientMethodProxy>(ref methodProxyJson);
                BlazorMobileComponent.GetJSRuntime().InvokeAsync <bool>("contextBridgeSendClient", resultProxy.InteropAssembly, resultProxy.InteropMethod, resultProxy.InteropParameters);
                return(true);
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteException(ex);
                return(false);
            }
        }
Example #8
0
        public static void Receive(string methodProxyJson)
        {
            if (string.IsNullOrEmpty(methodProxyJson))
            {
                return;
            }

            MethodProxy resultProxy  = BridgeSerializer.Deserialize <MethodProxy>(methodProxyJson);
            var         taskToReturn = MethodDispatcher.GetTaskDispatcher(resultProxy.TaskIdentity);

            MethodDispatcher.SetTaskResult(resultProxy.TaskIdentity, resultProxy);

            if (taskToReturn == null)
            {
                return;
            }

            taskToReturn.RunSynchronously();

            MethodDispatcher.ClearTask(resultProxy.TaskIdentity);
        }
        private static bool MessageProxyReceiver(string methodProxyJson, bool socketSuccess)
        {
            if (string.IsNullOrEmpty(methodProxyJson))
            {
                return(false);
            }

            try
            {
                MessageProxy resultProxy = BridgeSerializer.Deserialize <MessageProxy>(ref methodProxyJson);

                //Using JSInvokable API
                if (resultProxy.IsJSInvokable)
                {
                    BlazorMobileService.SendMessageToJSInvokableMethod(resultProxy);
                }
                else
                {
                    if (resultProxy.InteropParameters == null)
                    {
                        resultProxy.InteropParameters = new object[0];
                    }

                    //This is more about atomicity as we don't want to do extensive test
                    //We just only want to prevent to pass an empty / null value arguments that would maybe not be identifiable on its type after serialization / deserialization
                    Type ArgsType = resultProxy.InteropArgsType.ResolvedType();

                    //Using delegate Messaging API
                    BlazorMobileService.SendMessageToSubscribers(resultProxy.InteropMethod, ArgsType, resultProxy.InteropParameters);
                }

                return(true);
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteException(ex);
                return(false);
            }
        }
Example #10
0
 public static MethodProxy GetMethodProxyFromJSON(ref string json)
 {
     return(BridgeSerializer.Deserialize <MethodProxy>(ref json));
 }