No time for comments.
authorGustavo Martin <gu.martinm@gmail.com>
Tue, 25 Feb 2014 07:50:45 +0000 (08:50 +0100)
committerGustavo Martin <gu.martinm@gmail.com>
Tue, 25 Feb 2014 07:50:45 +0000 (08:50 +0100)
WindowsPhone/RemoteAgents/RemoteAgents/MainPage.xaml.cs
WindowsPhone/RemoteAgents/RemoteAgents/Model/CallRemoteProcedure.cs

index 8705bb9..032bc50 100644 (file)
@@ -30,10 +30,18 @@ namespace RemoteAgents
 
         async private void ButtonRetrieveRemoteData_Click(object sender, RoutedEventArgs e)
         {
-            string currentDate = await view.getCurrentDate();
-            if (currentDate != null)
+            try
             {
-                this.CurrentDateTextBox.Text = currentDate;
+                string currentDate = await view.getCurrentDate();
+                if (currentDate != null)
+                {
+                    this.CurrentDateTextBox.Text = currentDate;
+                }
+            }
+            catch (Exception exception)
+            {
+                //TODO: logger for Windows Phone 8 :(
+                Console.WriteLine("ButtonGetDateClicked. Message: {0}  Stacktrace: {1}", exception.Message, exception.StackTrace);
             }
         }
 
index 1179d86..d00a3ce 100644 (file)
@@ -1,4 +1,6 @@
 using Newtonsoft.Json;
+using Newtonsoft.Json.Serialization;
+using System;
 using System.Net;
 using System.Net.Http;
 using System.Text;
@@ -15,8 +17,17 @@ namespace RemoteAgents.WindowsPhone.Model
             postData.id = "2114567586433855105";
             postData.jsonrpc = "2.0";
             postData.method = method;
+            var jsonSettings = new JsonSerializerSettings
+            {
+                Error = delegate(object sender, ErrorEventArgs args)
+                {
+                    //TODO: logger for Windows Phone 8 :(
+                    Console.WriteLine(args.ErrorContext.Error.Message);
+                    args.ErrorContext.Handled = true;
+                }
+            };
 
-            string data = JsonConvert.SerializeObject(postData);
+            string data = JsonConvert.SerializeObject(postData, jsonSettings);
             HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json-rpc");
 
             HttpResponseMessage response = await this.doCall(uri, content);
@@ -27,7 +38,7 @@ namespace RemoteAgents.WindowsPhone.Model
             {
                 Task<byte[]> responseBytes = response.Content.ReadAsByteArrayAsync();
                 string responseString = Encoding.UTF8.GetString(responseBytes.Result, 0, responseBytes.Result.Length);
-                POSTResult<TResult> postResult = JsonConvert.DeserializeObject<POSTResult<TResult>>(responseString);
+                POSTResult<TResult> postResult = JsonConvert.DeserializeObject<POSTResult<TResult>>(responseString, jsonSettings);
                 result = postResult.result;
             }
 
@@ -35,9 +46,16 @@ namespace RemoteAgents.WindowsPhone.Model
         }
 
 
+        /// <summary>
+        /// Send a POST request to the specified Uri as an asynchronous operation.
+        /// </summary>
+        /// <param name="uri">The Uri the request is sent to.</param>
+        /// <param name="content">The HTTP request content sent to the server.</param>
+        /// <exception cref="System.InvalidOperationException">When some error.</exception>
+        /// <returns>System.Threading.Tasks.Task<![CDATA[<TResult>]]>.The task object representing the asynchronous operation.</returns>
         async private Task<HttpResponseMessage> doCall(string uri, HttpContent content)
         {
-            using (HttpClient client = new HttpClient())
+            using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) })
             {
                 return await client.PostAsync(uri, content);
             }