Azure function in VSCode gives reference errors - c#

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 :)

Related

CSharpAddImportCodeFixProvider encountered an error and has been disabled

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

Windows Service: The module was expected to contain an assembly manifest

I've got a C# program that was built as a Windows service. I tested this last week and it worked fine, but today I'm getting an error.
When I try to install using installutil.exe, I get the error:
Could not load file or assembly [file name] or one of its dependencies. The module was expected to contain an assembly manifest.
Rebuilding the project had no effect. I also noticed in task manager that the service is still listed and running. On the chance that might be interfering with the installation, I tried uninstalling via installutil, but got the same error message back again.
I've seen many other questions related to this error, but most of them have either no answer, or a very specific answer related to their unique situation. Has anybody had this happen, and how did you fix it? Any ideas are appreciated.
Update:
I successfully uninstalled the service using the "sc delete" command via command prompt, but I'm still getting the same error from installutil on trying to reinstall.
Update 2:
Just for the sake of trying something, I deleted the bin folder from the project, reopened the solution, and did a clean and build. It still won't install, but now I've got a different message:
This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
Question:
What would cause there to be a mismatch in runtimes? As far as I know, I've only used .NET Framework 4.5 and haven't deviated. Is there a value I should be looking at / changing to fix that?
Update 3:
Here's what I've tried so far:
One answer suggested the version of installutil being used matters. I'd been running v2.0.50727, but there's also a v4.0.30319 I hadn't thought to try. Running this resulted in:
An attempt was made to load a program with an incorrect format...
I'd be willing to work on that error instead, if it would solve the problem. However, I'm compelled to think that's not the problem since I'd been successfully running v2.0.etc just before the first error popped up.
Inside App.config, there's a line "supported Runtime version=v4.0". I switched this to 4.5, but it had no effect good or bad.
I tried switching the target framework (under project properties) down to 4.0, but this cause an error with:
Some NuGet packages were installed using a target framework different from the current target framework, etc...
This was referring to the EntityFramework, and I'd rather not start messing with NuGet again unless it's the only way (last time it was a nightmare).
I tried installing the service on the target server (the machine it will eventually be deployed to) and got the same error. This leads me to think it's a problem with the project / solution and not the pc.
Looks like a assembly loading problem. Use the Fuslogvw.exe utility. Start it from the Visual Studio Command Prompt. Click the Settings button and click the "Log binding failures to disk" radio button. Switch back to VS and start the program and wait for the exception to occur. Back to Fuslogvw, click the Refresh button and double-click the added entry. It will show you the file it found.
One another possibility is to trying to load a .NET 4 assembly with an EXE that asked for CLR version 2. That requires an app.exe.config file that forces CLR 4 to be used.

C# - Gtk.ComboBox Does Not Contain a Definition for 'NewText'

I'm writing a C# Gtk# application under Mono/MonoDevelop in Linux Mint 17. I have a ComboBox on a form with 3 items in it. I was able to place it and such fine however, I get an error in the designer code stating that the ComboBox doesn't contain a definition for 'NewText'. The Mono documentation at docs.go-mono.com says that this NewText method is how you add items, however, apparently mine doesn't have it.
My MonoDevelop version is 4.0.12
The projects target Gtk# version is 3.0
I have the following references and their versions referenced in the project
And finally, the designer code
//top of file declaration
private global::Gtk.ComboBox framestyle_Val;
//further down, this defines all the items
this.framestyle_Val = global::Gtk.ComboBox.NewText ();
this.framestyle_Val.AppendText (global::Mono.Unix.Catalog.GetString ("Single Sprite"));
this.framestyle_Val.AppendText (global::Mono.Unix.Catalog.GetString ("Left/Right Sprites"));
this.framestyle_Val.AppendText (global::Mono.Unix.Catalog.GetString ("Left/Right/Upside-Down Sprites"));
It's worth noting that apparently there's no AppendText definition/method either but I assume that'll be straightened out once we get this one straightened out.
Any ideas? I'm confused myself. Thanks in advanced,
Mike
I had the same problem. MonoDevelop seems to be unable to generate proper code to use with Gtk#3.
You may want to install Gtk#2, this will not override the GTK#3 installation, and target your project with this version to be able to use MonoDevelop's visual design abilities.
And maybe you will need to create the clean GTK#2 project once again.
If you have found a solution to use GTK3, please give me an idea.

Strange exception suddenly appears when trying to install a setup project, need help big time

Using Visual Studio 2010 to build a setup project that installs a Windows Forms application .Net 4.0 C#. It has worked fine for ages but now when I'm trying to install the finished setup file, I'm getting this error message:
Error 1001. Unable to get installed types in the "Path" assembly. -->
Unable to load one or more of the requested types. Retrieve the
LoaderExceptions property for more information.
I have been searching for answers for over 4 hours now without finding anything. This problem just came without me doing anything. Last time I build the install file was like 2 weeks ago and there was NO problem at all. I haven't deleted any reference or any code that have anything to do with the setup project.
How could this problem appear from nothing and more important, how do I fix it?
Based on the error message in your second comment, it appears that your SysDir.exe assembly has been added as a Custom Action with the InstallerClass property set to true, but either no installer classes could be found in the exe or the exe could not be loaded due to missing dependencies.
You can see the list of Custom Actions by right-clicking on the installer project, selecting View and then Custom Actions.
If your exe does not can an installer class, then you can remove it from the list of custom actions.
If it does contain an installer class, then the issue is going to be missing dependencies. If fuslogvw doesn't work for you (it has always helped resolve this kind of issue for us), you can carefully review the list of references in the exe's project and compare them to what is listed in the installer project.
The other trick that we use is to examine the install directory while the error message is displayed on the screen. We can often see that DLLs are missing by doing this, usually because the path was entered incorrectly in the DLL entry within the installer project or because a condition was set incorrectly.
Have the same error today. For me it was the project type of the class library.
I noticed that the pucture on the guide I was following had selected Class Library (.NET Framework) instead of just Class Library.
Creating the correct project type fixed the error.
https://nhvu1988.com/posts/how-to-create-msi-installer-using-vs-installer/

Strange dll error message

For about 2 weeks now, I have been unable to run any UnitTests (built in VS unit tests) for a project. Previously everything worked fine. The error message is:
Could not load file or assembly 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\MyProjectName.XmlSerializers.dll" or one of its dependencies.
The project references System.Xml.Serialization and uses the XmlSerializer class; just like many other classes/projects I've written. For some reason, only this project is affected. It builds fine, runs fine, I just can't run my unit tests.
I've checked the directory, and all the dlls in that directory are Microsoft dlls. The dll that it is looking for obviously is not a Microsoft dll.
Anyone have any ideas?
Edit:
It apparently has something to do with using the XmlSerializer and it generating that file automatically instead of using sgen.exe. Here is a link to the MSDN article. From what I've been able to find, it has something to do with using the serializer with generics. None of the sources I've found seem to offer any way to make it actually work.
First enable loader logging (using FUSLOGVW.exe from the SDK) to confirm what is not being found.
Then use Reflector on all your assemblies to find the one that is trying to load the non-existent assembly. If you find no such assembly it must be being loaded dynamically, in which case attaching to AppDomain.AssemblyResolve should allow you to identify where.
try to copy all your source files somewhere, then delete the project and try to make it from scratch. Maybe something happened with project dependencies
Is your computer 64bit? I got the same error when trying to run a 64bit dll with NUnit that was set to work as an x86 assembly (using corflags).
You can probably find out from the error message (use FUSLOGVW.exe lick Richard suggested).
If that is the case you can either sent the dll or NUnit to run as the correct assembly using corflags.
Solution
As it turns out, the problem was with VMWare. I installed the latest version of VMWare, and it installed it's tools to debug in a VM. Something it installed or changed caused this problem. When I uninstalled VMWare, the problem went away. So, I reinstalled VMWare without installing it's debugging capabilities and the problem did not come back.
Workaround:
I still have no idea why this problem suddenly started occurring, but I found a hack to make it work.
I had to go to project properties => Build Events and add this line to the Post-build event command line:
"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sgen.exe" "$(TargetPath)" /force
This forces VS to generate the file. I then had to copy that file manually to the directory it was looking for it in:
"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies"
Now it I can run my tests and step through them. The problems I have now are 1) I have to remember to copy the dll to that directory every time I change something in the classes that I am serializing, and 2) I now get a ThreadInterruptedException when a test finishes running; thus 3) I can only run one test at a time.
Not a good solution, but at least I can limp through. Unfortunately, redoing everything, as Nikita Borodulin suggested, is not an option.

Categories

Resources