Lawrence Wagerfield
Lawrence Wagerfield

Reputation: 6611

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?

Upvotes: 15

Views: 1561

Answers (4)

Adrian Sureshkumar
Adrian Sureshkumar

Reputation: 676

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

Upvotes: 2

Manuel Fahndrich
Manuel Fahndrich

Reputation: 665

Over the last several months, we have fixed many issues with rewriting async methods. I would suggest you try your code again on the latest installer and if you are still having a problem, please provide a complete repro.

Upvotes: 1

Stephen Cleary
Stephen Cleary

Reputation: 457127

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.

Upvotes: 2

slepox
slepox

Reputation: 812

I believe async keyword just stands for that - either you have a await during the code, by which it will be generated a Task and handled when the method is called, or you need to return a Task explicitly.

Upvotes: 0

Related Questions