简体   繁体   English

.NET 4.5中的代码约定+异步:“方法或操作未实现”

[英]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: 在Windows 7 x64上的VS2012中使用Code Contracts 1.4.51019.0时,我从ccrewrite收到以下编译错误: "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 . 它似乎是由属性访问器和使用缺少内部awaitasync方法的组合引起的。

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. awaitasync方法通常表示编程错误。 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) . 如果您希望使用异步签名同步实现方法,则执行此操作的常规方法是实现非async方法并返回Task ,例如Task.FromResult<object>(null) Note that with this approach, exceptions are raised synchronously instead of being placed on the returned Task . 请注意,使用此方法,异步会同步引发,而不是放在返回的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. 我建议你在最新的安装程序上再次尝试你的代码,如果你还有问题,请提供一个完整的repro。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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