Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Contracts + Async in .NET 4.5: "The method or operation is not implemented"

I receive the following compilation error from ccrewrite when using Code Contracts 1.4.51019.0 in VS2012 on Windows 7 x64: "The method or operation is not implemented."

It appears to be caused by a combination of property accessors and the use of async methods which lack an inner await.

Reproduction steps:

Create a new class library with 'Full' Runtime Contract Checking enabled:

namespace CodeContractsAsyncBug
{
    using System.Threading.Tasks;

    public class Service
    {
        // Offending method!
        public async Task ProcessAsync(Entity entity)
        {
            var flag = entity.Flag;
        }
    }

    public class Entity
    {
        public bool Flag { get; set; }
    }
}

Has anyone else experienced this?

like image 616
Lawrence Wagerfield Avatar asked Oct 26 '12 14:10

Lawrence Wagerfield


People also ask

What happens when you call async method without await C#?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

How can create async method in C#?

A method in C# is made an asynchronous method using the async keyword in the method signature. You can have one or more await keywords inside an async method. The await keyword is used to denote the suspension point. An async method in C# can have any one of these return types: Task, Task<T> and void.

How does async task work in C#?

The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete.

Can actions be async C#?

The Action method only works on synchronous methods. For asynchronous methods it does not work. To understand why, we have to dive a bit deeper into asynchronous methods and how the compiler generates them. An asynchronous method in C# is split on the await calls into multiple synchronous parts by the compiler.


2 Answers

This appears to be fixed in version 1.5 of Code Contracts.

like image 167
Adrian Sureshkumar Avatar answered Oct 19 '22 23:10

Adrian Sureshkumar


An async method that does not await is usually indicative of a programming error. There is a compiler warning that will inform you of this situation.

If you wish to synchronously implement a method with an asynchronous signature, the normal way to do this is to implement a non-async method and return a Task, such as Task.FromResult<object>(null). Note that with this approach, exceptions are raised synchronously instead of being placed on the returned Task.

like image 45
Stephen Cleary Avatar answered Oct 20 '22 01:10

Stephen Cleary