Compiler warning CS4014 (calling async method without awaiting result) is not emitted as a warning during build when the called method is in a referenced assembly.
When the called method is in the same assembly the warning is correctly emitted.
The compiler warning is signaled in Visual Studio when both projects are contained in the same solution.
The difference seems to be caused by the compiler having only the compiled referenced assembly and Visual Studio having the source code to both assemblies.
The question is: why have these two different behaviors? And is there any way to have the CS4014 warning emitted during compilation?
To replicate this behavior setup two class libraries, both having one code file:
TestClassLibrary1
public class Class1
{
public static async Task<string> DoSomething()
{
return await Task.FromResult("test");
}
}
TestClassLibrary2 (referencing TestClassLibrary1)
public class Class2
{
public void CallingDoSomething()
{
Class1.DoSomething();
}
}
Compiling these projects will complete without warnings. Opening them in the same solution in Visual Studio will result in 1 error being shown in the Error List and a red squiggly line under Class1.DoSomething()
.