MSBuild not restoring NuGet packages - msbuild

I have a solution consisting of multiple projects, several of which reference NuGet packages. There's a web site but also a service which has to be built for x86 or x64; the service is what I'm currently trying to build.
We're on TFS 2013; for this particular solution, I'm using Visual Studio 2015.
"Allow NuGet to download missing packages" and "Automatically check for missing packages during Visual Studio build" are both checked.
I was running NuGet 3.4.4, I've updated to 4.6.2.
I can build fine from Visual Studio. But automated builds on the server fail; but more importantly, it also fails if I try to run MSBuild locally. Similar to this question and this one.
As far as I can tell, the release template is the correct version (12), but I don't think it's a server issue, since I can't build locally either.
The packages are not in source control.
I have a nuget.config in the solution root folder; it previously was in a .nuget folder under the solution root. I do not (and never did) have nuget.exe or nuget.targets in the nuget folder. Here's the config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="NuGetCache" value="\\BuildServer\NuGetPackages" />
<add key="NuGetV2" value="https://www.nuget.org/api/v2/" />
</packageSources>
<packageRestore>
<add key="enabled" value="True" />
<add key="automatic" value="True" />
</packageRestore>
<solution>
<add key="disableSourceControlIntegration" value="true"/>
</solution>
</configuration>
If I (manually) clear the packages and run
MSBuild /p:Configuration=Release;Platform=x64
(from a VS2015 command prompt), it fails, because it can't find the referenced packages.
As suggested in this answer, I've removed the EnsureNuGetPackageBuildImports targets from my projects; they did not have a RestorePackages tag or an import for nuget.targets.
I tried adding a pre-build event to do the package restore (yes, I realize this is the "old" way of doing things)
"C:\Program Files (x86)\NuGet\nuget.exe" restore -ConfigFile "$(SolutionDir)nuget.config" -PackagesDirectory "$(SolutionDir)packages"
... but it consistently fails ("The command exited with code 1"). If I copy the NuGet command line from the MSBuild output and execute it on its own, it succeeds.
I have noticed one odd thing: in the pre-build event, NuGet says "The folder d:\path\to\solution\project1\bin\Release-x64 does not contain an msbuild solution or packages.config file to restore". I'm not sure why it's looking for those in the output folder.
I do have a workaround for this. We have a network share set up to use as sort of a package cache. If I hand-edit the project files and change each reference so that the HintPath points to \\BuildServer\NuGetPackages\ rather than ..\packages\, the build will succeed. But this is ugly, and more importantly it significantly degrades the performance of Visual Studio.

Related

Missing Nuget packages error in ,NET core 3.1 app

I am using VS code and not Visual Studio to run my .NET core 3.1 web app.
I have a .NET core 3.1 app. As I am using an Client VDI machine ( It is using Win 7 SP1 operating system) which is blocking the Nuget.Org URL currently.
so as a work around, I have copied over all the Nuget package into an folder in my VDI and updated the path in the Nuget.Config.
But still I am getting below
Missing Package" error. My App do not DIRECTLY depends on these packages.
following is my Nuget.Config file,
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!-- add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" / -->
<add key="nuget.org" value="C:\NugetPackageDownloadLocation" / >
</packageSources>
</configuration>
any help will be appriciated.
Have you tried adding a package source via the CLI? Given how .NET Core handles transitive dependencies getting all the packages required for a relatively complex solution might be tricky.
nuget sources Add -Name "MyServer" -Source \\myserver\packages

The "TransformAppSettings" task failed unexpectedly

I have a .NET Core 3 Blazor (server side) application which I recently upgraded to .NET Core 3.0.1 preview 6 version from the preview 5 version. When I build and run it locally, it works fine; but when trying to publish it to a file system folder (in Framework-Dependent mode), it throws this error:
C:\Program Files\dotnet\sdk\3.0.100-preview6-012264\Sdks\Microsoft.NET.Sdk.Publish\targets\TransformTargets\Microsoft.NET.Sdk.Publish.TransformFiles.targets(192,5): Error MSB4018: The "TransformAppSettings" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
File name: 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
at Microsoft.NET.Sdk.Publish.Tasks.AppSettingsTransform.UpdateDestinationConnectionStringEntries(String destinationAppSettingsFilePath, ITaskItem[] destinationConnectionStrings)
at Microsoft.NET.Sdk.Publish.Tasks.TransformAppSettings.TransformAppSettingsInternal()
at Microsoft.NET.Sdk.Publish.Tasks.TransformAppSettings.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()
It's perhaps worth mentioning that this error didn't occur in the preview 5 version. Also, I use Visual Studio Enterprise 2019 (Windows).
Things I've tried so far: (with no luck)
Clean/Rebuild solution
Reinstall .NET Core 3 preview 6 SDK
Add Newtonsoft.Json package via Nuget
Search for related issues raised by the community on github
.csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>7.3</LangVersion>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="6.1.0" />
<PackageReference Include="MatBlazor" Version="1.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="SqlTableDependency" Version="8.5.3" />
<PackageReference Include="System.DirectoryServices" Version="4.5.0" />
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="4.5.0" />
<PackageReference Include="Telerik.UI.for.Blazor" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\images\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\{path to project file}" />
</ItemGroup>
</Project>
In my case, I edit the publish configuration and turned off the Database -> Use this connection at runtime. Then I could publish without this error, however, I had to manually edit the web.config and make the following changes.
modules="AspNetCorModuleV2" had to become modules="AspNetCodeModule"
hostingModel="InProcess" had to become hostingModel="OutOfProcess"
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> had to become value="Production"
These changes were already in my own directories web.config, but they didn't seem to get propagated in the publish anymore.
I had this same error and was able to fix it by editing the .pubxml.user file.
When the error occurred, my publish settings didn't have the "Use this connection at runtime" selected, nor were any database migrations checked. But when I looked in the pubxml.user file and it turns out there was a reference to a non-existent database that wasn't showing up in the Publish Settings UI. I removed that and it solved the problem.
All I did was uncheck the "Use this connection string at runtime" database option, since my azure server will that set that anyway it shouldn't be an issue.
I have the same issue
Same Question on Stack Overflow
Though i have managed to publish my project via powershell commands
dotnet publish --configuration Release --framework netcoreapp2.2
Try it, I hope it will help...
I know this has been resolved for you. But in my own case.
IDE: Visual Studio 2017 Enterprise
Publish Profile: Folder
Follow this step:
Change the build option to the environment you want to publish for
(Test, Staging, Release or Production) and clean the solution.
Open your project folder and delete the bin and obj folders.
Restart Visual Studio
After restarting VS, delete the publish profile and re-create the profile
This should fix it as it did for me. After which I was able to publish comfortably.
I hope this helps.
My issue was resolved when I
checked "Use this connection string at runtime"
unchecked the "Default connection string"
If you are publishing to a local file System. Check whether the folder has required read/write permission for Visual Studio to copy the published files.

How to publish a Asp.net 5 MVC6 website on IIS containing two web apps

I have got a project with two web apps. One of them is only for reference in the base app. The project runs okay in visual studio. I am trying to publish it in local file system and then copy it across to the windows server 2008. Below is the web.config generated by visual studio.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="bootstrapper-version" value="1.0.0-beta4" />
<add key="runtime-path" value="..\approot\packages" />
<add key="dnx-version" value="1.0.0-beta4" />
<add key="dnx-clr" value="clr" />
<add key="dnx-app-base" value="..\approot\src\MySite" />
</appSettings>
</configuration>
When I run this website in Server using dnx . web, it runs okay. But when I try to run it under IIS by pointing IIS to wwwroot then all of the packages go missing. The website is unable to find packages.
Failed to resolve the following dependencies for target framework 'DNX,Version=v4.5.1':
Base32 1.0.0.13
BouncyCastle 1.7.0
CsQuery 1.3.4
EcmaScript.Net 1.0.1.0
EntityFramework 7.0.0-beta4
EntityFramework.Commands 7.0.0-beta4
EntityFramework.Core 7.0.0-beta4
EntityFramework.Relational 7.0.0-beta4
EntityFramework.Relational.Design 7.0.0-beta4
EntityFramework.SqlServer 7.0.0-beta4
EntityFramework.SqlServer.Design 7.0.0-beta4
.........
.........
How did you deploy?
Here are the steps that you can follow to get that running:
Make sure that you the app pool is a .NET 4 app pool
Run, in your web app's project folder dnu publish --runtime <name of runtime> (the name is the name of the runtime folder under %userprofile%\.dnx\runtimes
Step 2 generates a folder that contains the application, its dependencies and the runtime under the bin\debug folder.
Copy that folder to the IIS website folder
Run

NuGet Package Restore does not fetch Build Target Assemblies (Tools)

I added Fody ProperyChanged to two projects in my solution. Package Restore is enabled on the solution. However, the TFS Build Service fails building with the following error:
WindowsUI.csproj (443): The imported project
"SolutionDir\Tools\Fody\Fody.targets" was not found. Confirm that the
path in the declaration is correct, and that the file exists
on disk.
The folder is indeed not there. I could check it into source control, obviously. However, should it not be populated by the NuGet Package Restore? Or am I misunderstanding what NuGet Package Restore does?
I ran into a similar problem trying to get a solution to build on Visual Studio Online.
Problem is that packages are restored before a project build, but before that the project
files and target inclusions from packages (still to be restored) have already been interpreted.
Use the before build hook as described here:
http://sedodream.com/2010/10/22/MSBuildExtendingTheSolutionBuild.aspx
In your before.solutionname.sln.targets file put something like this to force all packages to be restored before even the first project is built:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"
DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="BeforeBuild" BeforeTargets="Build">
<Message Text="Restoring all nuget packages before build" Importance="high">
</Message>
<Exec Command=".\.nuget\NuGet.exe restore YourSolution.sln" />
</Target>
</Project>
If you have external package sources configure them in your nuget.config file which should
also be in the .nuget folder. For example:
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<packageSources>
<add key="NuGet official package source" value="https://nuget.org/api/v2/" />
<add key="YourSource" value="http://yoursource.somewhere.net/nuget" />
</packageSources>
<packageRestore>
<!-- Allow NuGet to download missing packages -->
<add key="enabled" value="True" />
<!-- Automatically check for missing packages during build in Visual Studio -->
<add key="automatic" value="True" />
</packageRestore>
</configuration>
As of version 1.13.0.0 (released March 23, 2013) Fody is a 100% nuget deployed tool and as such it will work with package restore.
https://nuget.org/packages/Fody/
This will appear when you install the Fody Nuget
https://github.com/Fody/Fody/blob/master/NuGet/readme.txt
UPDATE: This answer now only applies to versions prior to 1.13.0.0.
The files in SolutionDir\Tools\Fody cannot be deployed through nuget and needs to be checked into source control
You are running into the same issue that I did when I tried to ship a build update in NuGet package. The issue is that NuGet package restore is invoked during the build process. Because of this if NuGet package restore restores a .targets file that is imported, it is restored too late. By the time the file is written to disk the <Import element has already been evaluated and skipped due to the file not being on disk.
The best thing that I have found is to build another project to invoke the package restore for you. In order to smooth this out for my own SlowCheetah NuGet package when the NuGet package is installed I create a packageRestore.proj file in the same director as the .csproj/.vbproj. Then users can build this project file and then the .sln/.csproj/.vbproj. By doing this the NuGet packages are restored and then the build process is kicked off.
If you are interested in using my packageRestore.proj I can re-factor that part of SlowCheetah NuGet package into its own and your NuGet package can depend on that one. Let me know if you are interested in that.

NuGet Enable Package Restore with TFS

I have enabled "Package Restore", into our builds, to which we have a nightly build to ensure everything builds correctly.
We are getting package errors on our build machine, but not on our local machines.
The error is:
nuget.targets (43): Unable to find version (2.5.1) Castle.Core
I would assume the version are package are irrelevant, but I've added for context.
Any thoughts?
Package Config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="2.5.2" />
<package id="FluentNHibernate" version="1.2.0.712" />
<package id="Iesi.Collections" version="3.1.0.4000" />
<package id="NHibernate" version="3.1.0.4000" />
<package id="NHibernate.Castle" version="3.1.0.4000" />
</packages>
Set the build log to output at a diagnostic level. This should show the error during the nuget portion of the build.
The switch is:
/verbosity:diag
You didn't say what you are using to initiate the CI, so I assume you know how to modify the invocation.
You can also try to run the nuget.targets file manually via msbuild from the command line. From the project folder try:
msbuild myproj.csproj /target:RestorePackages /v:diag
or
msbuild myproj.csproj /target:BuildPackages /v:diag
Make sure all the package sources you need for restoring are set and not disabled on the ci machine.
To list/add them :
nuget.exe sources <List|Add|Remove|Enable|Disable|Update> -Name [name] -Source [source]
This could also be a proxy issue, but it's less likely since support has been added in recent nuget.exe and it would ask for credentials.