DEV Community

Onorio Catenacci
Onorio Catenacci

Posted on

3 1

An MSBuild File To Work With .Net Core As Well

I sometimes take some extra time and effort to create some files from scratch simply because I don't want all the extraneous fluff that comes with letting automated utilities build a file. For example, if I create a new solution in Visual Studio there will be a .csproj file created as part of the process. And that .csproj will be filled with all sorts of extraneous settings that I don't want or care about. So every now and again I build an MSBuild .csproj from scratch.

I had an MSBuild file that worked fine for building a small/simple library that I created. But then I tried to build the library with dotnet core and I ran into an issue.

Digging into the issue a bit I found that basically MSBuild does some hidden wizardry to include some files without being told to include them. Specifically it includes mscorlib.dll without the file ever getting mentioned. But dotnet core doesn't have that implied behavior.

So without any further ado here's my modified build file that works with both MSBuild and dotnet build for whatever it's worth. I hope it helps others.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup> 
    <Compile Include="ColorNumberGameEvent.cs" /> 
    <Compile Include="GameStates.cs" />
    <Compile Include="LegalColors.cs" />
    <Compile Include="GameSolution.cs" />
  </ItemGroup>
  <ItemGroup>
    <References Include="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client" />  <!-- Included for dotnet build -->
  </ItemGroup>                        
  <Target Name="Build">
    <Csc TargetType="library" Sources="@(Compile)"
       TreatWarningsAsErrors="true"
       WarningLevel="4"
       AdditionalLibPaths="@(References)"
       References="mscorlib.dll"/> <!-- Included for dotnet build -->
  </Target>
</Project>
Enter fullscreen mode Exit fullscreen mode

A couple of notes:

1.) I would have used the ProgramFiles(x86) environment variable (rather than hard coding the path) but there's a bug with MSBuild and environment variables that contain "("

2.) You need both the AdditionalLibPaths and the References set in order for this to work.

πŸ‘‹ While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay