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:
public void ConfigureServices(IServiceCollection services) {
services.AddMemoryCache();
}
It is flagging the _cache
field with:
Field HomeController._cache is never assigned to, and will always have its default value 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?
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.
However, I was notified of Visual Studio 2017 update version 15.3.3 when the environment reloaded, which I installed.
After re-starting Visual Studio post-update, this situation is no longer occurring.
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.
I had the same case on VS2015. In my case Build -> Rebuild Solution
resolved the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With