RemoteAgents Windows Phone 8 .NET implementation.
authorGustavo Martin <gu.martinm@gmail.com>
Wed, 26 Feb 2014 08:17:10 +0000 (09:17 +0100)
committerGustavo Martin <gu.martinm@gmail.com>
Wed, 26 Feb 2014 08:17:10 +0000 (09:17 +0100)
Nested using statement.
Call Dispose when using classes implementing IDisposable
Trying to use Cancellation

WindowsPhone/RemoteAgents/RemoteAgents/MainPage.xaml.cs
WindowsPhone/RemoteAgents/RemoteAgents/Model/CallRemoteProcedure.cs
WindowsPhone/RemoteAgents/RemoteAgents/View/View.cs
WindowsPhone/RemoteAgents/RemoteAgents/ViewModel/ViewModel.cs

index 032bc50..449a27a 100644 (file)
@@ -32,7 +32,7 @@ namespace RemoteAgents
         {
             try
             {
-                string currentDate = await view.getCurrentDate();
+                string currentDate = await view.GetCurrentDateAsync();
                 if (currentDate != null)
                 {
                     this.CurrentDateTextBox.Text = currentDate;
index d00a3ce..4b3f119 100644 (file)
@@ -4,6 +4,7 @@ using System;
 using System.Net;
 using System.Net.Http;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace RemoteAgents.WindowsPhone.Model
@@ -11,7 +12,16 @@ namespace RemoteAgents.WindowsPhone.Model
     public class CallRemoteProcedure
     {
 
-        async public Task<TResult> callRemoteService<TResult>(string uri, string method)
+        async public Task<TResult> CallPostRemoteServiceAsync<TResult>(string uri, string method)
+        {
+            POSTResult<TResult> postResult = null;
+
+            postResult = await this.PostAsync<TResult>(uri, method, CancellationToken.None);
+
+            return postResult.result;
+        }
+
+        async private Task<POSTResult<TResult>> PostAsync<TResult>(string uri, string method, CancellationToken cancellation)
         {
             var postData = new POST();
             postData.id = "2114567586433855105";
@@ -28,21 +38,25 @@ namespace RemoteAgents.WindowsPhone.Model
             };
 
             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);
+            // see: http://stackoverflow.com/questions/1329739/nested-using-statements-in-c-sharp
+            // see: http://stackoverflow.com/questions/5895879/when-do-we-need-to-call-dispose-in-dot-net-c
+            //TODO: Am I really sure I have to call the Dispose method of HttpContent content. In this case shouldn't it be stupid?
+            // for HttpResponseMessage response I am sure I have to do it but I am not for HttpContent content.
+            using (HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json-rpc"))
+            using (HttpResponseMessage response = await this.PostAsync(uri, content, cancellation))
+            {
+                POSTResult<TResult> postResult = null;
 
-            TResult result = default(TResult);
+                if (response.StatusCode == HttpStatusCode.OK)
+                {
+                    byte[] responseBytes = await response.Content.ReadAsByteArrayAsync();
+                    string responseString = Encoding.UTF8.GetString(responseBytes, 0, responseBytes.Length);
+                    postResult = JsonConvert.DeserializeObject<POSTResult<TResult>>(responseString, jsonSettings);
+                }
 
-            if (response.StatusCode == HttpStatusCode.OK)
-            {
-                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, jsonSettings);
-                result = postResult.result;
+                return postResult;
             }
-
-            return result;
         }
 
 
@@ -51,13 +65,14 @@ namespace RemoteAgents.WindowsPhone.Model
         /// </summary>
         /// <param name="uri">The Uri the request is sent to.</param>
         /// <param name="content">The HTTP request content sent to the server.</param>
+        /// <param name="System.Threading.CancellationToken">Cancellation token.</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)
+        async private Task<HttpResponseMessage> PostAsync(string uri, HttpContent content, CancellationToken cancellation)
         {
             using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) })
             {
-                return await client.PostAsync(uri, content);
+                return await client.PostAsync(uri, content, cancellation);
             }
         }
 
index 92c1324..997c6b7 100644 (file)
@@ -5,11 +5,11 @@ namespace RemoteAgents.WindowsPhone.View
 {
     public class ViewImpl
     {
-        private static readonly ViewModelImpl vm = new ViewModelImpl();
+        private readonly ViewModelImpl vm = new ViewModelImpl();
 
-        async public Task<string> getCurrentDate()
+        async public Task<string> GetCurrentDateAsync()
         {
-            return await vm.getCurrentDate();
+            return await vm.GetCurrentDateAsync();
         }
     }
 }
index 7e52b6d..e4777ca 100644 (file)
@@ -8,9 +8,9 @@ namespace RemoteAgents.WindowsPhone.ViewModel
         private static readonly string uri = "http://gumartinm.name/spring-mainapp/CurrentDateService.json";
         private readonly CallRemoteProcedure remoteProcedure = new CallRemoteProcedure();
 
-        async public Task<string> getCurrentDate()
+        async public Task<string> GetCurrentDateAsync()
         {
-            return await remoteProcedure.callRemoteService<string>(uri, "getCurrentDate");
+            return await remoteProcedure.CallPostRemoteServiceAsync<string>(uri, "getCurrentDate");
         }
     }
 }