MY METHOD ALWAYS CALL 'FINALLY' BESIDES FINISHING THE 'TRY'

SIUSSIUS PSMember
edited April 2016 in Xamarin.Forms

Method below is always calling the 'Finally'

public async Task Save(){ var trip = new Trip { Name = this.Name, Description = this.Description, Latitude = this.Latitude, Longitude = this.Longitude }; try{ var tripLogService = new TripLogApiDataService (new Uri ("http://tripservice.azurewebsites.net")); await tripLogService.AddEntryAsync(trip); await this._navigationService.ComeBack(); }finally{ await _messageService.ShowDialog ("Error", "Finally exception no método Save", "Beleza"); // IT ALWAYS SHOW THIS } }

Constructor I call at the 1st line of my 'try'
public TripLogApiDataService (Uri baseUri){ _baseUri = baseUri; _headers = new Dictionary<string, string> (); _headers.Add ("zumo-api-version", "2.0.0"); }

1st await method call:
public async System.Threading.Tasks.Task<Trip> AddEntryAsync (Trip entry){ var url = new Uri (_baseUri, "/tables/entry"); var response = await SendRequestAsync<Trip> (url, HttpMethod.Post, _headers, entry); return response; }

Method SendRequestAsync:
protected async Task<T> SendRequestAsync<T>( Uri url, HttpMethod httpMethod = null, IDictionary<string, string> headers = null, object requestData = null){ var result = default(T); var method = httpMethod ?? HttpMethod.Get; var data = requestData == null ? null : JsonConvert.SerializeObject (requestData); using (var request = new HttpRequestMessage (method, url)) { if (data != null) request.Content = new StringContent (data, Encoding.UTF8, "application/json"); if (headers != null) foreach (var h in headers) request.Headers.Add (h.Key, h.Value); using (var handler = new HttpClientHandler ()) { using (var client = new HttpClient (handler)) { using (var response = await client.SendAsync (request, HttpCompletionOption.ResponseContentRead)) { var content = response.Content == null ? null : await response.Content.ReadAsStringAsync (); if (response.IsSuccessStatusCode) result = JsonConvert.DeserializeObject<T> (content); } } } } return result; }

Best Answers

Answers

  • SIUSSIUS PSMember

    @DaveHunt
    @Momm

    hehehehehe omg... it was just this???
    I'm ashamed now.... sorry for taking your time for something so simple

Sign In or Register to comment.