none
Interface method returns task that is not awaited - no warnings RRS feed

  • Question

  • On our current project we are using dependency injection and utilizing the async-await pattern. This leads us to the following misuse:

    We have interfaces where methods return a Task (can't be declared async since it is on an interface). Then in a synchronous method one of these methods are used without awaiting the method (because the developer didn't notice that it actually returned a Task). This leads to a unwanted behavior where the method is used as fire and forget - where you'd want it to be run sequentially.

    Now the fix is easy, we can simply make the calling method async and await the task - the problem is to identify when it happens since no compiler warnings nor errors appear.

    Wednesday, November 8, 2017 9:24 AM

All replies

  • Do you mean like CS4014?
    Wednesday, November 8, 2017 9:52 AM
  • Hi Hartlev,

    Thank you for posting here.

    For your question, have you try the link provided by Brian Reichle?

    If you have any questions about this issue, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 10, 2017 5:22 AM
    Moderator
  • Well sort of - yes. Except you don't get one since the method I'm calling is on an interface and hence cannot be async. The method however still returns a Task and the underlying implementation is async which therefore results in the undesired behavior without any warnings.
    Friday, November 10, 2017 11:46 AM
  • So all they need to do is change the behaviour to match the documentation and make the determination based on the return type rather than the implementation :)

    You could add a warning yourself using Roslyn analyzers, I don't think the Roslyn guys are too receptive to the idea of changing the warning.

    Saturday, November 11, 2017 12:39 AM