CSharpAddImportCodeFixProvider encountered an error and has been disabled - c#

I had my PC re-imaged for me. I have Visual Studio Version 14.0.25123.00 Update 2 installed on my computer. I'm getting this error when I try to use VS intellisense to reference another project.
CSharpAddImportCodeFixProvider encountered an error and has been disabled
I have two projects. One of them using namespace ProjectName.Web. And the other project using ProjectName.Web.Controllers. The provider crashes when I reference ProjectName.Web.Controllers, I believe because it is setup as a project. Any idea how to fix this?

I had the same issue on VS-2015 update 3.
I did was :
1) Closing visual studio
2) restarting as administrator

Happened to me when chose Add using System.Data.SqlClient automatically after typing using (SqlConnection...){} in DataLayer, in one of Repository classes.
Nothing helped (except creating new project), but I saw that the problem was with loading System.Data.SqlClient.dll file, although it existed in appropriate folder.
Found by trial and error that after removing Dependencies->Assemblies->System.Data.SqlClient from DataLayer (right click -> Remove, or just press Delete key when selected), I can Add System.Data.SqlClient without any errors.
Didn't try for other cases where CSharpAddImportCodeFixProvider encountered an error and has been disabled message appears, but the solution might be similar.
In my case the problem appeared probably due to .net Core version conflict or something like that, because the project was on external drive and created on another computer.
Edit: Also, some errors might be caused by .vs folder (hidden by default) inside Solution folder, especially if the project is moved between different computers. I know from experience that IntelliSense can seem to be broken and classes from other namespace would be unavailable although using namespace_name statement is present. The solution is to delete .vs folder or just avoid copying it with the project, as suggested here: https://weblog.west-wind.com/posts/2018/Aug/07/Fixing-Visual-Studio-Intellisense-Errors

There may be multiple reasons why this error occurs. So this answer may not apply to all situations, though it seems that it only occurs when another project is referenced.
The error does not occur for all statements. In my case I had the following code:
private System.Threading.Timer Timer;
public void Start()
{
Timer.Change(0, 60000);
}
As soon as intellisense would open for Timer.Change( the error occurred. Please note that I had no parameters at that point. If valid parameters are present there will be no error.
I could solve the issue by updating the version of the framework. I found out that both projects targetted different frameworks, resp. 4.5.2 and 4.6.
As long as the framework versions are different the error occurs. As soon as both are equal (either 4.5.2 or 4.6) the error no longer shows.
I have tested this with VisualStudioVersion = 14.0.25420.1 (Visual Studio Community 2015).
-- update --
I've reported this as a bug to Microsoft. Including steps to reproduce.

I end up with this same error.
what i did was to manually go and find the nuget packet for System.Data.SqlClient, installed and then invoke it in the class i was working on, like:
using System.Data.SqlClient;
Dont know if this is some kind of bug, cause it happens on a new blank project i create using visual studio 2019 community
hope it helps someone

Had the same problem solved by installing the sqlClient package from nuget package manager
right-click on the project choose manage nuget packages.. and go to browse tab and search for System.Data.SqlClient and install it. that easy :)

Judging from the label you were given, "CSharpAddImportCodeFixProvider", I'd guess your problem was due to Visual Studio trying to identify and/or correct a missing "using" statement at the top of your C# source code file in which you made reference to a class that needed it. The Visual Studio components that usually deal with this type of problem are Intellisense, or third-party syntax highlighting/correction plugins like JetBrains' ReSharper.
On second thought, I'm not quite sure it's Intellisense's fault as opposed to the plugin ReSharper's. That's to be determined.
I did file a similar bug report with Microsoft. The error in my case seemed to be a result of the Intellisense not knowing how to deal with a logic error in my own code (see https://connect.microsoft.com/VisualStudio/feedback/details/3133049).
In my case, I had inadvertently placed code for a method outside its class definition, though inside its similarly named namespace. Visual Studio 2015 Update 3 complained,
'GenerateVariableCodeFixProvider' encountered an error and has been
disabled.
The fix was to move my method back into its corresponding class definition, but it definitely brought a Visual Studio bug up to the surface.
Specifically, Visual Studio Intellisense had seen the line of code,
Response.Write("I did something");
placed in a method that was declared outside a class definition (i.e., inside a namespace, but inadvertently not inside its class). The "'FeatureLabel' encountered an error and has been disabled" error was then displayed in a yellow bar across the top of my editor window and an "Enable" button and an "Enable and ignore future errors" button were displayed next to it.
I believe that Intellisense (or ReSharper?) tried to automatically deal with the situation and attempted to generate a variable for the keyword, "Response", but it tripped trying to do so--which in turn caused the error that was displayed.

anyone trying this solution from here ?
https://developercommunity.visualstudio.com/content/problem/623872/add-import-not-working.html
the last reply solves my issue..
Tools > Options > Text Editor > C# > Advanced
turning off Suggest usings for types in NuGet packages

Related

Azure function in VSCode gives reference errors

I have set up a local Azure Function project in VSCode as per the instructions here https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-csharp?tabs=in-process
The default templated function that is created works when I run it in VSCode, but I am seeing reference errors in the VSCode editor as per screen shot.
When I go to the definition of the HttpRequest class for example, I see no Query method.
How can I fix these?
The options I selected were C#, .NET 6, HTTP Trigger, Anonymous, Add to Workspace
I have installed .NET 6 SDK, Azure Function Tools, C# extension, Azure function extension.
Thank you
Edit:
The exact message I get when I mouseover one of the reference errors is
The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13 ...
I ran into the same problem today. I have the C# extension installed in VS Code and noticed this line in the extension description:
When I set that flag to true (and restarted OmniSharp or VSCode), the issue went away :)
I tried to reproduce the issue:
Firstly, I have selected my workspace folder from the VSCode,
Created the function project using func init command in the VS Code terminal.
Create the function using func new command in the terminal, selected Http Trigger from the dropdown display of triggers and given the function name as Hello.
Click on the Hello.cs file, then you will get a side dialog box on the bottom of the same VS Code window.
Click on Restore.
OR
If you get two dialog boxes, then click on Restore and Yes so that it will restore the NuGet packages and functions dependencies required for that environment.
You can also see in the output terminal opens immediately after clicking restore option:
After adding those, you will get clean error-free UI like below:
Note:
If you have written the class/function manually in the code, please check that you renamed the Function class name both in the code as well in the file explorer like:
Updated Answer:
As per your edits and comments, I also installed .Net SDK Version 6 and checked the project in VS Code and I got the restore dialog box again with that red squiggly lines. After clicking on restore option, all red squiggly lines went out and this is the Proof-Of-Concept:
I did get this resolved, though I am not sure exactly how/ why, so i'll just post what I did in case it helps someone else.
Firstly I removed the Microsoft C# extension and re-installed it. I dont think this had any effect and it didnt seem to fix the issue, I am just mentioning it as I will mention everything I did.
When I googled the error that is in my Edit above The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13 I found some references to the MS Build tool/ library shipped with Visual Studio (not VS Code) in regards to .NET 6 and how the .NET 6 requires a certain version of the Build utility.
The recommendation was to upgrade Visual Studio (not VSCode), which I already had installed and that would update the MS Build.
I didnt really understand quite a bit of what I read, but I updated my install of Visual Studio Community 2019 in the hope that it would update MS Build and it seems to have worked.
I expect there is another way to do this without Visual Studio, but I chose this way as I had it installed anyway.
One last thing was I went to bed and had a sleep, then checked it again in the morning.
Cant really say for sure which of the three things worked :)

Error MSB3095 when compiling a Windows Form Application

When I try to compile any Windows form application project, I get an error saying:
\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2114,5): error MSB3095: Invalid argument. Culture is not supported.
\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2114,5): error MSB3095: Parameter name: name
\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2114,5): error MSB3095: x86 is an invalid culture identifier.
I was using VS 2017 when I stumbled across this error message. After searching the net, I saw that the problem I had was never asked by someone else. So I uninstalled anything related to Visual Studio and installed VS 2019.
With all my efforts to solve this problem going down, I checked if there was a conflict with my application's references and the MSBuild assembly files. This was also not the solution. Right now I am two weeks behind my schedule and yet still have no answers.
I tried changing platform target and target framework, hoping to see a difference but no; nothing has changed.
I also tried other application projects & targets. Here is the result:
Compiling a console application does not raise an error message.
Compiling a Windows Forms application, however raises the same error message with different projects.
The reason to error MSB3095 is actually not totally specific but it may be caused by the directory which your project is in, like it was in my case.
So simply move your project to another directory, that does not consist a non-ASCII character in. It will most likely solve it.

WPF error : The name does not exist in the namespace

I'm getting this problem building a WPF project
The name does not exist in the namespace
Everything I’ve looked at says you should
Change the namespace and the project name, build, change them back
Change the build from 86 to 64, build, and back Tried removing the
reference and letting ReSharper re-instate the reference Tried
excluding the file, build, then include the file again
None of the above worked.
I have checked that each referenced class has only one instance of it throughout the whole solution – ruling out named duplicates
The references in question are all references within the one DLL – all namespaces within this dll so it’s not like there is a cross dll issue of any sort.
Your thoughts or suggestions at this point are appreciated.
I to assume that You have this error in XAML file.
I thing it is very common problem with WPF and Visual Studio 2015.For example I have this error when I use DevExpress dlls
My suggestions is to try this:
Restart Visual Studio - it is weird but frequently it works. I don't now way it works.
Use F5 and run the program instead of build them (F6). Sometimes when You compile app the error disappear and app runs. It is also weird, but in my case it works
This not resolve the problem but it helps exclude the problem with IDE

"Internal error in the expression evaluator"

I've encountered a problem in expression evaluator of visual studio 2015 that says "Internal error in the expression evaluator", after some investigations I found that this is caused by an assembly that is loaded using reflection. This assembly wouldn't throw any exceptions but after that, vs exp evaluator will fail.
This is not the only assembly that I load, there are some good working ones that don't influent the evaluator.
To overcome this issue I had to check 'Menu > options > debugging > Use Managed Compatibility Mode' but this disables the 'Edit & Continue' feature, what can I do?
Properties of the causer assembly:
its name is the same as the main assembly
(i changed its name but nothing happened)
all of my projects are using dotNet 4.5
all root namespaces are same
(all of the assemblies are so)
Thanks!
That sounds like a bug in the expression evaluator. For a better workaround, instead of checking "Use Managed Compatibility Mode", check "Use the legacy C# and VB expression evaluators". This should enable you to continue using Edit and Continue as well as other features added within the last few releases.
In terms of the "Internal error in expression evaluator", can you please open an issue by going to Help -> Send Feedback -> Report a problem? That will help us fix the problem in future releases.
Just extending on the solution provided by Patrick Nelson. For Visual Studio 2015+ as inquired, the steps are as follows.
If you're debugging at the moment, this option will be unavailable. Stop the debugger.
Go to Tools -> Options
and here under the Options select Debug -> General and scroll down to find the ...legacy C# expression.. option:
More information is provided here:
Switching to Managed Compatibility Mode
Note: There are also some serious drawbacks that occur from switching to the legacy mode. Especially Runtime Reflection of the implemented Interfaces becomes almost an impossibility or extremely inconvenient. There are also other Reflection methods which will throw errors.
I finally figured out what created this problem in my Visual Studio!
The quick fix is to delete the debug object favorites from the "Documents/Visual Studio xx/Visualizers" folder and restart Visual Studio.
When you "pin" a variable in the debugger, Visual Studio saves a 'favorite' json object for it.
It appears that there is a bug in Visual Studio which corrupts the 'favorite' for some child variables that are dynamic in nature (not exactly sure of the conditions though).
For me checking "Use Managed Compatibility Mode" option worked. I was also seeing question marks when hovering over variables, instead of properties and values
I resolved this issue by simply resetting my visual studio settings by going to: to Tools->Import and Export Settings and selecting to reset to default settings
I had the same issue with VS2019. I ended up deleting my Documents/Visual Studio 2019 folder. Hope this might help someone, one day. Cost me a day.
PS. Probably not required to delete all, and of course not your projects (if they're in there), but in my case, everything in there was autogenerated by VS.
I of course tried all solutions mentioned here, and even reinstalling VS didn't work. Refactoring the class to another name was the 'trigger' which made me think there must be some cache, despite cleaning symbols and such didnt work.
In my case I was trying to evaluate lambda expression on List<> and had such error ("Internal error in the expression evaluator"). I was using VS2015, so lambda expressions were allowed. It turns out expression evaluator was lacking of Linq library. I added
using System.Linq;
to my current class and voilà! Lambda evaluated.
I encountered the "internal error in the expression evaluator" error when I was debugging in release mode instead of in debug mode. I had changed it to Release when publishing to production and forgot to change it back to Debug.
Check your use of the [DebuggerBrowsable] attribute; I found a minimal case in VisualStudio 2017 15.5 and posted it here.
In this particular case, the expression evaluator (EE) crash appears related to the [DebuggerBrowsable] attribute applied to a property overriding a field of the same name. This will account for some percentage of the cases that people are experiencing out there, but there's no way of knowing how many are due to this specific issue until it gets fixed.
The full and complete demonstration example is shown in the image (and included below for good measure)
Machine-readable copy of the code in the image:
using System;
using System.Diagnostics;
class Program { static void Main() => new _derived(); }
abstract class _base
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public Object trace;
};
class _derived : _base
{
public _derived() => Debugger.Break(); // <-- vs2017 EE crash when stopped here
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
new public Object trace => base.trace;
}
In my case, I had 2 same dll files in 2 different folders (seems, one dll was not correct). Deleting the .dll and rebuilding solution solved my issue.
In my case the data I was attempting to inspect was extremely large, a string which unexpectantly had hundreds of megabytes of data in it. The issue wasn't apparent when the amount of data being inspected was reasonable.

ArgumentException when using TeamFoundation Builds

I am using Visual Studio online to manage my sources. I am also using Continous Integrations which means my source is compiled in the cloud on the TFS(Visual Studio Online).
I have my own build process template and also a few code activities. Since now everything works fine. After I edited the activities and also the template, everytime I start a build, I get the following error:
TF215097: An error occurred while initializing a build for build definition \BuildTest\BuildTasks:
Exception Message: Expression of type 'Microsoft.TeamFoundation.Build.Workflow.Activities.AgentReservationSpec' cannot be used for return type 'Microsoft.TeamFoundation.Build.Workflow.Activities.AgentReservationSpec' (type ArgumentException)
Exception Data Dictionary:
MS.TF.Diagnostics.Logged = True
A strange side effect is, that the order of the arguments of the build template in Visual Studio is now mingled and there are also some arguments, which are not defined in the template. This happens not only for my custom template, but also for the default templates from Microsoft.
Can anybody help me?
The cause of such errors is due to two versions of the assembly providing the erroneous type being visible simultaneously. The value being assigned from is from one and the value being assigned to is from the other.
This happened to me when I accidentally checked in a load of Microsoft.TeamFoundation assemblies into my custom activities location in source control. Removing these assemblies made the issue go away for me.
Also worth looking out for is if you have more than one version of the TFS API installed on your build controller (multiple versions of Visual Studio will do this). You may need to use explicit versions (full strong name syntax) in the references to TFS assemblies in your projects to ensure the correct API version is loaded.
I had the same problem with our local TFS instance. I removed all my DLLs from the CustomActivities-Folder in TFS and the error was gone.
Now second steop is to identify the CustomAction causing the problem ...

Categories

Resources