Nuget Automatic Restore for WebSite
Asked Answered
D

4

12

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?

Dripstone answered 22/4, 2015 at 20:30 Comment(1)
WAP is definitely the way to go. Websites are much more limited and doesn't really have many advantages if you are using VS. Note that with the release of ASP.NET 5, it will combine the best of both worlds. I've used NuGet with websites in the past just fine though.Peggypegma
B
8

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.

Bugg answered 17/12, 2016 at 15:56 Comment(2)
Doesn't seem possible to do it. I spent a half a day trying to get it to work with no success.Serene
use a .refresh filePicco
P
2

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.

Pest answered 23/4, 2015 at 13:26 Comment(0)
S
2

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:

  1. You are using NuGet 2.7 or higher (Tools > Extensions and Updates > Updates)
  2. There is no .nuget\NuGet.targets file at the solution root
  3. You have packages.config in Web Site root with all references to the packages (normally generated by VS when adding NuGet packages)
  4. Visual Studio is configured to "Allow NuGet to download missing packages" and "Automatically check for missing packages during build" in Visual Studio (see Options > NuGet Package Manager) - these are ON by default

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.

Showmanship answered 24/5, 2015 at 19:6 Comment(5)
I see the package being restored, but the scripts do not appear to be re-added to the actual web site directory. Do you see them being re-added?Intrados
@GodDibbs, which scripts do you mean?Showmanship
Sorry, just realized I wasn't being generic enough :). For example, if you install the react nuget package to a web site project, then remove the files and the packages folder, then attempt to restore, the packages folder gets repopulated, but the scripts folder in the website does not.Intrados
@GotDibbs, it might be something with this particular package - I need to try it out. It worked for regular .NET assemblies NuGet packages (like Newtonsoft.Json) for sure.Showmanship
@GotDibbs, right, in your particular case the "scripts" will not be copied if you delete it. However looks like it has nothing to do with Web Sites. It's how NuGet works for such "content" packages - the same behavior will be for WebApplications projects. The reason it works differently for the assemblies is ".refresh" files being created for references DLLs that makes msbuild to copy them over from location with NuGet packages. The workaround for you might be fictive project with build event to copy needed files from "packages". VS will make sure that you will always have it up-to-date.Showmanship
B
2

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

Bugg answered 23/4, 2017 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.