How are old versions of a application handled with the PublishSingleFile option? - .net-core

Do aplications build with the PublishSingleFile clean up old versions? If not is there a way to reliably clean them up automatically?
Csproj example for reference:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
<RuntimeIdentifier>ubuntu.16.04-x64</RuntimeIdentifier>
</PropertyGroup>

Related

How to set OS condition for TargetFramework in MSBuild

I have few projects in my solution - few for mobile (Xamarin.Forms) and one xUnit. xUnit is build with .NET5 and references mobile project.
There are 2 pipelines on Azure DevOps to build mobile app. One is for Android and runs on Windows, another for iOS and runs on OSX.
xUnit project build started to fail after I added new NuGet to mobile project (Rg.Plugins.Popup).
Initially, setting the TargetFramework for xUnit project looked like this
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
That setup worked on OSX, but did not work on Windows with error message Error NETSDK1136: The target platform must be set to Windows (usually by including '-windows' in the TargetFramework property) when using Windows Forms or WPF, or referencing projects or packages that do so.
Changing it to
<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
fixes Windows problem, but it starts failing on OSX (with same error message, which is weird, as we are no longer on Windows).
I tried few other options, but could not make it to succeed on both machines.
<PropertyGroup>
<TargetFramework Condition=" '$(OS)' == 'Windows_NT' ">net5.0-windows</TargetFramework>
<TargetFramework Condition=" '$(OS)' != 'Windows_NT' ">net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>net5.0;net5.0-windows</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
I also tried with Choose, When and Otherwise, but it looks like you cannot wrap TargetFramework into that at all.
Am I doing it completely wrong or it it just some coincidence that correct code does not work in my case?
Try to place Condition inside PropertyGroup not TargetFramework .
Try the code below
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
</PropertyGroup>

Can a .NET 5 project be set up to always build with release configuration?

I have a small .NET 5 project whose project file is shown below.
This project will be used to run performance benchmarks against my main project. As such, I'd like to always build it with the release configuration.
The documentation for the -c option of dotnet build command does say the following, but I haven't found any reference to it in the project file section:
The default for most projects is Debug, but you can override the build
configuration settings in your project.
Is there a way to specify this in the project file rather than having to write dotnet build -c release each time I run it?
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Domain\Domain.fsproj" />
</ItemGroup>
</Project>
Yes, you can add <Configuration>Release</Configuration> to the property group. See the corresponding MSBuild documentation.

Debug specific PropertyGroup in csproj file

Is it possible to have a section of my csproj file in dotnet core (3.1.1) which is only included when debugging? I'd like to accomplish something like the following
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <!--always valid-->
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<AssemblyName>SomeAssembly</AssemblyName>
<RootNamespace>Some.Root.Namespace</RootNamespace>
</PropertyGroup>
<PropertyGroup> <!--Debugonly-->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
....
</Project>
Yes, use conditional PropertyGroup:
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<!--Debugonly-->
</PropertyGroup>

Visual Studio always restoring because Directry.Build.props

I created a Directory.Build.props file, with VersionSuffix contain the current Hour and Minutes:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VersionSuffix>$([System.DateTime]::Now.ToString(yyyy-MM-dd-HHmm))-$(Configuration)</VersionSuffix>
</PropertyGroup>
</Project>
Now, because this file changed every minute, the Visual Studio complete it auto-restore, and start it again, because after a minute the properties are different.
So, I added a Target to update the properties only before the Build:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Version" BeforeTargets="Build">
<PropertyGroup>
<VersionSuffix>$([System.DateTime]::Now.ToString(yyyy-MM-dd-HHmm))-$(Configuration)</VersionSuffix>
</PropertyGroup>
</Target>
</Project>
Now the Visual Studio works fine, but from the command-line (msbuild MySln.sln /t:Build...) the properties don't apply.
(I tried with Directory.Build.targets and it acts the same (unless I missed something))
(You can try with more simple property, Company for example).
How can I use a time-based property, but apply it only when I creating the DLL?
Using InitialTargets solved my problem:
<Project InitialTargets="Version" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Version">
<PropertyGroup>
<VersionSuffix>$([System.DateTime]::Now.ToString(yyyy-MM-dd-HHmm))-$(Configuration)</VersionSuffix>
</PropertyGroup>
</Target>
</Project>

How to specify ASP.NET Core target framework imports in .csproj file (instead of project.json)?

I'm building an ASP.NET Core app, and am trying to install the Azure Storage package.
From the Azure Storage github page, it says I need to place the following in my project.json file - but since this is using the latest ASP.NET Core version, we don't have a project.json file, just a .csproj file.
"imports": [
"dnxcore50",
"portable-net451+win8"
]
Is there a way to do this in .csproj file? I assume the place might be somewhere around this:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
Thanks very much!
After migrating one of my projects to the new model, this is what it generated:
<PropertyGroup>
<TargetFramework>netcoreapp1.6</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>TestApp</AssemblyName>
<OutputType>Exe</OutputType>
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp1.6' ">$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
Try adding dnxcore50 and portable-net451+win8 in a similar fashion, something like this:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">$(PackageTargetFallback);dnxcore50;portable-net451+win8</PackageTargetFallback>
</PropertyGroup>

Resources