为什么将Postsharp与异步方法一起使用时,编译器为什么要优化参数 [英] Why is the compiler optimizing out the parameters when using Postsharp with an async method

查看:104
本文介绍了为什么将Postsharp与异步方法一起使用时,编译器为什么要优化参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Web API缓存,该缓存通过使用方面[Cache]添加到特定方法中。一切都很好。后来我将此方法更改为异步方法,但此后编译器开始抛出以下警告:

I started using a Web API cache which I add to particular methods by using an aspect [Cache]. Everything worked great. Later on I changed this method to be async, but since then the compiler started throwing following warnings:


方法' GetTree(System.String,
System.String,System.String [])'已由
编译器优化,因此该方面将不可用。禁用编译器
优化来访问参数。

The parameter 'region' of method 'GetTree(System.String, System.String, System.String[])' has been optimized out by the compiler and will not be available to the aspect. Disable compiler optimizations to access the parameter.

在这里,您可以看到有关我如何使用Postsharp的示例:

Here you can see an example of how I am using Postsharp:

[Cache]
public async Task<IEnumerable<Node>> GetTree(            
[FromUri] string region,
[FromUri] string language,
[FromUri] string[] networks)
{
 ...
 await ...
}

我需要做什么才能摆脱

推荐答案

如果异步内部没有使用这些参数,则C#编译器优化将从状态机类中删除这些参数。方法。无论是否使用PostSharp,都会发生这种情况。 PostSharp向您显示警告,通知您无法在方面内访问已删除的参数。

The C# compiler optimizations remove the parameters from the state machine class if these parameters are not used anywhere inside the async method. This happens regardless of whether you use PostSharp or not. PostSharp shows you the warning to notify that the removed parameters cannot be accessed inside the aspect.

建议升级到最新版本的PostSharp-较新的版本可以处理此问题通过将缺少的参数重新引入状态机来解决问题。

It's recommended to upgrade to the latest build of PostSharp - the newer versions can handle this issue by re-introducing the missing parameters back into the state machine.

如果无法升级,则解决方法是禁用优化代码 在发布属性的项目属性的构建页面中(默认情况下,调试构建是禁用的)。

If you cannot upgrade, then the workaround is to disable "Optimize code" in the build page of the project properties for release builds (it's disabled for debug builds by default).

这篇关于为什么将Postsharp与异步方法一起使用时,编译器为什么要优化参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆