12
votes

I'm using MVC 5, and I have helper extension methods to generate links and other urls based on Expression<Action<TController>>s that invoke controller actions. These expressions obviously aren't invoked in generating the view. They are only used for metadata.

Given this excerpt from my razor view,

@this.Form((AccountController c) => c.Register(null))

the compiler generates a warning:

Warning 1 Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

This warning doesn't seem appropriate because it could only apply if that lambda were invoked, which I know never happens.

Is there a way to suppress this? If not, I will probably make the action non-async.

2
Is making the action non-async really an option? Presumably you have made them async because you are in fact using await in the body?Kirk Woll
It is currently using await in the body, but I can just stop doing that.recursive
Can't you just do @this.Form(async (AccountController c) => await c.Register(null)), or add an overload that takes Expression<Func<Task>>?Stephen Cleary
@StephenCleary: I think I cannot use await because "Async lambda expressions cannot be converted to expression trees". Also, Expression<Func<Task>> would be tough, because I am ultimately using Microsoft.Web.Mvc.BuildUrlFromExpression, which requires Expression<Action<TController>>.recursive
I use an empty extension method called "Forget."Casey

2 Answers

9
votes

You can use #pragma in code blocks, as the code is then merged to a single source file, which is compiled, and when you get the warning from.

@{ #pragma warning disable }

and

@{ #pragma warning restore }

UDATE:

You can even disable specific warnings. See #pragma warning (C# Reference)

-1
votes

From MSDN :

To suppress specific warnings for Visual C# or F#

  1. In Solution Explorer, choose the project in which you want to suppress warnings.
  2. On the menu bar, choose View, Property Pages.
  3. Choose the Build page.
  4. In the Suppress warnings box, specify the error codes of the warnings that you want to suppress, separated by semicolons, and then rebuild the solution.

Best of luck ;-)