Start a new topic

This is just getting INSANE

The following is a native client i have been using as a base class my my remoting system:


        public void greetServer(System.String input, MethodCallback<System.String> callback = null)

 {

            AjaxClient.CallMethod<System.String>(GatewayUrl, "ajax.gateway.greetserver", new object[] { input }, callback);

 }


this really wraps a work horse AjaxClient.CallMethod



        public static void CallMethod<T>(string url, string method, object[] parameters = null, MethodCallback<T> callback = null)

 {

 ExecuteMethodAsync(url, method, parameters).ContinueWith((result) => {

                if (result.IsFaulted) {

                    Exception exc = (result.Exception != null) ? result.Exception : new Exception("An unkonwn error occured");

                    if (exc is AggregateException && exc.InnerException != null) {

 exc = exc.InnerException;

 }

                    if (exc is TargetInvocationException && exc.InnerException != null) {

 exc = exc.InnerException;

 }

                    if (callback != null && callback.OnFailure != null) {

 callback.OnFailure(exc);

                    } else {

                        throw exc;

 }

                } else if (result.IsCanceled) {

                    Exception exc2 = new Exception("The call method has been canceled: " + result.Status.ToString());

                    if (callback != null && callback.OnFailure != null) {

 callback.OnFailure(exc2);

                    } else {

                        throw exc2;

 }

                } else if (result.IsCompleted) {

                    if (callback != null) {

                        try {

                            if (callback.OnSuccess != null) { 

                                if (typeof(T) == typeof(System.Nothing)) {

                                    object nil = new System.Nothing();

 callback.OnSuccess((T)nil);

                                } else {

 callback.OnSuccess((T)result.Result);

 }

 }

                        } catch (Exception ex3) {

                            Exception exc4 = (ex3 != null) ? ex3 : new Exception("An unkonwn error occured");

                            if (exc4 is AggregateException && exc4.InnerException != null) {

 exc4 = exc4.InnerException;

 }

                            if (exc4 is TargetInvocationException && exc4.InnerException != null) {

 exc4 = exc4.InnerException;

 }

                            if (callback.OnFailure != null) {

 callback.OnFailure(exc4);

                            } else {

                                throw exc4;

 }

 }

 }

                } else {

                    Exception exc5 = new Exception("An unkonwn task status encounted: " + result.Status.ToString());

                    if (callback != null && callback.OnFailure != null) {

 callback.OnFailure(exc5);

                    } else {

                        throw exc5;

 }

 }

 });

 }



ALL NATIVE .NET USING BinaryFormatter


I will post my DuoCode version OF THE EXACT call method


Look for next post for DuoCode

1 Comment

I just made this class in DuoCode:


        public static void CallMethod<T>(string url, string method, object[] parameters = null, MethodCallback<T> callback = null)

 {

            string data = FormatRequest(method, parameters);

 XMLHttpRequest xhr = new XMLHttpRequest();

            xhr.open("POST", url, true);

            xhr.setRequestHeader("Content-Type", RpcPostContentType);

            //

            xhr.onerror = delegate(ErrorEvent err) {

                System.Console.WriteLine("XHR - ON ERROR: " + err.message); // TODO: Handle Error Here

                return null;

 };

            //

            xhr.onreadystatechange = delegate(Event evt) {

                if (xhr.readyState == 4) {

                    if (xhr.status == 200) {

                        try {

                            object nil = null;

 T httpResponse = (T)nil;

                            string response_data = (string)xhr.response;


                            System.Console.WriteLine(response_data);

                            Ajax.Gateway.Webix.Webix.Message(response_data);


                            string returnType = xhr.getResponseHeader("Content-Type");

                            if (returnType == RpcVoidResponseType || returnType == RpcNullResponseType) {

 httpResponse = (T)nil;

                            } else if (returnType == RpcJsonResponseType) {

                                httpResponse = JSON.parse(xhr.response);

                            } else if (returnType == RpcInteropResponseType) {

                                httpResponse = JSON.parse(xhr.response);

                            } else if (returnType == RpcStringResponseType || returnType == RpcBase64ResponseType) {

 httpResponse = xhr.response;

                            } else if (returnType == RpcBooleanResponseType) {

 httpResponse = (T)xhr.response;

                            } else if (returnType == RpcInt16ResponseType || returnType == RpcInt32ResponseType || returnType == RpcInt64ResponseType || returnType == RpcIntPtrResponseType) {

 //httpResponse = parseInt(xhr.response);

                            } else if (returnType == RpcUInt16ResponseType || returnType == RpcUInt32ResponseType || returnType == RpcUInt64ResponseType || returnType == RpcUIntPtrResponseType) {

 //httpResponse = parseInt(xhr.response);

                            } else if (returnType == RpcDoubleResponseType || returnType == RpcDecimalResponseType) {

 //httpResponse = parseInt(xhr.response);

                            } else if (returnType == RpcSingleResponseType) {

 //httpResponse = parseFloat(xhr.response);

                            } else if (returnType == RpcByteResponseType || returnType == RpcSByteResponseType) {

 //httpResponse = parseInt(xhr.response);

                            } else if (returnType == RpcDateTimeResponseType) {

 //httpResponse = new Date(xhr.response);

                            } else if (returnType == RpcCharResponseType) {

 httpResponse = xhr.response;

                            } else if (returnType == RpcXmlResponseType) {

                                //if (window.DOMParser) {

 //    var parser = new DOMParser();

 //    httpResponse = parser.parseFromString(xhr.response, "text/xml");

                                //}

                                //else // Internet Explorer

                                //{

 //    httpResponse = new ActiveXObject("Microsoft.XMLDOM");

 //    httpResponse.async = false;

 //    httpResponse.loadXML(xhr.response);

                                //}

                            } else if (returnType == RpcNativeContentType) {

 throw new Exception("Native response content types not supported");

                            } else {

                                throw new Exception("Invalid response content type encountered: " + returnType);

 }

                            //

                            if (callback != null && callback.OnFailure != null) {

 callback.OnSuccess(httpResponse);

 }

                        } catch (Exception err) {

                            if (callback != null && callback.OnFailure != null) {

 callback.OnFailure(err);

 }

 }

                    } else {

                        if (callback != null && callback.OnFailure != null) {

                            string status = xhr.status.ToString(), status_text = xhr.statusText;

                            callback.OnFailure(new Exception(status + " - " + status_text));

 }

 }

 }

                return null;

 };

 xhr.send(data);

 }



AND IT WORKS... Now i can use the SAME EXACT signature for my remoting wheather im writing code on mac using Xamarin targeting IOS, ANDROID, MAC and WIndows... AND NOW C# IN THE BROWSER

with the same services (At least the way i call those services)


how freaking cool is that ;-)


 GreetingService svc = new GreetingService("http://localhost:4444/ajax/gateway");

                svc.greetServer("John Doe Smith", new MethodCallback<string>(

 (result) => {

                        Console.WriteLine("Got a Good Result: " + result);

 }, (error) => {

                        Console.WriteLine("Got Error: " + error.Message);

 }

 ));




Login or Signup to post a comment