[英]Constructor DI - Field is never assigned to, and will always have its default value
I have the following code: 我有以下代码:
private IMemoryCache _cache;
public HomeController(IMemoryCache memoryCache) : base() {
this._cache = memoryCache;
}
ASP.Net Core
is handling the injection of the IMemoryCache
instance via: ASP.Net Core
方式处理IMemoryCache
实例的注入:
public void ConfigureServices(IServiceCollection services) {
services.AddMemoryCache();
}
It is flagging the _cache
field with: 它正在标记
_cache
字段:
Field HomeController._cache is never assigned to, and will always have its default value null
字段HomeController._cache永远不会分配给,并且将始终具有其默认值null
It's not causing any errors, it is just distracting in the editor. 它没有造成任何错误,只是在编辑器中分散注意力。
What is the way to go about convincing Visual Studio 2017 that I am in fact assigning this field a value, via the constructor? 有什么方法可以说服Visual Studio 2017我实际上是通过构造函数为这个字段赋值?
Code (because the error overlaps it): 代码(因为错误重叠):
With the editor showing the error: 编辑器显示错误:
At runtime showing the valid instance being injected: 在运行时显示正在注入的有效实例:
Edit: 编辑:
For the comments suggesting to use: 对于建议使用的评论:
private IMemoryCache _cache = null;
... this makes no difference, same exact result. ......这没什么区别,同样的确切结果。
Restarting Visual Studio 2017 and rebooting did not resolve the issue. 重新启动Visual Studio 2017并重新启动无法解决问题。
However, I was notified of Visual Studio 2017 update version 15.3.3 when the environment reloaded, which I installed. 但是,当我重新加载环境时,我收到了Visual Studio 2017更新版本15.3.3的通知。
After re-starting Visual Studio post-update, this situation is no longer occurring. 重新启动Visual Studio后更新,这种情况不再发生。
I doubt the update actually did anything related to this problem, it's more likely something in my VS configuration was off and the update put things back in place. 我怀疑更新实际上做了与此问题相关的任何事情,更可能是我的VS配置中的某些内容已关闭且更新将事情重新放回原位。
Cleaning your solution should solve this. 清理解决方案应解决此问题。 In VS2017 menus:
Project -> Clean Solution
. 在VS2017菜单中:
Project -> Clean Solution
。
I had the same case on VS2015. 我在VS2015上遇到了同样的情况。 In my case
Build -> Rebuild Solution
resolved the issue. 在我的案例中,
Build -> Rebuild Solution
解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.