Reputation: 121
I'm trying to migrate all my C# projects to new Nuget Automatic Restore, following this tutorial: Migrating MSBuild-Integrated solutions to use Automatic Package Restore
I've successfully done it to my desktop/libraries projects, which I had to edit .csproj files, removing these lines from it (I'm not using TFS):
<RestorePackages>true</RestorePackages>
...
<Import Project="$(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>
However, WebSites don't seem to have any .csproj or any other file containing these instructions. When I install a package, it sucessfully put the .dll inside my packages folder, but it also put in bin folder. If I select the .dll under /bin within Solution Explorer, it has the following properties:
Auto-refresh path: C:\mypackages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll
File Name: Newtonsoft.Json.dll
Full Path: C:\MyWebSite\Bin\Newtonsoft.Json.dll
This is set default when I first install a package from nuget. I think it should not look into bin folder, or when I build the project, it should bring the .dll to bin folder if it doesn't exist. The problem is if I build the project without the .dll in bin, it gives me the following error: "The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)". For desktop/libraries projects, the .dll is copied to bin folder.
I read in another question Nuget doesn't support WebSite, but Web Applications instead: NuGet Package restore for website, but I also read in Nuget's page that they have added compatibility to ASP.NET Web Sites, so here is my question: Am I doing something wrong? Or should I migrate to Web Application because they don't support Web Sites at all?
Upvotes: 12
Views: 7315
Reputation: 489
Here is the solution to this problem and it does work.
If you have a separate business logic layer, you could install the nuget packages into that project. Then you can do the nuget restore on the business logic layer prior to doing msbuild on the website solution. When doing msbuild on the solution which contains the BLL and the WebSite it will pull all the referenced assemblies (including the nuget restored dlls) out of the BLL project into the website's bin folder.
Here is a hack option if you don't have a separate business logic layer. Using NuGet with *.dll.refresh files in ASP.NET "Web Site" projects with Web Deployment Projects
Upvotes: 2
Reputation: 489
I also have the same problem described in the above question. I was wondering if you figured out how to do a nuget restore on the website without the project file. I do have a package.config in the website.
I tried a bunch of different things and the following command got me closer to a resolution, but not quite.
nuget restore packages.config -PackagesDirectory ..\packages
The above command does restore the packages into the ..\packages folder as expected, but I cannot figure out how to get the correct assemblies into the website's bin folder.
Upvotes: 8
Reputation: 5357
I can definitely confirm that NuGet automatic restore feature actually works for Web Sites projects under VS with latest version of NuGet.
Make sure that:
Here what I see as the build output when some package for Web Site project is missing.
Restoring NuGet packages... To prevent NuGet from restoring packages during build, open the Visual Studio Options dialog, click on the Package Manager node and uncheck 'Allow NuGet to download missing packages during build.' ------ Build started: Project: WebSite1, Configuration: Debug Any CPU ------ Validating Web Site Building directory '/'. Validation Complete ========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========
Link that describes the above in details: https://docs.nuget.org/consume/package-restore.
Upvotes: 2
Reputation: 440
When you click "Enable NuGet Package Restore" in the right mouse button context menu on the solution in Visual Studio you get an info message shown which says:
Packages installed into Website projects will not be restored during build. Consider converting those into Web application projects necessary.
However there is a workaround I tried that works. Consider situation when you have a class library (DLL) project referenced by the Website project. If both projects reference the same NuGet package, then building whole solution the DLL project is built first, packages are restored correctly. Next step when it comes to the Website project the required package is already in place and its DLLs are copied into /Website/Bin/
folder according to the *.refresh
file. Result - solution build finishes successfully.
Upvotes: 2