Nuget Packages with content in ASP.net 5 - asp.net

I have a couple packages that include content files (an example would be the wurfl package for getting device data from a user agent, it includes it's database file in the nuget package marked as content).
When adding these packages in an asp.net 5 setting, the content doesn't get added anywhere.
Where or how do I get at the content of these nuget packages added to my solution?

http://blog.nuget.org/20150729/Introducing-nuget-uwp.html
Deprecated Features
Starting with NuGet 3.1 when using project.json, we are deprecating support for executing the install.ps1/uninstall.ps1 scripts and delivering elements in the /content folder of packages. Installing packages that have these elements will not execute the install.ps1 file and will not copy content to your project.
Read the link for more on why...

This is an old question, but my even so old solution seems to work still for Net 5. Here it is:
Include a YourPacakgeName.targets file in your package like this:
<file src="YourPacakgeName.targets" target="build" />
The content of the .targets file should then be:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<MySourceFiles Include="$(MSBuildThisFileDirectory)..\Content\**\*.*"/>
</ItemGroup>
<Target Name="CopyFiles" BeforeTargets="Build" AfterTargets="Clean">
<Copy
SourceFiles="#(MySourceFiles)"
DestinationFolder="$(MSBuildProjectDirectory)\%(RecursiveDir)"
SkipUnchangedFiles="true"
/>
</Target>
</Project>
Effectively this copies the content files before a build and after a clean action.

Related

dotnet core nuget package copying content files on restore

So I feel like I have come to the end of the rope here, but hoping someone knows more than I do here. I have some Typescript files, though that is mostly irrelevant as I am having this problem with all content files.
I am able to generate a nuget, or more precisely dotnet pack, nuget package that includes my content files in the package by using this in the .csproj of my parent project:
<ItemGroup>
<Content Include="Scripts\Utility.ts">
<Pack>true</Pack>
<PackagePath>contentFiles\Scripts\;content\Scripts</PackagePath>
</Content>
</ItemGroup>
I can browse the generated .nupkg and see that indeed the file was added to the package in both the content\Scripts and contentFiles\Scripts locations
The problem is that whenver I consume this package in my 'child' progect, that Typescript never gets copied into any folder of the child project, though I can see it extracted in the .nuget\packages\parent\... folders.
At first I thought it was something with my initial settings in the parent project, and it may be, but after trying what seems like everything in the book, that fails to copy the content files to the child project. I then tried going the dark path of trying to use Init.ps1 in the tools folder of my package, and though it was impossible to debug, it also seemed to run sporatically (I completely unistalled and reinstalled the package and it still failed to run most of the time.) This could be the way but I don't know why I can't get it to output to the Package Manager Console... maybe there's still hope with Init.ps1 but I can't seem to figure it out. Finally I see some potential with a nuget .targets file but I can's seem to grasp how to use it for my purpose either! I would love some feedback as to how to get this done.
From: Announcing NuGet 3.1 with Support for Universal Windows Platform
Importing content from a Nuget package was depreciated for projects using a project.json file in Nuget v3.1. Since then the project.json file has been dropped in favour of the new .csproj format. Importing content from a Nuget package should still work though if you're using the packages.config file instead.
Also mentioned is the fact that there are other package managers available for delivering content.
It looks to me like the answer in the new world is to create a node module containing utility.js and let npm deliver it to your project.
Possible Workaround:
I've looked at .targets to copy files and got this working, but it does run on each build - which may or may not be a problem for you. I can't do what I want with it.
In [PackageId].targets:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Either do this for all scripts in the Scripts/js folder -->
<Target Name="CopyScriptsToProject" BeforeTargets="Build">
<Message Text="Copying scripts to project" />
<ItemGroup>
<SourceScripts Include="$(MSBuildThisFileDirectory)..\..\content\Scripts\js\**\*.*"/>
</ItemGroup>
<Copy SourceFiles="#(SourceScripts)" DestinationFiles="#(SourceScripts -> '$(MSBuildProjectDirectory)\wwwroot\js\%(RecursiveDir)%(Filename)%(Extension)')" Condition="!Exists('$(MSBuildProjectDirectory)\wwwroot\js\%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>
<!-- Or do this for the individual script -->
<Target Name="CopyUtilityScriptToProject" BeforeTargets="Build">
<Copy SourceFiles="$(MSBuildThisFileDirectory)..\..\content\Scripts\js\Utility.js" DestinationFiles="$(MSBuildProjectDirectory)\wwwroot\js\Utility.js" Condition="!Exists('$(MSBuildProjectDirectory)\wwwroot\js\Utility.js')" />
</Target>
</Project>
<!-- Note: condition can be removed from either if you want it to overwrite each build -->
and in the .csproj file (replacing [PackageId] with the name of your package):
<Project Sdk="Microsoft.NET.Sdk">
... any Globals for source control stuff ...
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<Version>7.0.0</Version>
<PackageId>[PackageId]</PackageId>
</PropertyGroup>
... any PackageReference stuff ...
<ItemGroup Label="Packaging">
<Content Include="build\netcoreapp2.0\[PackageId].targets" PackagePath="build\netcoreapp2.0\[PackageId].targets" />
<!-- Either -->
<Content Include="Scripts\js\**\*.*" PackagePath="content\Scripts\js;contentFiles\Scripts\js" />
<!-- or -->
<Content Include="Scripts\js\Utility.js" PackagePath="content\Scripts\js;contentFiles\Scripts\js" />
</ItemGroup>
</Project>
There seemed to be a bug whereby when the <PackageId>[PackageId]</PackageId> wasn't set explicitly in the .csproj, the build targets didn't work. Although that may well be an issue with my development environment.
Apparently you need the any\any in the path (learn more) as well as to include <PackageCopyToOutput>true</PackageCopyToOutput>, like this:
<ItemGroup>
<Content Include="Scripts\js\Utility.js">
<Pack>true</Pack>
<PackagePath>contentFiles\any\any\wwwroot\js\;content\any\any\wwwroot\js\</PackagePath>
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
</ItemGroup>
You'll also need to precompile your TypeScript before including the .js files in the package
However, this still doesn't create a file there, just some strange reference to it.
In the end, we got it working with a .targets file, you can find a working repo here: https://github.com/NuGet/Home/issues/6743
Serj Sagan's answer got me on the right track, but it wasn't sufficient to deploy the content file to the bin directory (as he noted). I was able to get the file to be deployed by changing the package reference options in the consuming project's .csproj file, as follows:
<PackageReference Include="MyNuGetPackage" Version="0.0.0.1">
<IncludeAssets>all</IncludeAssets>
<PrivateAssets>analyzers;build</PrivateAssets>
</PackageReference>
It seems like the default for PrivateAssets is contentfiles;analyzers;build (documentation), which is not what we want in this case.
Simplified code and explanation from #PurplePiranha
TL;DR:
Basic .NET6 simplified sample code on Github
Step by Step guide
Selection of the files
First we need to select all the files that needs to get into the nuget package.
Add this to the <LibraryPackageName>.csproj:
<Project Sdk="Microsoft.NET.Sdk">
...
<ItemGroup Label="Packaging">
<Content Include="<Your directory path>\<your file(s)>" />
</ItemGroup>
Multiple content lines are allowed.
Write a target
Make a target file to copy the files before (or after) the build to the bin directory:
The location and name of this file is important:
<root>\build\<LibraryPackageName>.targets
Now, make sure that it will get executed by referencing it in the <LibraryPackageName>.csproj by adding a content line:
<Project Sdk="Microsoft.NET.Sdk">
...
<ItemGroup Label="Packaging">
<Content Include="build\<LibraryPackageName>.targets" PackagePath="build\<LibraryPackageName>.targets" />
<Content Include="filesToAdd\*.txt">
<Pack>true</Pack>
</Content>
</ItemGroup>
Eg: From the code in github:
<Project Sdk="Microsoft.NET.Sdk">
...
<ItemGroup Label="Packaging">
<Content Include="build\PackageToGenerateFile.targets" PackagePath="build\PackageToGenerateFile.targets" />
<Content Include="filesToAdd/*.txt">
<Pack>true</Pack>
</Content>
</ItemGroup>
NOTE: By copying the files to the bin directory, the files are not part of your version control, but your package is!
Build and pack
In Visual Studio, right-click on the package name and select "Pack".
A new nuget package should be created in the bin directory of your library.
Use the nuget package
Install the nuget package now in your destination package.
Notice that the files are in the solution explorer, but not in a directory on your disk. They have a shortcut symbol.
Build the destination package
Check the bin directory.
The files should be copied to the location mentioned in the targets.

How to discover .js, .css files, what are in the filesystem (and repo) but not in .csproj?

Context
I regularly make the mistake to forget include a vendor .css or .js to the Asp Mvc project. I just copy/download with a tool, or from a theme, and referencing them. All works locally because the files are in the virtual directory so IIS Express will server them.
When publish times come, and I publish the new version, those files which are not in the .csproj will not be deployed.
Question
Although some tools or itself the IDE creates warning in some cases if in a syntax construct I refer to a resource what is not in the .csproj, this is not all working (for example: when using BundleConfig)
It seems to be pretty simple prevent this source of errors: Just check the file system with a well picked filter and list all files what are not included in the .csproj. (the filter could be: (*.css, .js, ...) or (assets/.*)
How can I accomplish this task?
If you switch to the new .csproj format supported by Visual Studio 2017, you no longer need to add references to files in the file system, they are picked up by default and you have to exclude files that you don't want.
Migration to the new .csproj format is pretty straightforward - you can use the dotnet migrate tool to make the conversion.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net47</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MyProj\MyProj.csproj" />
</ItemGroup>
<ItemGroup>
<!-- /* Exclude files you don't want */ -->
<Compile Remove="Text\AnyTransliterator.cs" />
<Compile Remove="Text\BreakTransliterator.cs" />
</ItemGroup>
</Project>
If you have files outside of your project directory that you want to include, you can create a link to a file or directory.
<!-- /* Link to an individual file outside of the project */ -->
<Content Include="..\..\..\Assets\something.css" Link="Assets\something.css" />
<!-- /* Create a virtual directory in Visual Studio named Assets
and link to external Assets directory. All files in that
directory will be included in the project */ -->
<Content Include="..\..\..\Assets\**\*" LinkBase="Assets" />
<!-- /* Create a virtual directory in Visual Studio named Assets
and link to external Assets directory. Only .css files in that
directory will be included in the project */ -->
<Content Include="..\..\..\Assets\**\*.css" LinkBase="Assets" />
This works with .NET Framework, but do note that you need to install the .NET Core SDK 2.0.0 in addition to VS 2017 15.3 (and ensure no global.json selects a lower SDK version) for the LinkBase option to work.
Reference: New .csproj format - How to specify entire directory as "linked file" to a subdirectory?

Build a single web deploy package from multiple web projects

I have a solution with A LOT of web projects (80+). I'm using WDP to deploy my projects to the environments, so I have a script that builds a web deploy package for each project and then each package is published.
Each package is deployed without deleting what is already there and each package can override duplicate files. It works but the deployment is quite slow.
I have a strong suspicion that having one package with all the file would be a lot faster, since from the looks of it a lot of the time is spent on the setup of the connection to the server and not much on the deployment itself (probably because many files are not updated)
Is there a way to build a single WDP from multiple web projects in visual studio solution or using MSBuild and a couple of scripts?
Thanks
Is there a way to build a single WDP from multiple web projects in visual studio solution or using MSBuild and a couple of scripts?
Since you have a script that builds a web deploy package for each project, you can try to deploy those project to the local folder, you can think of this an “intermediate publish folder”. From there you can construct a zip task to all packages to one zip file, then publish/copy this zip file to publish directly.
To accomplish this, you can create a .csproj file with following code:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<OutputType>Library</OutputType>
</PropertyGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="Zip" AfterTargets="Build">
<CreateItem Include="YourLocalPublishFolder\*.*" >
<Output ItemName="ZipFiles" TaskParameter="Include"/>
</CreateItem>
<Zip ZipFileName="YourZipFile.zip" WorkingDirectory="YourPublishFolder" Files="#(ZipFiles)" />
With this .csproj file, all .zip are compressed into a package to the publish folder, do not need to publish each project to the publish folder.
Note: Need import the MSBuildTasks.targets to the .csproj file, which including Zip target.
Hope this helps.

Convert .Net Core to .Net Framework

I have a .Net Core project web project, and for various reasons want to convert it to a .Net Framework project.
Is there an easy way to do this, or do I have to start again and import the code from the previous projects
I have loaded core project to the VS 2017 RC Community and open *.csproj in text editor.
Just delete teg
<RuntimeFrameworkVersion>
and replace
<TargetFramework>netcoreapp1.1</TargetFramework>
to
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
And after all in project properties set to any another framework and reset back (VS reload and repair *.csproj file).
This worked for me in VS2017:
Start with .net core web project template.
Edit *.csproj so it looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.CookiePolicy" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.RazorPages" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
</ItemGroup>
</Project>
Save and close.
Try running project.
The PackReferences is just the NuGet files, and you can add them through the GUI if the versions are different from mine above.
There's lots of similar answers here, but I didn't see one that was quite what I ended up doing, so I'd like to leave this here just in case someone else is in the same shoes.
Just to be clear, my project was a console program. So, if you're trying to use this answer for something else, your mileage may vary.
In your .csproj file, inside of the <PropertyGroup></PropertyGroup> tag, modify <TargetFramework> to reflect the following:
<TargetFramework>net461</TargetFramework>
Now, in this example, I was using v4.6.1. I can only assume that you'll plug in your version behind the word "net", without the periods. Good luck!
None of the answers here worked for me. In .Net Core 2 the project.json file no longer exists. However, I did solve this problem using the following steps.
1) I removed all nuget packages from my existing project.
2) I created a separate .net core web app project, targeting .net 4.61. This was to get the default nuget packages.
3) I edited the temporary project's .csproj file, copied all the PackageReference nodes inside ItemGroup, and pasted them into my existing projects .csproj file.
4) Edited the TargetFramework node (inside PropertyGroup) from "netstandard2" to "net461"
I had a few package changes to track down and resolve, but otherwise I was able to run.
In my version of Visual Studio 2017 (15.6.2) after 'Unloading the Project', right-clicking and selecting 'Edit <your project file>, I had to:
Add the node:
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
Delete the nodes:
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.16299.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.16299.0</TargetPlatformMinVersion>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
There are several steps that you need to do, in order to achieve this.
Firstly right click on the .csproj file and add the following
<TargetFrameworks>netstandard2.0;netcoreapp2.0;net35;</TargetFrameworks>
<RuntimeIdentifiers>win7-x86;win7-x64</RuntimeIdentifiers> <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
Once you have made these changes reload the project and build it.
This will generate the .dll files and Nuget package for this
build in the Debug/Release folder of the project.
Add these .dll to the nuget and access these projects from
nuget.
Try the above steps. This should work.
My .net standard project is relatively simple with few Nuget packages. I just changed
<TargetFramework>netstandard2.0</TargetFramework>
TO
<TargetFramework>**net461**</TargetFramework> under PropertyGroup section of .csproj file and this did the job for me.. Thanks to Brandon Barkley for your answer in the comments.
add below in csproj
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net471</TargetFrameworks>
</PropertyGroup>
I had only a handful of source files. For me it worked best by
Closing Visual Studio 2022
Renaming away the solution folder
Creating a new Visual Studio solution of type "WPF App (.NET Framework)" with the original folder name and same project name
Copying all *.xaml. *.xaml.cs and *.cs from the old project to the new, not touching *.sln, *.csproj and *.config.
Project->Add Existing Item… and adding the copied items
Adding all the special references.
That rebuilt all without a complaint.

Replace VS project files when doing web publishing (Web Deploy)

I'm running into a scenario where I need to replace some files in my project during publishing with other custom files. The case is we have several websites that uses the same code base. However, some websites needs a customized version of the deployed files (e.g. favicon.ico, Error.aspx…).
I have been trying to include these files as “extra files” in my projectName.wpp.targets as mentioned Here in Sayed Ibrahim Hashimi blog
but they get overwritten by the original project files during publishing.
I can see from the publishing output that the "extra files" are being included in the package but then they are being replaced by the same files from the project.
Is there a way to tell MS Deploy to include my files after the project content files are included such that my files overwrite?
------------Update------------------
I found out from the below log (thanks to David Martin comment) that the files are not being overwritten but rather they are being skipped because the files in the project are newer than what I am trying to include:
CopyPipelineFiles: Skip copying F:\Extra Files For Sites Deployment\xxx\favicon.ico to obj\xxx\Package\PackageTmp\favicon.ico, File obj\xxx\Package\PackageTmp\favicon.ico is up to date
So is there a way to force including these files even if they are older into the package?
OK, so I have found a fix for this and will post here if someone else ran to this issue but I welcome any other solutions!
The problem as stated in my question is caused by the files I want to include in my package being older than the files in my project. So I needed to find a way to update the last modified date.
I found out that there is a "Touch" task that will do exactly that:
http://msdn.microsoft.com/en-us/library/37fwbyt5.aspx
So this is what I finally did in my script:
<ItemGroup>
<Files Include="F:\Files For Sites Deployment\xxx\**\*" />
</ItemGroup>
<PropertyGroup>
<CopyAllFilesToSingleFolderForMsdeployDependsOn>
TouchIncludeFiles;
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForMsdeployDependsOn);
</CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
<Target Name="TouchIncludeFiles">
<Touch Files="#(Files)" ForceTouch="true"></Touch>
</Target>
<Target Name="CustomCollectFiles">
<ItemGroup>
<_CustomFiles Include="#(Files)" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>

Resources