-1

Looking forward to adding authentication to the MVC 5 Boilerplate template, The next piece of code worked well in its own original sample project, but when integrated its content into the Boilerplate template, and tried to register a new user, something become conflicting and a browser exception appears, pointing to the following "await" line:

// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
   if (ModelState.IsValid)
   {
      var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
      var result = await UserManager.CreateAsync(user, model.Password);
      if (result.Succeeded)
      {
         string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account");
         ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
                         + "before you can log in.";
         ViewBag.Link = callbackUrl;
         return View("Info");
      }
      AddErrors(result);
   }
   return View(model);
}

I've read in many places that when an async issue happens, people advise to make it synchronous, but for my case many things would become incompatible.I wonder how to keep this method async as it was originaly in the template,

Sami-L
  • 5,553
  • 18
  • 59
  • 87
  • 1
    First, it's a warning, not an error. It doesn't prevent your application from working, it's just a sign that you're doing something weird. Second, it's entirely right - your `GetItems` method doesn't have any `await`s, so the `async` keyword in the declaration is useless, and the method will be 100% synchronous - there's no reason for it to return a `Task` either. – Luaan Nov 02 '15 at 19:04
  • The browser gives an exception and I cannot ignore that, please read the entire question, the template discussion included, please. – Sami-L Nov 02 '15 at 19:07
  • Are you compiling with warnings as errors? – Lee Nov 02 '15 at 19:13
  • @Lee 'Treat Warnings as Errors' is set to 'None' – Sami-L Nov 02 '15 at 19:14
  • 1
    Can you please update your post with [MCVE] and exact error you are facing. Compile time warning mentioned in the post is very unlikely to be an error shown when you run the site. – Alexei Levenkov Nov 02 '15 at 19:44
  • 1
    Well, you didn't post any exception, so how are we supposed to help you with the exception you're having? The message you posted is a compiler warning, not a runtime exception. And it has nothing to do with the highlighted line (unless it's somehow involved in a JIT compilation), so it's more likely that your real error is still there, hidden somehow. Remove the offending piece of code (remove the `async` from your definitely synchronous method), and try again - maybe you'll get the real error. – Luaan Nov 03 '15 at 08:33

2 Answers2

3

Your GetItems doesn't need to await, so it shouldn't be async. Just change the signature to:

private List<SyndicationItem> GetItems(CancellationToken cancellationToken)

and change the calling code from:

await GetItems(token);

to:

GetItems(token);
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • And what about if I use the TaskCompletionSource as proposed by Andrii, it worked for me for the FeedServices.cs but I am still getting the same issue for the AccountController method. – Sami-L Nov 02 '15 at 21:10
  • I've read many posts where it is said to transform asynchronous methods to synchronous, I wonder why templates authors made them async. The accountController was made by Rick Anderson: http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset – Sami-L Nov 02 '15 at 21:45
  • @Sami-L You'd want an asynchronous controller if you have asynchronous work to do. You don't have any asynchronous work that you're doing. – Servy Nov 02 '15 at 21:48
  • @Servy You're right, but I just try to keep original templates as they was, all what I am trying to do is to make them working together, since they could be a good starting point for an MVC project. And if I want to make all synchronous I'll have to revolutionize all the methods in the templates, and many things will become incompatibles. – Sami-L Nov 02 '15 at 22:03
  • @Sami-L: You should not use `QueueUserWorkItem` on ASP.NET. The templates are asynchronous because *most* controller actions perform I/O, which can be made asynchronous. But not all of them can or should be asynchronous. – Stephen Cleary Nov 03 '15 at 00:43
  • 2
    @Sami-L The `async` keyword is not part of the method signature. It just allows you to use `await` in the method body, nothing else. It also wraps any exceptions and returns in a task, but you can do that manually. So if it's part of the *public* interface, it's perfectly fine to remove the `async` while keeping the `Task` return value (use `Task.FromResult(...)` to wrap the real return value in a completed task). If it's just some private method, don't bother, it's a lie anyway; you're saying "sure, this method is totally asynchronous", when it actually isn't. – Luaan Nov 03 '15 at 08:40
  • @Luaan The 'await' operator can only be used within an async method , so if I remove the 'async' keyword I'll need to remove the 'await' then I'll need to remove the task, and that way the method won't be asynchronous any more. This method is working fine in its original template, but when I added the template content to the boilerplate content, as said before, something become conflicting, I do not know what it is, and I am somewhat confused on how to report this kind of issue here. – Sami-L Nov 03 '15 at 10:23
  • 1
    @Sami-L: Wait, so now your method *is* using await??? In your original question, you were asking about a compiler warning that you were getting because your method *wasn't* using await. The question is 100% different now. – Stephen Cleary Nov 03 '15 at 13:26
  • @Stephen I am sorry if I was not enough clear but I do all my best to clarify the situation, but my initial question is not different, from the begining I wanted to keep the original template as it was, always asynchronous, I avoid to change it to synchronous. Please I need to talk to you in the ASP.net MVC chat room ? before coming back here ? – Sami-L Nov 03 '15 at 13:37
  • @Sami-L: I recommend you delete this question and ask a new one. If you are getting a compiler warning/error, post it. If you are getting an exception, post the exception type, message, and stack trace. – Stephen Cleary Nov 03 '15 at 15:31
1

Thanks to Nikita1315 help, I could find that the next configuration syntax was missing in the startup.cs file:

       ConfigureAuth(app);

so the startup class would look like:

    public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureContainer(app);
        ConfigureAuth(app);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    }
}

So, as soon as I added this configuration, and without modifying any async method, I could register my first user.

Sami-L
  • 5,553
  • 18
  • 59
  • 87