JsonSerializerSettings, try/catch exceptions, HttpClient with timeout
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Mon, 24 Feb 2014 23:51:07 +0000 (00:51 +0100)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Mon, 24 Feb 2014 23:51:07 +0000 (00:51 +0100)
Mono/RemoteAgents/GTKLinux/GTKLinux.csproj
Mono/RemoteAgents/GTKLinux/Model/CallRemoteProcedure.cs

index ad9a7f6..712fb3a 100644 (file)
@@ -10,6 +10,8 @@
     <RootNamespace>GTKLinux</RootNamespace>
     <AssemblyName>GTKLinux</AssemblyName>
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <Description>Little program to connect with remote applications
+through JSON API.</Description>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
     <DebugSymbols>true</DebugSymbols>
index 709543f..3ec0a97 100644 (file)
@@ -1,13 +1,15 @@
 using System;
-using System.Runtime.Remoting.Messaging;
-using Newtonsoft.Json;
+using System.Net;
 using System.Net.Http;
+using System.Reflection;
 using System.Runtime.Remoting.Lifetime;
+using System.Runtime.Remoting.Messaging;
+using System.Security.Cryptography.X509Certificates;
 using System.Threading.Tasks;
 using Gtk;
-using System.Reflection;
-using System.Net;
-using System.Security.Cryptography.X509Certificates;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Converters;
+using Newtonsoft.Json.Serialization;
 
 namespace Example.RemoteAgents.GTKLinux.Model
 {
@@ -16,32 +18,44 @@ namespace Example.RemoteAgents.GTKLinux.Model
 
     async public Task<TResult> callRemoteService<TResult>(string uri, string method)
     {
+      TResult result = default(TResult);
       var postData = new POST();
       postData.id = "2114567586433855105";
       postData.jsonrpc = "2.0";
       postData.method = method;
+      var jsonSettings = new JsonSerializerSettings{
+        Error = delegate(object sender, ErrorEventArgs args)
+        {
+          Console.WriteLine(args.ErrorContext.Error.Message);
+          args.ErrorContext.Handled = true;
+        }
+      };
 
-      string data = JsonConvert.SerializeObject(postData);
-      HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json-rpc");
+      try {
+        string data = JsonConvert.SerializeObject(postData, jsonSettings);
+        HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json-rpc");
 
-      HttpResponseMessage response = await this.issueCall(uri, content);
+        HttpResponseMessage response = await this.issueCall(uri, content);
 
-      TResult result = default(TResult);
+          if (response.StatusCode == HttpStatusCode.OK) {
+            Task<byte[]> responseBytes = response.Content.ReadAsByteArrayAsync();
+            string responseString = System.Text.Encoding.UTF8.GetString(responseBytes.Result);
 
-      if (response.StatusCode == HttpStatusCode.OK) {
-        Task<byte[]> responseBytes = response.Content.ReadAsByteArrayAsync();
-        string responseString = System.Text.Encoding.UTF8.GetString(responseBytes.Result);
-        POSTResult<TResult> postResult = JsonConvert.DeserializeObject<POSTResult<TResult>>(responseString);
-        result = postResult.result;
+            POSTResult<TResult> postResult = JsonConvert.DeserializeObject<POSTResult<TResult>>(responseString, jsonSettings);
+            result = postResult.result;
+          }
+      }
+      catch (Exception e)
+      {
+          Console.WriteLine("callRemoteService exception. Message: {0}  Stacktrace: {1} ", e.Message, e.StackTrace);
       }
 
       return result;
     }
 
-
     async private Task<HttpResponseMessage> issueCall(string uri, HttpContent content)
     {
-        using (HttpClient client = new HttpClient())
+        using (HttpClient client = new HttpClient{ Timeout = TimeSpan.FromSeconds(5)})
         {
             return await client.PostAsync(uri, content);
         }