Created
October 3, 2017 13:05
-
-
Save reinaldocoelho/faf61e769ccbe5ed56496130ea25fdb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Classe base para servir comunicação aos serviços do servidor de e-mail da enki. | |
/// </summary> | |
public abstract class EnkiMailBase : IDisposable | |
{ | |
protected HttpClient client = new HttpClient(); | |
protected string _baseUri = "http://localhost:58240/"; | |
protected EnkiMailBase() | |
{ | |
_baseUri = System.Configuration.ConfigurationManager.AppSettings["EnkiMailServerUri"]; | |
client.BaseAddress = new Uri(_baseUri); | |
client.DefaultRequestHeaders.Accept.Clear(); | |
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
} | |
public void Dispose() | |
{ | |
client.Dispose(); | |
GC.SuppressFinalize(this); | |
} | |
} | |
/// <summary> | |
/// Classe cliente para acesso a API rest do servidor de e-mails da enki. | |
/// </summary> | |
public class EnkiMailAccount : EnkiMailBase | |
{ | |
private ILog _logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
private IList<string> _errors; | |
public long Id { get; set; } | |
public string Domain { get; set; } | |
public string Login { get; set; } | |
private string Email | |
{ | |
get | |
{ | |
return (Login ?? "").Trim() + "@" + (Domain ?? "").Trim(); | |
} | |
} | |
public bool Active { get; set; } | |
public bool Deleted { get; set; } | |
public EnkiMailAccount(long id, string domain, string login) : base() | |
{ | |
_errors = new List<string>(); | |
Id = id; | |
Domain = domain; | |
Login = login; | |
} | |
[JsonConstructor] | |
public EnkiMailAccount(long id, string domain, string login, bool active, bool deleted) : this(id, domain, login) | |
{ | |
Active = active; | |
Deleted = deleted; | |
} | |
public IList<string> GetErrors() | |
{ | |
return _errors; | |
} | |
/// <summary> | |
/// Efetua a contagem de e-mails pendentes no servidor para esta conta. | |
/// </summary> | |
/// <returns>Contagem de mensagens pendentes no servidor para esta conta.</returns> | |
public async Task<int> CountPendingMessagesToAccount() | |
{ | |
_errors = new List<string>(); | |
var response = await client.GetAsync("api/ReceivedMessage/count/" + Email).ConfigureAwait(false); | |
try | |
{ | |
if (!response.IsSuccessStatusCode && response.StatusCode == System.Net.HttpStatusCode.NotFound) | |
{ | |
_errors.Add("A conta solicitada ainda não existe no servidor de e-mails."); | |
return 0; | |
} | |
else if (!response.IsSuccessStatusCode && response.StatusCode == System.Net.HttpStatusCode.BadRequest) | |
{ | |
var resultError = await response.Content.ReadAsAsync<Exception>(); | |
_errors.Add(resultError.Message); | |
return 0; | |
} | |
response.EnsureSuccessStatusCode(); | |
var resultData = await response.Content.ReadAsAsync<int>(); | |
return resultData; | |
} | |
catch (Exception e) | |
{ | |
_logger.Error("Resultado de erro não tratado ao tentar contabilizar mensagens pendentes para a conta.", e); | |
return 0; | |
} | |
} | |
} |
Já tentou trocar isso:
var resultData = await response.Content.ReadAsAsync<int>();
Por isso:
var resultData = await System.Net.Http.HttpContentExtensions.ReadAsAsync<int>(response.Content);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Descrição do erro: