Confusion with await not return value [on hold]
I'm doing an API and now I'm consuming with ASP.NET MVC.
I have HomeController.cs, MailController.cs and Services.cs
HomeController
public async Task<IActionResult> Mirror()
{
WeatherService _weather = new WeatherService();
await _weather.GetMail();
var a = await _weather.GetWeatherToDay();
return View(a);
}
MailController
[HttpGet]
public async Task<IEnumerable<MailMessage>> GetEmail()
{
try
{
var client = new ImapClient("imap.gmail.com", 993, "ACCOUNT", "PASS", AuthMethod.Login, true);
var uids = client.Search(SearchCondition.All()).TakeLast(5);
var msg = client.GetMessages(uids, FetchOptions.HeadersOnly);
return msg;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Services
public async Task<string> GetMail()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost:44313/api/");
var response = await client.GetAsync("mail");
var responseToString = await response.Content.ReadAsStringAsync();
return responseToString;
}
}
Yes I know that my code it could be better...
I think that the problem is in the awaiters because when try to save result on variable response from Services get error and the executions it's continue... but without value.
Thanks! :)
c# asp.net api asp.net-web-api
put on hold as unclear what you're asking by Damien_The_Unbeliever, Servy, Peter Bons, EJoshuaS, Edric 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I'm doing an API and now I'm consuming with ASP.NET MVC.
I have HomeController.cs, MailController.cs and Services.cs
HomeController
public async Task<IActionResult> Mirror()
{
WeatherService _weather = new WeatherService();
await _weather.GetMail();
var a = await _weather.GetWeatherToDay();
return View(a);
}
MailController
[HttpGet]
public async Task<IEnumerable<MailMessage>> GetEmail()
{
try
{
var client = new ImapClient("imap.gmail.com", 993, "ACCOUNT", "PASS", AuthMethod.Login, true);
var uids = client.Search(SearchCondition.All()).TakeLast(5);
var msg = client.GetMessages(uids, FetchOptions.HeadersOnly);
return msg;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Services
public async Task<string> GetMail()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost:44313/api/");
var response = await client.GetAsync("mail");
var responseToString = await response.Content.ReadAsStringAsync();
return responseToString;
}
}
Yes I know that my code it could be better...
I think that the problem is in the awaiters because when try to save result on variable response from Services get error and the executions it's continue... but without value.
Thanks! :)
c# asp.net api asp.net-web-api
put on hold as unclear what you're asking by Damien_The_Unbeliever, Servy, Peter Bons, EJoshuaS, Edric 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
GetEmail
does not await anything
– Nkosi
Dec 27 at 13:20
4
"Confusion", "problem is in". You haven't actually told us what the problem is or what's confusing you. Are you getting unexpected results (what did you expect)? Compilation errors? Runtime errors? Something else?
– Damien_The_Unbeliever
Dec 27 at 13:34
add a comment |
I'm doing an API and now I'm consuming with ASP.NET MVC.
I have HomeController.cs, MailController.cs and Services.cs
HomeController
public async Task<IActionResult> Mirror()
{
WeatherService _weather = new WeatherService();
await _weather.GetMail();
var a = await _weather.GetWeatherToDay();
return View(a);
}
MailController
[HttpGet]
public async Task<IEnumerable<MailMessage>> GetEmail()
{
try
{
var client = new ImapClient("imap.gmail.com", 993, "ACCOUNT", "PASS", AuthMethod.Login, true);
var uids = client.Search(SearchCondition.All()).TakeLast(5);
var msg = client.GetMessages(uids, FetchOptions.HeadersOnly);
return msg;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Services
public async Task<string> GetMail()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost:44313/api/");
var response = await client.GetAsync("mail");
var responseToString = await response.Content.ReadAsStringAsync();
return responseToString;
}
}
Yes I know that my code it could be better...
I think that the problem is in the awaiters because when try to save result on variable response from Services get error and the executions it's continue... but without value.
Thanks! :)
c# asp.net api asp.net-web-api
I'm doing an API and now I'm consuming with ASP.NET MVC.
I have HomeController.cs, MailController.cs and Services.cs
HomeController
public async Task<IActionResult> Mirror()
{
WeatherService _weather = new WeatherService();
await _weather.GetMail();
var a = await _weather.GetWeatherToDay();
return View(a);
}
MailController
[HttpGet]
public async Task<IEnumerable<MailMessage>> GetEmail()
{
try
{
var client = new ImapClient("imap.gmail.com", 993, "ACCOUNT", "PASS", AuthMethod.Login, true);
var uids = client.Search(SearchCondition.All()).TakeLast(5);
var msg = client.GetMessages(uids, FetchOptions.HeadersOnly);
return msg;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Services
public async Task<string> GetMail()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost:44313/api/");
var response = await client.GetAsync("mail");
var responseToString = await response.Content.ReadAsStringAsync();
return responseToString;
}
}
Yes I know that my code it could be better...
I think that the problem is in the awaiters because when try to save result on variable response from Services get error and the executions it's continue... but without value.
Thanks! :)
c# asp.net api asp.net-web-api
c# asp.net api asp.net-web-api
asked Dec 27 at 13:11
sergibarca
21
21
put on hold as unclear what you're asking by Damien_The_Unbeliever, Servy, Peter Bons, EJoshuaS, Edric 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as unclear what you're asking by Damien_The_Unbeliever, Servy, Peter Bons, EJoshuaS, Edric 2 days ago
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
GetEmail
does not await anything
– Nkosi
Dec 27 at 13:20
4
"Confusion", "problem is in". You haven't actually told us what the problem is or what's confusing you. Are you getting unexpected results (what did you expect)? Compilation errors? Runtime errors? Something else?
– Damien_The_Unbeliever
Dec 27 at 13:34
add a comment |
GetEmail
does not await anything
– Nkosi
Dec 27 at 13:20
4
"Confusion", "problem is in". You haven't actually told us what the problem is or what's confusing you. Are you getting unexpected results (what did you expect)? Compilation errors? Runtime errors? Something else?
– Damien_The_Unbeliever
Dec 27 at 13:34
GetEmail
does not await anything– Nkosi
Dec 27 at 13:20
GetEmail
does not await anything– Nkosi
Dec 27 at 13:20
4
4
"Confusion", "problem is in". You haven't actually told us what the problem is or what's confusing you. Are you getting unexpected results (what did you expect)? Compilation errors? Runtime errors? Something else?
– Damien_The_Unbeliever
Dec 27 at 13:34
"Confusion", "problem is in". You haven't actually told us what the problem is or what's confusing you. Are you getting unexpected results (what did you expect)? Compilation errors? Runtime errors? Something else?
– Damien_The_Unbeliever
Dec 27 at 13:34
add a comment |
2 Answers
2
active
oldest
votes
MailController.GetEmail
does not await
anything.
I suggest you you refactor if no Task
is needed
[HttpGet]
public IActionResult GetEmail() {
var client = new ImapClient("imap.gmail.com", 993, "ACCOUNT", "PASS", AuthMethod.Login, true);
var uids = client.Search(SearchCondition.All()).TakeLast(5);
var msgs = client.GetMessages(uids, FetchOptions.HeadersOnly);//IEnumerable<MailMessage>
return Ok(msgs);
}
Also the calling code for Mirror
is not assigning a value;
//...
var data = await _weather.GetMail();
//...
This will behave identically to the OP's post, with the only difference being that exceptions are no longer wrapped in the returned task.
– Servy
Dec 27 at 14:57
add a comment |
I think you haven't use HttpResponseMessage in the service.
public async Task<string> GetMail() {
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://localhost:44313/api/");
using (HttpResponseMessage response = await client.GetAsync("mail")) {
if (response.IsSuccessStatusCode) {
var responseToString = await response.Content.ReadAsStringAsync();
return responseToString;
}
}
}
Why do you think this is functionally any different from usingvar
?
– mason
Dec 27 at 14:12
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
MailController.GetEmail
does not await
anything.
I suggest you you refactor if no Task
is needed
[HttpGet]
public IActionResult GetEmail() {
var client = new ImapClient("imap.gmail.com", 993, "ACCOUNT", "PASS", AuthMethod.Login, true);
var uids = client.Search(SearchCondition.All()).TakeLast(5);
var msgs = client.GetMessages(uids, FetchOptions.HeadersOnly);//IEnumerable<MailMessage>
return Ok(msgs);
}
Also the calling code for Mirror
is not assigning a value;
//...
var data = await _weather.GetMail();
//...
This will behave identically to the OP's post, with the only difference being that exceptions are no longer wrapped in the returned task.
– Servy
Dec 27 at 14:57
add a comment |
MailController.GetEmail
does not await
anything.
I suggest you you refactor if no Task
is needed
[HttpGet]
public IActionResult GetEmail() {
var client = new ImapClient("imap.gmail.com", 993, "ACCOUNT", "PASS", AuthMethod.Login, true);
var uids = client.Search(SearchCondition.All()).TakeLast(5);
var msgs = client.GetMessages(uids, FetchOptions.HeadersOnly);//IEnumerable<MailMessage>
return Ok(msgs);
}
Also the calling code for Mirror
is not assigning a value;
//...
var data = await _weather.GetMail();
//...
This will behave identically to the OP's post, with the only difference being that exceptions are no longer wrapped in the returned task.
– Servy
Dec 27 at 14:57
add a comment |
MailController.GetEmail
does not await
anything.
I suggest you you refactor if no Task
is needed
[HttpGet]
public IActionResult GetEmail() {
var client = new ImapClient("imap.gmail.com", 993, "ACCOUNT", "PASS", AuthMethod.Login, true);
var uids = client.Search(SearchCondition.All()).TakeLast(5);
var msgs = client.GetMessages(uids, FetchOptions.HeadersOnly);//IEnumerable<MailMessage>
return Ok(msgs);
}
Also the calling code for Mirror
is not assigning a value;
//...
var data = await _weather.GetMail();
//...
MailController.GetEmail
does not await
anything.
I suggest you you refactor if no Task
is needed
[HttpGet]
public IActionResult GetEmail() {
var client = new ImapClient("imap.gmail.com", 993, "ACCOUNT", "PASS", AuthMethod.Login, true);
var uids = client.Search(SearchCondition.All()).TakeLast(5);
var msgs = client.GetMessages(uids, FetchOptions.HeadersOnly);//IEnumerable<MailMessage>
return Ok(msgs);
}
Also the calling code for Mirror
is not assigning a value;
//...
var data = await _weather.GetMail();
//...
edited Dec 27 at 15:06
answered Dec 27 at 13:21
Nkosi
110k16117184
110k16117184
This will behave identically to the OP's post, with the only difference being that exceptions are no longer wrapped in the returned task.
– Servy
Dec 27 at 14:57
add a comment |
This will behave identically to the OP's post, with the only difference being that exceptions are no longer wrapped in the returned task.
– Servy
Dec 27 at 14:57
This will behave identically to the OP's post, with the only difference being that exceptions are no longer wrapped in the returned task.
– Servy
Dec 27 at 14:57
This will behave identically to the OP's post, with the only difference being that exceptions are no longer wrapped in the returned task.
– Servy
Dec 27 at 14:57
add a comment |
I think you haven't use HttpResponseMessage in the service.
public async Task<string> GetMail() {
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://localhost:44313/api/");
using (HttpResponseMessage response = await client.GetAsync("mail")) {
if (response.IsSuccessStatusCode) {
var responseToString = await response.Content.ReadAsStringAsync();
return responseToString;
}
}
}
Why do you think this is functionally any different from usingvar
?
– mason
Dec 27 at 14:12
add a comment |
I think you haven't use HttpResponseMessage in the service.
public async Task<string> GetMail() {
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://localhost:44313/api/");
using (HttpResponseMessage response = await client.GetAsync("mail")) {
if (response.IsSuccessStatusCode) {
var responseToString = await response.Content.ReadAsStringAsync();
return responseToString;
}
}
}
Why do you think this is functionally any different from usingvar
?
– mason
Dec 27 at 14:12
add a comment |
I think you haven't use HttpResponseMessage in the service.
public async Task<string> GetMail() {
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://localhost:44313/api/");
using (HttpResponseMessage response = await client.GetAsync("mail")) {
if (response.IsSuccessStatusCode) {
var responseToString = await response.Content.ReadAsStringAsync();
return responseToString;
}
}
}
I think you haven't use HttpResponseMessage in the service.
public async Task<string> GetMail() {
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://localhost:44313/api/");
using (HttpResponseMessage response = await client.GetAsync("mail")) {
if (response.IsSuccessStatusCode) {
var responseToString = await response.Content.ReadAsStringAsync();
return responseToString;
}
}
}
answered Dec 27 at 13:30
Shubham Sharma
37
37
Why do you think this is functionally any different from usingvar
?
– mason
Dec 27 at 14:12
add a comment |
Why do you think this is functionally any different from usingvar
?
– mason
Dec 27 at 14:12
Why do you think this is functionally any different from using
var
?– mason
Dec 27 at 14:12
Why do you think this is functionally any different from using
var
?– mason
Dec 27 at 14:12
add a comment |
GetEmail
does not await anything– Nkosi
Dec 27 at 13:20
4
"Confusion", "problem is in". You haven't actually told us what the problem is or what's confusing you. Are you getting unexpected results (what did you expect)? Compilation errors? Runtime errors? Something else?
– Damien_The_Unbeliever
Dec 27 at 13:34