简体   繁体   English

Nuget自动还原为WebSite

[英]Nuget Automatic Restore for WebSite

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 我正在尝试将所有C#项目迁移到新的Nuget自动还原,遵循本教程: 迁移MSBuild集成解决方案以使用自动包还原

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): 我已成功完成了我的桌面/库项目,我必须编辑.csproj文件,从中删除这些行(我没有使用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. 但是,WebSites似乎没有任何.csproj或包含这些说明的任何其他文件。 When I install a package, it sucessfully put the .dll inside my packages folder, but it also put in bin folder. 当我安装一个软件包时,它成功地将.dll放在我的packages文件夹中,但它也放在bin文件夹中。 If I select the .dll under /bin within Solution Explorer, it has the following properties: 如果我在解决方案资源管理器中选择/ bin下的.dll,它具有以下属性:

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. 当我第一次从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. 我认为它不应该查看bin文件夹,或者当我构建项目时,它应该将.dll带到bin文件夹(如果它不存在)。 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?)". 问题是如果我在bin中没有.dll的情况下构建项目,它会给我以下错误:“找不到类型或命名空间名称'Newtonsoft'(您是否缺少using指令或程序集引用?)”。 For desktop/libraries projects, the .dll is copied to bin folder. 对于桌面/库项目,.dll将复制到bin文件夹。

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? 我在另一个问题中读到Nuget不支持WebSite,而是Web应用程序: 网站的NuGet包还原 ,但我也在Nuget的页面中读到他们已经添加了对ASP.NET网站的兼容性,所以这是我的问题:我是做错了什么? Or should I migrate to Web Application because they don't support Web Sites at all? 或者我应该迁移到Web应用程序,因为它们根本不支持Web站点?

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. 我想知道你是否想出如何在没有项目文件的情况下在网站上进行nuget恢复。 I do have a package.config in the website. 我在网站上有一个package.config。

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 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. 上面的命令确实按预期将包恢复到.. \\ packages文件夹中,但我无法弄清楚如何将正确的程序集放入网站的bin文件夹中。

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: 当您在Visual Studio中的解决方案的鼠标右键上下文菜单中单击“启用NuGet包还原”时,您会收到一条信息,其中显示:

Packages installed into Website projects will not be restored during build. 在构建期间,将不会还原安装到网站项目中的软件包。 Consider converting those into Web application projects necessary. 考虑将这些转换为必要的Web应用程序项目

However there is a workaround I tried that works. 但是我尝试了一种可行的解决方法。 Consider situation when you have a class library (DLL) project referenced by the Website project. 当您拥有Website项目引用的类库(DLL)项目时,请考虑这种情况。 If both projects reference the same NuGet package, then building whole solution the DLL project is built first, packages are restored correctly. 如果两个项目都引用相同的NuGet包,那么构建整个解决方案首先构建DLL项目,正确地恢复包。 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. 在网站项目的下一步,所需的软件包已经到位,并且根据*.refresh文件将其DLL复制到/Website/Bin/文件夹中。 Result - solution build finishes successfully. 结果 - 解决方案构建成功完成。

I can definitely confirm that NuGet automatic restore feature actually works for Web Sites projects under VS with latest version of NuGet. 我绝对可以确认NuGet自动恢复功能实际上适用于 VS下的网站项目和最新版本的NuGet。

Make sure that: 确保:

  1. You are using NuGet 2.7 or higher (Tools > Extensions and Updates > Updates) 您正在使用NuGet 2.7或更高版本 (工具>扩展和更新>更新)
  2. There is no .nuget\\NuGet.targets file at the solution root 解决方案根目录下没有.nuget \\ NuGet.targets文件
  3. You have packages.config in Web Site root with all references to the packages (normally generated by VS when adding NuGet packages) 您在Web站点根目录中有packages.config ,包含对包的所有引用(通常在添加NuGet包时由VS生成)
  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 Visual Studio在Visual Studio中配置为“ 允许NuGet下载缺少的包 ”和“ 在构建期间自动检查缺少的包 ”(请参见选项> NuGet包管理器) - 默认情况下为ON

Here what I see as the build output when some package for Web Site project is missing. 这里我看到的是当缺少某些Web站点项目包时的构建输出。

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 . 详细描述以上内容的链接: https//docs.nuget.org/consume/package-restore

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. 如果您有一个单独的业务逻辑层,则可以将nuget包安装到该项目中。 Then you can do the nuget restore on the business logic layer prior to doing msbuild on the website solution. 然后,您可以在网站解决方案上执行msbuild之前在业务逻辑层上执行nuget还原。 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. 在包含BLL和WebSite的解决方案上进行msbuild时,它会将所有引用的程序集( 包括nuget恢复的dll )从BLL项目中拉出到网站的bin文件夹中。

Here is a hack option if you don't have a separate business logic layer. 如果您没有单独的业务逻辑层,那么这是一个hack选项。 Using NuGet with *.dll.refresh files in ASP.NET "Web Site" projects with Web Deployment Projects 在Web部署项目的ASP.NET“Web站点”项目中使用NuGet和* .dll.refresh文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM