15

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?

svick
  • 236,525
  • 50
  • 385
  • 514
Lawrence Wagerfield
  • 6,471
  • 5
  • 42
  • 84

4 Answers4

2

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.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • 1
    I am aware of its correct use and you're right in saying that fixing up non-async code resolves the issue. It is still a Code Contracts bug, however :) – Lawrence Wagerfield Jan 14 '13 at 11:55
2

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

1

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.

Manuel Fahndrich
  • 665
  • 4
  • 12
0

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.

slepox
  • 792
  • 4
  • 9
  • 2
    You certainly *can* have `async` method without `await`. It usually means that the author of the code doesn't understand how `async` works (and it produces a warning because of that), but it does work and I think it can be useful in some rare cases. – svick Jan 14 '13 at 00:33
  • There are some other subtle differences too. For example, an exception thrown from an async method will always be applied to the task, and never raise synchronously. – Lawrence Wagerfield Jan 14 '13 at 11:53