简体   繁体   中英

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?

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 .

这似乎在代码合同1.5版中得到修复。

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.

我相信async关键字就代表了这一点 - 要么在代码期间等待,要么生成一个Task并在调用方法时处理,或者你需要显式返回一个Task。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM