Nuget problems with Azure Function 1.x and VS17 - visual-studio

I'm having some problems with my project, I don't know where it went wrong exactly but now my Azure Function 1.x project won't build. It's critical I get it building again :-/.
This is a .NET Framework 4.6.2 project with Microsoft.NET.Sdk.Functions 1.0.35 (latest before 2.x).
I've got a couple of usings that it does not seem to able to find, and Function itself has this error:
Error CS0246 The type or namespace name 'FunctionNameAttribute' could not be found (are you missing a using directive or an assembly reference?)
So my [FunctionName] has red squiggly lines and I don't understand why, it should be in Microsoft.NET.Sdk.Functions? I've tried to clean and rebuild the project.
I've tried to reinstall my packages by command Update-Package -reinstall -Project MyVeryCriticalProject
PM> Update-Package -reinstall -Project MyVeryCriticalProject
No package updates are available from the current package source for project 'MyVeryCriticalProject'.
Executing nuget actions took 0 ms
Time Elapsed: 00:00:10.4211309
It also complains about missing namespace:
But my csproj file has these included as I have installed them in nuget:
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="2.3.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Core" Version="2.3.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.ServiceBus" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.35" />
I have VS2019 installed and I am going to move this project over to Azure Functions 3.x and .NET Core but right now I need this to compile :-/ Any clue?
Edit: with VS17 I've just a new Function 1.x project and right from the get-go its missing stuff:
I have .NET Framework 4.6.2 installed, and just reinstalled it.

It should be enough to just install the Microsoft.NET.Sdk.Functions package. From your error message, the problem lies with the Microsoft.NET.Sdk.Functions package. It seems that the build of the software package has failed. Please make sure you are using the latest version of the nuget package management tool.

Nuget problems with Azure Function 1.x and VS17
I think there are something broken in your VS2017 environment.
And in my side, when I created Azure Function project with Azure Function v1, it works well without any build errors. So l suggest you could try these suggestions:
Note: Update-Package -reinstall -Project MyVeryCriticalProject does not support PackageReference nuget format.
1) make sure that VS2017 has installed Azure development workload with those option tools( on the right part).
2) change your itemgroup xml node and keep the unique nuget package Microsoft.NET.Sdk.Functions and delete the others which are the dependencies of Microsoft.NET.Sdk.Functions. Like this:
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.35" />
</ItemGroup>
Then try to uninstall Microsoft.NET.Sdk.Functions nuget package under Nuget Package Management UI and then reinstall it.
3) Then click Restore Nuget Packages by Right-click on your solution
4) disable any third party extensions under Tools-->Updates and Extensions in case some extensions causes this behavior. From your code editor, I found you used some extensions like Resharper.
5) close VS Instance, delete .vs hidden folder(a hidden folder which is in the same directory as the xxx.sln), bin ,obj folder and then restart your project again.
6) repair VS in VS Installer
In addition, you could try to create a new Azure Function in VS2017 to test whether it is the problem of your own current project.

Related

DevOps Nuget Restore vs VisDev Nuget Restore

I'm having trouble getting DevOps to build my project. When I do a package restore is Visual Studio I get messages such as the following:
warning NU1701: Package 'Newtonsoft.Json 6.0.5' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETStandard,Version=v2.1'.
But doing a Nuget restore in DevOps... This is reports as an error and it doesn't restore or do the build.
Package Newtonsoft.Json 6.0.5 is not compatible with netstandard2.1 (.NETStandard,Version=v2.1)
It feels like I'm missing a command option someone on the DevOps restore.
Any pointers would be much appreciated.
I found a solution by using the PackageTargetFallback feature of the csproj file.
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>4.8.7</Version>
<PackageTargetFallback>netstandard2.1;net472;net462</PackageTargetFallback>
</PropertyGroup>
After adding this, nuget allowed packages from the specified frameworks to be restored.

Installing package with Nuget in .NET core on mac

I'm using .NET core on a mac with Visual Studio Code. i'm trying to install NewtonSoft.Json to use as a Json parser. The command I use is:
nuget install CoreCompat.NewtonSoft.Json -Pre
I use this command in my top level project folder. This leaves me with two problems. Firstly, and most glaring, I am still not able to use the package in my code.
using NewtonSoft.Json;
will not compile. Secondly, although this does download the package, it downloads a whole bunch of other stuff too - files like System.Threading, and puts them in my project directory. There are about 40 of these extra files. I already have these dependencies (which I'm assuming they must be) in my .nuget folder (and I'm able to include them in any project). I don't want to clutter up my project folder. How do I properly use nuget to install this package?
The command you want is
dotnet add package NewtonSoft.Json
This will add the following to your csproj file, which you could also do manually.
<ItemGroup>
<PackageReference Include="NewtonSoft.Json" Version="10.0.2" />
</ItemGroup>
Then you can use dotnet restore, dotnet build and friends to continue developing.
I also believe that you didn't mean to use the CoreCompat. prefixed package as this is not the original JSON.net library.

Cannot install Microsoft.NETCore.UniversalWindowsPlatform

Seemed to be related to this:
Nuget error install package Microsoft.NETCore.UniversalWindowsPlatform
but it is not, because the checkbox is checked by default in vs2017. I am running the creators update by the way and everything is up-to-date.
I have just started a new C# background IoT project and got this:
When manually installing the package in the package console I got this:
...
Successfully installed 'Microsoft.Net.Native.Compiler 1.6.0' to projectnamehere
Install failed. Rolling back...
Package 'Microsoft.NETCore.Jit.1.0.3' does not exist in project
...
Package 'Microsoft.NETCore.Jit.1.0.3' does not exist in folder
...
Install-Package : Could not install package 'Microsoft.NETCore.Jit 1.0.3'. You are trying to install this package into a project that targets '.NETCore,Version=v5.0', but the package does not contain any assembly references or content files that are compatible with that f
ramework. For more information, contact the package author.
seems related to this https://connect.microsoft.com/VisualStudio/feedback/details/1617801/newly-created-universal-app-projects-dont-work
but I have no idea how to fix it.
I have also seen a website suggesting to remove the project.json file, which I did but did not resolve it.
I have also closed vs2017 and modified the installtion to verify that the creators update sdk was checked and installed.
I have lowered the target version in the project props, but that did not help either.
edit manual install the jit package did not work either:
Could not install package 'Microsoft.NETCore.Jit 1.1.1'. You are trying to install this package into a project that targets '.NETCore,Version=v5.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
edit2 funny thing is that the solution explorer tells me that there is a project.json while there is none in windows explorer:
I verified the sdk installation by hitting the modify button of the vs2017 installation and tried to create a new project afterwards but got the exact same issue again.
Update: Microsoft updated the templates in their github (https://github.com/ms-iot/samples/commit/2e2aa34ab514b8c0725a53263898a412e0a1be1c) but didn't push it as an updated templates package to Visual Studio yet. Changed the answer accordingly.
Temporary fix:
replace in your .csproj file
<ItemGroup>
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
<None Include="project.json" />
</ItemGroup>
with
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
add the Microsoft.NETCore.UniversalWindowsPlatform package
Optional extra:
Add the Windows IoT Extension SDK via the references dialog
This issue has been fixed for Visual Studio 2017 in updated templates released here: https://marketplace.visualstudio.com/items?itemName=MicrosoftIoT.WindowsIoTCoreProjectTemplatesforVS15

Visual Studio Mac Preview Entity Framework SQLite add migration

I've installed Visual Studio for Mac (OSX 10.12.1) today and I've been diving in quite extensively.
I wanted to try to get EntityFrameworkCore (1.1.0) to run with SQLite.
So I've created a new Console Application .NET Core and with some troubles been able to add all the necessary nuget packages. Somehow Visual Studio was not able to download the dependencies, so I had to download every dependency manually. (Maybe this solves the problem: .Net Core 1.1.0 NuGet packages fail to install in Visual Studio Mac haven't testet this yet.)
As stated in this article (https://learn.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite) I wanted to add the migration, but I couldn't find the necessary command line tool in the IDE.
Did I miss something here?
Then I went on to use the .NET Core CLI to do it manually via. console. ( https://www.microsoft.com/net/core#macos). But when I execute dotnet ef migrations add init I get the following error.
No executable found matching command "dotnet-ef"
Was anyone able to get this to run successfully?
Visual Studio for Mac 2017 currently (April 2017) does not support adding a reference to Microsoft.EntityFrameworkCore.Tools.DotNet and returns an error:
Package 'Microsoft.EntityFrameworkCore.Tools.DotNet 1.0.0' has a package type 'DotnetCliTool' that is not supported by project 'MacMvc'.
You can edit the file manually and add the reference directly to the csproj file, as documented. Add this to your csproj file:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
</ItemGroup>
Then run dotnet restore to install the package. After that, you will be able to use dotnet ef migrations add NameOfMigration and dotnet ef database update scripts as per documentation.
N.B.: you must be in the project directory when executing commands.
Also see suggestion feeedback for VS 2017 for Mac:
https://visualstudio.uservoice.com/forums/563332-visual-studio-for-mac/suggestions/17169425-add-sql-server-integration
https://visualstudio.uservoice.com/forums/563332-visual-studio-for-mac/suggestions/17138506-terminal-window
Using VS for Mac, adding those following lines into .csproj makes the migration work for me:
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild2-final" />
Packages will be automatically restored saving the .csproj from VS.
To run the "dotnet ef" command through the terminal, you need to be in the project directory, I mean not from the directory where the .sln file is, but from the lower level.
Note: Same trick with v1.0.1 of Tools.DotNet didn't work, I do not know why.
Check if you have this section in your project.json file and add it if it's missing.
"tools": {
"Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4"
}
This is valid for EF 1.1, previous version was using Microsoft.EntityFrameworkCore.Tools package
Without add this ItemGroup, can't add the dbcontext scaffold connection string use of cmd, so adding this in your project (Edit .csproj) first
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
For me this solved the issue on macOS, run this on any terminal:
dotnet tool install --global dotnet-ef

Does nuget still require these entries in Visual Studio projects?

I've noticed that new .csproj projects without these nuget directives appear to work fine.
Are these a relic of the past that can be removed from projects?
I scanned the nuget FAQ but did not find anything related to this.
I'm running Visual Studio 2013 and also a recent build of Xamarin Studio.
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
Those particular targets are added if you have used the Enable NuGet Package Restore menu option. In this case these targets can be removed. Visual Studio will automatically restore NuGet packages, if you have a recent version of NuGet installed, without these extra targets. This menu option has been deprecated by the NuGet team and I believe it will be removed at some point.
Note that if you are building your project on a build server you will need to create an extra step to restore the NuGet packages, using NuGet.exe restore, that would have automatically been restored when the solution was built on the build server by MSBuild.
Also note that NuGet will add very similar targets if you add a NuGet package that includes its own MSBuild .targets file, such as Microsoft.Bcl.Build. The project will work without the EnsureNuGetPackageBuildImports Target in this case, but would need the Import element that imports the MSBuild .targets file. Leaving the EnsureNuGetPackageBuildImports target in the project does give you a perhaps more useful error message as to why a build may be failing if the NuGet package is not restored.

Resources