Including referenced projects in NuGet package - nuget

I have the following project in my solution which I am trying to use to create a NuGet package:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>ExpressionTreeTestObjects</PackageId>
<Authors>Zev Spitz</Authors>
<Company />
<Product>ExpressionTreeTestObjects</Product>
<Description>A set of expression trees, and instances of other types from System.Linq.Expressions, for testing code against a variety of expression trees. The objects are generated by the C# compiler, by the VB.NET compiler, or using the factory methods at System.Linq.Expressions.Expression.</Description>
<Copyright>Copyright (c) 2019 Zev Spitz</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/zspitz/ExpressionTreeToString</PackageProjectUrl>
<RepositoryUrl>https://github.com/zspitz/ExpressionTreeToString</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>expression-tree code-generation visual-basic.net vb.net csharp test-data tostring</PackageTags>
<IncludeBuildOutput>true</IncludeBuildOutput>
<IncludeReferencedProjects>true</IncludeReferencedProjects>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ExpressionTreeTestObjects.VB\TestObjects.VB.vbproj" />
<ProjectReference Include="..\ExpressionTreeTestObjects\TestObjects.csproj" />
</ItemGroup>
</Project>
Build and pack both seem to work. However, the .nupkg file doesn't seem to include the referenced DLLs.
How can I troubleshoot this? How can I resolve it?

I think the solution can be found here:
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="#(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
</ItemGroup>
</Target>

Related

Visual Studio Code adding Microsoft.EntitiyFrameworkCore.dll

unfortunately the PC I am working with has no Internet Connection and only Visual Studio Code. So far I have not been able to install the microsoft.entityframeworkcore.3.1.4.nupkg package. So I was thinking about just using the dll but keep everything else in the project like usual.
In my class I want to call:
using Microsoft.EntitiyFrameworkCore;
my csproj file looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" />
</ItemGroup>
<Reference Include="Microsoft.EntityFrameworkCore">
<HintPath>..\Framework\Microsoft.EntityFrameworkCore.dll</HintPath>
</Reference>
</Project>
I also copied the dll to the debug and release folder, as well as the Framework folder. Can someone give me a hint what I am missing? I assume locally add the nupkg package just by a command line without internet and without the full version of Visual Studio is impossible.
Thanks
Stephan
I think I forgot the itemgroup:
<ItemGroup>
<Reference Include="Microsoft.EntityFrameworkCore">
<HintPath>\Framework\Microsoft.EntityFrameworkCore.dll</HintPath>
</Reference>
</ItemGroup>

Get the location of NuGet packages

In my company we have some home made tools that are used in the build process when building other projects.
I need to use these tools in VS2017 BeforeBuild and AfterBuild scripts and it must work in MS Build as well.
The tools are distributed as NuGet packages and most of our projects are ported to PackageReference instead of Packages.config
I know that the current installation of MyTool (version X.Y.Z) is at C:\Users\Me\.nuget\packages\MyTool\X.Y.Z, but how do I reference it in my project file, so it also works when the next version is released?
I think C:\Users\Me\.nuget\packages can be replaced with $(NuGetPackageRoot), but what to do to always reference the version installed in the project?
Some Nuget packages seem to put contributions into MyProject.csproj.nuget.g.props and MyProject.csproj.nuget.g.targets in the obj folder, but I can find very little useful information about these files.
Inside a target, you can use this to create a property based on an item:
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
</ItemGroup>
<Target Name="PrintStuff" AfterTargets="AfterBuild">
<PropertyGroup>
<NewtonsoftJsonVersion Condition="'%(PackageReference.Identity)' == 'Newtonsoft.Json'">%(PackageReference.Version)</NewtonsoftJsonVersion>
<NewtonsoftJsonPath>$(NuGetPackageRoot)newtonsoft.json\$(NewtonsoftJsonVersion)\</NewtonsoftJsonPath>
</PropertyGroup>
<Message Importance="high" Text="JSON.NET version: $(NewtonsoftJsonVersion)" />
<Message Importance="high" Text="JSON.NET path: $(NewtonsoftJsonPath)" />
<Exec Command="ls" WorkingDirectory="$(NewtonsoftJsonPath)" Condition="'$(OS)' != 'Windows_NT'" />
<Exec Command="dir" WorkingDirectory="$(NewtonsoftJsonPath)" Condition="'$(OS)' == 'Windows_NT'" />
</Target>

Get rid of stylecop.json from nuget package

I've setup building nuget package from my project using package tab in project options. Everything seems to work except that my nuget package contains stylecop.json file. Build action for this file is set to "C# analyzer additional file". How to get rid of this file from nuget package?
This is my csproj file content
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseUrl>https://github.com/aixasz/ImageShareTemplate/blob/develop/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/aixasz/ImageShareTemplate/</PackageProjectUrl>
<Description>ImageShareTemplate is image template library to share an image to social like facebook, twitter, etc.</Description>
<RepositoryUrl>https://github.com/aixasz/ImageShareTemplate/</RepositoryUrl>
<PackageTags>image socialmedia</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0001" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0001" />
</ItemGroup>
<ItemGroup>
<Folder Include="Fonts\" />
</ItemGroup>
</Project>

Not able see test cases in Text Explorer for the .net core 2 based Test project

I have created a test project based on .Net Core 2 and wrote some NUnit test cases. After installing necessary NuGet packages i.e. NUnit3TestAdapter, I was able to see all test cases in "Test Explorer" and able to execute those. Now, when I looked into the project directory, I found that it's creating "obj" folder and some json files in it. So I tried to change the path of "obj" folder by modifying ".csproj" file. I provided some different path in the parameter "BaseIntermediateOutputPath" and that way, I was able to get rid of "obj" folder. The reason for providing different path was, I wanted to keep json files separate from source code.
However, after modifying that I am not able to see or execute any test cases from Test Explorer.
Is this a Microsoft bug?
Is any packages having dependency on "obj" folder?
P.S.
I am using "NUnit" and "NSubstitute" packages for my test project.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<TargetFramework>netcoreapp2.0</TargetFramework>
<OutputPath>..\..\build\$(Configuration)\UnitTests\</OutputPath>
<BaseIntermediateOutputPath>..\..\work\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<ItemGroup>
<PackageReference Include="Castle.Core" Version="4.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="NSubstitute" Version="2.0.3" />
<PackageReference Include="NUnit" Version="3.8.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
</ItemGroup>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
<ItemGroup>
<ProjectReference Include="..\UtilityLibrary\UtilityLibrary.csproj" />
</ItemGroup>
</Project>
When .NET Core projects build, they do not copy all referenced files into the bin folder. When you add Microsoft.NET.Test.Sdk to your test project, one of the things it does is add an AssemblyResolve event handler which loads other dependent assemblies from a list of searchDirectories.
BaseIntermediateOutputPath not working was reported against the VSTest project and is an issue with MSBuild. The workaround is noted in the dotnet sdk repository. From that, you need to use Sdk imports in your csproj instead of the Sdk attribute on the Project element.
<Project>
<PropertyGroup>
<BaseIntermediateOutputPath>obj\XXX\</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<!-- Body of project -->
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>

Publishing my asp.net mvc application via script and not Visual Studio

I dont really know much about this to be honest with you...
I have managed to download mscommunity build and I have managed to use the script below to successfully compile and build my application, however I want to get my asp.net mvc application "published" so I want the same files that you when clicking "publish" inside visual studio. My current build file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Import the MSBuild Tasks -->
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<ClassLibraryOutputDirectory>c:\publish\</ClassLibraryOutputDirectory>
<ProjectDir>..\PetProject\</ProjectDir >
<ProjectTestDir>$(ProjectDir)PetProject.WebUI\</ProjectTestDir >
<ProjectFile>$(ProjectDir)PetProject.sln</ProjectFile >
<TestProjectFile>$(ProjectTestDir)PetProject.WebUI.csproj</TestProjectFile >
</PropertyGroup>
<!-- Build projects by calling the Project files generated by VS -->
<Target Name="Build">
<MSBuild Projects="$(ProjectFile)" />
<MSBuild Projects="$(TestProjectFile)" />
</Target>
</Project>
I call this in command line using:
C:\Windows\Microsoft.NET\Framework\v3.5>msbuild.exe C:\Projects\PetProject\build
\PetProject.build
Help is greatly appreciated...
NOTE: I want to avoid CI, Nant etc. because I dont really know what they are and I ideally want to get the above working as First Base, then move onto other things like CI or whatever else, I dont want to confuse myself too much...
This should give you the same result as publishing from within Visual Studio:
<Project DefaultTargets="BuildAndPublish" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
<PropertyGroup>
<ProjectFile>C:\PetProject\PetProject.csproj</ProjectFile >
<OutDir>C:\PetProject\MyPublish</OutDir>
</PropertyGroup>
<Target Name="BuildAndPublish">
<MSBuild Projects="$(ProjectFile)" Targets="Package" Properties="Configuration=Release;PackageLocation=$(OutDir)\MSDeploy\Package.zip;_PackageTempDir=$(OutDir)\Temp" />
</Target>
</Project>
for your project.
Don't forget to import Microsoft.Web.Publishing.targets which contains the Package target (which I mixed up with Publish in my inital answer).
If you want to build your solution your script should look something like this:
<Project DefaultTargets="BuildAndPublish" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
<PropertyGroup>
<OutDir>C:\PetProject\MyPublish\</OutDir>
</PropertyGroup>
<ItemGroup>
<Solution Include="C:\PetProject\PetProject.sln">
<Properties>
OutDir=$(OutDir);
Platform=Any CPU;
Configuration=Release;
DeployOnBuild=True;
DeployTarget=Package;
PackageLocation=$(OutDir)\MSDeploy\Package.zip;
_PackageTempDir=$(OutDir)\Temp
</Properties>
</Solution>
</ItemGroup>
<Target Name="BuildAndPublish">
<MSBuild Projects="#(Solution)" />
</Target>
</Project>
There's a blog post by Code Inside which describes basically the same approach but didn't work when I tried it in my environment.