locked
issue from making all my methods async RRS feed

  • Question

  • User1034446946 posted

    Hi

    I am trying to make all my methods async, however i have a method which is run inside and outside the constructor, but inside the constructor is has the error saying await methods should be decorated with async which isn't an options.

    Does that mean its impossible to have all methods async?
    Does that mean I need to have two methods,one async and one not? (is this good practice, it means i have to manage the same code twice)

    Saturday, February 22, 2020 2:45 AM

Answers

  • User753101303 posted

    The purpose of the constructor is to "new" the object and you usually avoid doing complex things from a constructor.

    Alternate options are :
    - to use a static method such as Bitmap.FromImage which creates a bitmap and then load the image to finally return the newly initialized object
    - or you could differ this actual step until really needed (calling an internal "EnsureInitiliazed" object as needed)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 26, 2020 9:36 AM
  • User475983607 posted

    EnenDaveyBoy

    I don't "need" an async constructor, i just need a method to be run inside the constructor, thought i'd ask the question, glad i have and i appriciate all the responses.

    You're not following standards or recommendations.  A constructor returns Void.  One of the rules with asycn is methods cannot return void.  How will you know the class is initialized from an asynchronous method? What if there is an exception?  Rather than trying to force an unrecommended design, I recommend following standards.

    https://blog.stephencleary.com/2013/01/async-oop-2-constructors.html

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 26, 2020 11:55 AM

All replies

  • User753101303 posted

    Hi,

    You can't have an async coonstuctor. Usually you try to keep it as a simple as possible. If the opration is quite complex it could be exdposed maybe as a emthod instead.

    Also you shouldnt make all your methods asyncs at any cost but rather when it make sense ie if you have to call an async API..

    Saturday, February 22, 2020 7:06 AM
  • User475983607 posted

    I agree.  If you need an async constructor than there is something wrong with the design.  Perhaps you need a factory pattern?

    Saturday, February 22, 2020 12:37 PM
  • User-854763662 posted

    Hi EnenDaveyBoy ,

    however i have a method which is run inside and outside the constructor, but inside the constructor is has the error saying await methods should be decorated with async which isn't an options.

    Did you mean that you get the error when you use await to call a async method in the constructor?

    The await operator only in a method, lambda expression, or anonymous method that is modified by the async keyword.

    Try to call asynchronous methods directly in the constructor without using await operator .

    Best Regards,

    Sherry

    Monday, February 24, 2020 9:03 AM
  • User1034446946 posted

    is doing something like

    public class someClass
    {
    public someClass()
    {
    Methoda();
    }
    public bool Methoda()
    {
    return true;
    }
    public bool MethodaAsync()
    {
    return await Task.FromResult(Methods);
    }
    }
    
    

    The way to go?

    Monday, February 24, 2020 8:04 PM
  • User-854763662 posted

    Hi EnenDaveyBoy ,

    Make some modification like this

    public class someClass
    {
       public someClass()
      {
         MethodaAsync().Result;
      }
       
       public async Task<bool> MethodaAsync()
      {
          var model = await _context.Card.SingleOrDefaultAsync(c => c.Id == 1);
          if(model!=null)
          {
              return await Task.FromResult(true);
          }
          return await Task.FromResult(false);
      }
    }

    Best Regards,

    Sherry

    Tuesday, February 25, 2020 2:07 AM
  • User-474980206 posted

    public async Task<bool> MethodaAsync()
      {
          var model = await _context.Card.SingleOrDefaultAsync(c => c.Id == 1);
          if(model!=null)
          {
              return await Task.FromResult(true);
          }
          return await Task.FromResult(false);
      

    this code is more complex than it needs to be. Using async when not required add extra overhead. It should be:

      public async Task<bool> MethodaAsync()
      {
          var model = await _context.Card.SingleOrDefaultAsync(c => c.Id == 1);
          if(model!=null)
          {
              return true;
          }
          return false;
      }

    Tuesday, February 25, 2020 5:28 PM
  • User753101303 posted

    IMHO do things the other way round ie :
    - ie if your method calls itself an async API you need to use await and then your method needs to be async
    - if not just leave your method as is.

    For now it seems you are trying to "force" all methods to be async which is IMO entirely useless.

    Tuesday, February 25, 2020 5:47 PM
  • User1034446946 posted

    Thanks for all the replies, this article is the microsoft website says:

    https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming

    As you convert synchronous code to asynchronous code, you’ll find that it works best if asynchronous code calls and is called by other asynchronous code—all the way down (or “up,” if you prefer).

    and

    “Async all the way” means that you shouldn’t mix synchronous and asynchronous code without carefully considering the consequences.

    I know the article is 2013, is that considered the case anymore?

    Tuesday, February 25, 2020 7:15 PM
  • User475983607 posted

    You're over thinking this and at the same time over simplifying the pattern.   Basically hardware and firmware within a computer is all asynchronous.  When you are invoking hardware and firmware resources, which is an abstraction to the developer, you should use async so your not forcing a thread to wait for the response when the thread can be doing something else.

    Assigning async to to methods that are not async is a waste of resources.

    IMHO, needing an async constructor means you have design issues.  A constructor should not need to access a REST service or DB to initialize itself. If you have this situation, you might need a factory pattern.

    Tuesday, February 25, 2020 7:41 PM
  • User1034446946 posted

    You're over thinking this and at the same time over simplifying the pattern.   Basically hardware and firmware within a computer is all asynchronous.  When you are invoking hardware and firmware resources, which is an abstraction to the developer, you should use async so your not forcing a thread to wait for the response when the thread can be doing something else.

    Assigning async to to methods that are not async is a waste of resources.

    IMHO, needing an async constructor means you have design issues.  A constructor should not need to access a REST service or DB to initialize itself. If you have this situation, you might need a factory pattern.

    I don't "need" an async constructor, i just need a method to be run inside the constructor, thought i'd ask the question, glad i have and i appriciate all the responses.

    Wednesday, February 26, 2020 12:52 AM
  • User753101303 posted

    The purpose of the constructor is to "new" the object and you usually avoid doing complex things from a constructor.

    Alternate options are :
    - to use a static method such as Bitmap.FromImage which creates a bitmap and then load the image to finally return the newly initialized object
    - or you could differ this actual step until really needed (calling an internal "EnsureInitiliazed" object as needed)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 26, 2020 9:36 AM
  • User475983607 posted

    EnenDaveyBoy

    I don't "need" an async constructor, i just need a method to be run inside the constructor, thought i'd ask the question, glad i have and i appriciate all the responses.

    You're not following standards or recommendations.  A constructor returns Void.  One of the rules with asycn is methods cannot return void.  How will you know the class is initialized from an asynchronous method? What if there is an exception?  Rather than trying to force an unrecommended design, I recommend following standards.

    https://blog.stephencleary.com/2013/01/async-oop-2-constructors.html

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 26, 2020 11:55 AM
  • User1034446946 posted

    gone for the static class, many thanks for the hep, and appriciate that last link really good read.

    Thursday, February 27, 2020 12:27 AM