I'm using a custom targets file to prevent superfluous copying when running MSBuild on C# projects in a build server. Originally I started with this answer:
<ItemDefinitionGroup>
<Reference>
<Private>False</Private>
</Reference>
<ProjectReference>
<Private>False</Private>
</ProjectReference>
</ItemDefinitionGroup>
This works great for references, but I wasn't able to find any equivalent version to files marked with Content or None tags.
This is what I've come up with, and unfortunately it doesn't work:
<ItemDefinitionGroup>
<Content>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<None>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemDefinitionGroup>
Example: I'd like my_resource.dll to never copy to the output directory. This is how it's defined within the project file:
<ItemGroup>
<Content Include="..\..\..\my_resource.dll">
<Link>my_resource.dll</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
What am I missing? Thanks!
Related
I have an asp.net core project built starting from net core 3.1 into which I required to separate some controllers and views into assemblies, so I used application parts technique. I followed as indicated in this article and this other article to achieve that, so I started by configuring csprojs related with those dlls:
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<EnableDefaultContentItems>false</EnableDefaultContentItems>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="Telerik.UI.for.AspNet.Core" Version="2021.3.1207" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Shared\_ComponenteDivisionPoliticaPartial.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Views\Shared\_ComponenteOrganismoTransitoPartial.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Views\Shared\_ComponenteAgregarPersonaPartial.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Views\Shared\_ComponentePersonasPartial.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="Views\Shared\_PageScriptsPartial.cshtml">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\Controllers\" />
<Folder Include="Resources\Models\" />
</ItemGroup>
</Project>
Then, into each dll project I added _ViewImports.cshtml to share imports and enable taghelpers and other things, as shown below:
Lastly, I configured the Startup class to add application parts:
After solving some issues, I achieved to run those views successfully from the main application. However I had to migrate the framework from 3.1 to NET 6, I solved different errors but for some reason, application parts' assemblies reported different errors and all related with usings, injects and others when compiling all Views/Shared, because the content of the _ViewImports files weren't recognized or loaded by default in Visual Studio 2022. As you know this ViewImport file is to configure imports, injects, etc, as a shared resource for all views.
As a workaround, I had to copy the content of the view imports and paste it into each View file to get each project compiled successfully, as shown below in a cshtml View:
I appreciate any help.
I solved the issue by removing EnableDefaultContentItems entry from PropertyGroup in all csproj related with razor views.
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
This link is related and explain about it.
I have 2 projects in a solution, and I am not sure why I am running into this error for the 1st project when building the solution.
Error CS0579 Duplicate
'global::System.Runtime.Versioning.TargetFrameworkAttribute'
Ive tried the following answer, Cleaned and Rebuilt, but it didn't help.
Add the following two lines to the <PropertyGroup>.
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
And this answer says to delete the assemeblyinfo.cs file from project under properties menu and rebuild it, but I don't even see an Assemblyinfo.cs file under properties...
I've also commented out the assembly line per a different answer, and still it failed:
// <autogenerated />
using System;
using System.Reflection;
//[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v3.1", FrameworkDisplayName = "")]
Here are my .csproj files:
Project1:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.10" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DI\DI.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="HttpTrigger1/readme.md">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Project2:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Storage.Files.DataLake" Version="12.6.0" />
<PackageReference Include="Azure.Storage.Queues" Version="12.6.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DI\DI.csproj" />
</ItemGroup>
</Project>
I got this error because I integrated two projects by removing the csproj file from Project A then including the root folder in Project B. I failed to remove the .bin and .obj folders from Project A. Hope this saves somebody some unnecessary grief.
Deleting Assembly files from Release and Debug folder resolved the issue. They must've been added at some point when I built the project.
Solved by running git clean -xdf to remove artefacts from previous builds. After that, built successfully.
I got the same problem after suppressing a level of hierarchy in my solution. The folder structure was like
/Project
/WebApi
/WebApi
/WebApi
Program.cs
webapi.csproj
I wanted to suppress a level of hierarchy so I copied the last WebApi under the first WebApi folder. After copying the projects I forgot to delete the source folders and began to experience the same error as you.
It was due to duplicated project folders :
/Project
/WebApi
/WebApi
Program.cs
webapi.csproj
/WebApi
Program.cs
webapi.csproj
After original folder deletion the problem was gone, final hierachy:
/Project
/WebApi
/WebApi
Program.cs
webapi.csproj
When I add this code directly to the .csproj file, the reference is resolved correctly:
<ItemGroup>
<Reference Include="base_csharp">
<HintPath>D:\Repositories\MDSBuild\MdsDrivers\deps\Base\install\bin\Windows\MSVC\x86\Debug\base_csharp.dll</HintPath>
</Reference>
</ItemGroup>
However, when I put the exact same code in a props file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Reference Include="base_csharp">
<HintPath>D:\Repositories\MDSBuild\MdsDrivers\deps\Base\install\bin\Windows\MSVC\x86\Debug\base_csharp.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
</Project>
And import that props file into the .csproj
<Import Project="D:\Repositories\MDSBuild\MdsDrivers\deps\Base\scripts\props\base_csharp.props" /
I get the warning that the reference could not be resolved:
warning MSB3245: Could not resolve this reference. Could not locate the assembly "base_csharp". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
The code is exactly the same and all the paths are absolute, but for some inexplicable reason it is unable to find the .dll file. What am I doing wrong?
One problem is that you have opened 2 "ItemGroup" tags and closed only one.
If your path is absolute you can give it like this:
<ItemGroup>
<Reference Include= "D:\Repositories\MDSBuild\MdsDrivers\deps\Base\install\bin\Windows\MSVC\x86\Debug\base_csharp.dll" />
</ItemGroup>
How can I include a file in the list of files in solution explorer without including it as a dependency for compilation?
I have a .targets file that generates .cs files, similar to the examples in this answer.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<CoreCompileDependsOn>$(CoreCompileDependsOn);GenerateCode</CoreCompileDependsOn>
</PropertyGroup>
<ItemGroup>
<Sources Include="..\sources\*.txt" />
</ItemGroup>
<Target Name="GenerateCode" Inputs="#(Sources)" Outputs="#(Sources->'generated\%(Filename).cs')">
<!-- run executable that generates files -->
<ItemGroup>
<Compile Include="generated\*.cs" />
</ItemGroup>
</Target>
</Project>
This builds correctly and consecutive builds don't rebuild the project unnecessarily. The resulting .cs files are not visible in the solution explorer. The generated code also isn't found by intellisense.
If I add the files with ItemGroups in the .csproj, the generated files are visible in the solution explorer, but subsequent builds result in rebuilding the project unnecessarily. The genereated code still isn't found by intellisense.
<ItemGroup>
<Sources Include="..\sources\*.txt">
<Link>sources\%(Filename)%(Extension)</Link>
</Sources>
<!-- using None instead of Compile on the next line makes no difference -->
<Compile Include="#(Sources->'generated\%(Filename).cs')">
<Generator>MSBuild:Compile</Generator>
<Link></Link>
</Compile>
</ItemGroup>
How can I tell msbuild that the .cs files included in the project are inconsequential to the build and therefore shouldn't trigger rebuilding the entire project?
Move the code generation to BeforeCompile instead of CoreCompileDependsOn. this will keep the generation of the files from tirggering the subsequent builds.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="BeforeCompile" DependsOnTargets="GenerateCode">
</Target>
<Target Name="GenerateCode" Inputs="#(Sources)" Outputs="#(Sources->'generated\%(Filename).cs')">
<!-- run executable that generates files -->
</Target>
</Project>
If you include all of the generated files in the .csproj, the visual studio intellisense will work.
<ItemGroup>
<Sources Include="..\sources\*.txt">
<Link>sources\%(Filename)%(Extension)</Link>
<LastGenOutput>generated\%(Filename).cs</LastGenOutput>
</Sources >
<Compile Include="#(Sources->'generated\%(Filename).cs')">
<Link></Link>
</Compile>
</ItemGroup>
I am trying to embed a resource in my CSPROJ from two different directories, depending on the configuration. This post gave me the idea, but it's not working. Any help is appreciated.
<Choose>
<When Condition="'$(Configuration)' == 'Debug'">
<ItemGroup>
<EmbeddedResource Include="..\Debug\file.txt">
<Link>Files\file.txt</Link>
</EmbeddedResource>
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<EmbeddedResource Include="..\Release\file.txt">
<Link>Files\file.txt</Link>
</EmbeddedResource>
</ItemGroup>
</Otherwise>
</Choose>
I have also tried this but it worked equally bad.
<ItemGroup>
<EmbeddedResource Include="..\$(Configuration)\file.txt">
<Link>Files\file.txt</Link>
</EmbeddedResource>
</ItemGroup>
You only need to place the condition on the ItemGroup element:
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<EmbeddedResource Include="..\Debug\file.txt">
<Link>Files\file.txt</Link>
</EmbeddedResource>
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Release'">
<EmbeddedResource Include="..\Release\file.txt">
<Link>Files\file.txt</Link>
</EmbeddedResource>
</ItemGroup>
As I said in comments to your question this should work for you:
<ItemGroup>
<EmbeddedResource Include="..\$(Configuration)\file.txt">
<Link>Files\file.txt</Link>
</EmbeddedResource>
</ItemGroup>
Even though you might see old values in "Full Path" of VS's property editor - when you build, it will respect your current configuration. VS Property Editor should be updated by Refresh button of Solution explorer or reloading project in the worst case. May be changing selection to other file and coming back to file.txt will be enough to refresh property editor.
UPDATE:
I've figured out in which case Full path changed for me by hitting "refresh" button of Solution explorer - it's Dll Reference's Hint path.
<Reference Include="MyDll">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\$(Configuration)\MyDll.dll</HintPath>
</Reference>
This will work only in case all DLLs (in all target folders) do actually exist.
For some reason for Item files Full path are not refreshed - for file items VS always think that current configuration named Debug - EVEN IF YOU DELETE DEBUG CONFIGURATION FROM THE PROJECT. Fortunately this VS Bug does not impact build - it still will take valid files.