From a5dc9ae6f1ef834882780c3051542c9d7dc24edf Mon Sep 17 00:00:00 2001 From: Gustavo Martin Date: Wed, 26 Feb 2014 09:17:10 +0100 Subject: [PATCH] RemoteAgents Windows Phone 8 .NET implementation. Nested using statement. Call Dispose when using classes implementing IDisposable Trying to use Cancellation --- .../RemoteAgents/RemoteAgents/MainPage.xaml.cs | 2 +- .../RemoteAgents/Model/CallRemoteProcedure.cs | 43 +++++++++++++++------- .../RemoteAgents/RemoteAgents/View/View.cs | 6 +-- .../RemoteAgents/ViewModel/ViewModel.cs | 4 +- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/WindowsPhone/RemoteAgents/RemoteAgents/MainPage.xaml.cs b/WindowsPhone/RemoteAgents/RemoteAgents/MainPage.xaml.cs index 032bc50..449a27a 100644 --- a/WindowsPhone/RemoteAgents/RemoteAgents/MainPage.xaml.cs +++ b/WindowsPhone/RemoteAgents/RemoteAgents/MainPage.xaml.cs @@ -32,7 +32,7 @@ namespace RemoteAgents { try { - string currentDate = await view.getCurrentDate(); + string currentDate = await view.GetCurrentDateAsync(); if (currentDate != null) { this.CurrentDateTextBox.Text = currentDate; diff --git a/WindowsPhone/RemoteAgents/RemoteAgents/Model/CallRemoteProcedure.cs b/WindowsPhone/RemoteAgents/RemoteAgents/Model/CallRemoteProcedure.cs index d00a3ce..4b3f119 100644 --- a/WindowsPhone/RemoteAgents/RemoteAgents/Model/CallRemoteProcedure.cs +++ b/WindowsPhone/RemoteAgents/RemoteAgents/Model/CallRemoteProcedure.cs @@ -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 callRemoteService(string uri, string method) + async public Task CallPostRemoteServiceAsync(string uri, string method) + { + POSTResult postResult = null; + + postResult = await this.PostAsync(uri, method, CancellationToken.None); + + return postResult.result; + } + + async private Task> PostAsync(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 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>(responseString, jsonSettings); + } - if (response.StatusCode == HttpStatusCode.OK) - { - Task responseBytes = response.Content.ReadAsByteArrayAsync(); - string responseString = Encoding.UTF8.GetString(responseBytes.Result, 0, responseBytes.Result.Length); - POSTResult postResult = JsonConvert.DeserializeObject>(responseString, jsonSettings); - result = postResult.result; + return postResult; } - - return result; } @@ -51,13 +65,14 @@ namespace RemoteAgents.WindowsPhone.Model /// /// The Uri the request is sent to. /// The HTTP request content sent to the server. + /// Cancellation token. /// When some error. /// System.Threading.Tasks.Task]]>.The task object representing the asynchronous operation. - async private Task doCall(string uri, HttpContent content) + async private Task 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); } } diff --git a/WindowsPhone/RemoteAgents/RemoteAgents/View/View.cs b/WindowsPhone/RemoteAgents/RemoteAgents/View/View.cs index 92c1324..997c6b7 100644 --- a/WindowsPhone/RemoteAgents/RemoteAgents/View/View.cs +++ b/WindowsPhone/RemoteAgents/RemoteAgents/View/View.cs @@ -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 getCurrentDate() + async public Task GetCurrentDateAsync() { - return await vm.getCurrentDate(); + return await vm.GetCurrentDateAsync(); } } } diff --git a/WindowsPhone/RemoteAgents/RemoteAgents/ViewModel/ViewModel.cs b/WindowsPhone/RemoteAgents/RemoteAgents/ViewModel/ViewModel.cs index 7e52b6d..e4777ca 100644 --- a/WindowsPhone/RemoteAgents/RemoteAgents/ViewModel/ViewModel.cs +++ b/WindowsPhone/RemoteAgents/RemoteAgents/ViewModel/ViewModel.cs @@ -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 getCurrentDate() + async public Task GetCurrentDateAsync() { - return await remoteProcedure.callRemoteService(uri, "getCurrentDate"); + return await remoteProcedure.CallPostRemoteServiceAsync(uri, "getCurrentDate"); } } } -- 2.1.4