Debug specific PropertyGroup in csproj file - .net-core

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>

Related

Warning about server GC in F# project

I am working on my very first F# project and I am experimenting with the Hopac library.
I am on dotnet version 3.1.300 on a Mac. I have initialized my project using the following:
dotnet new console --language F#
dotnet add package Hopac
And after writing using the Hopac library ran the program in the following way:
dotnet run
The runtime behaviour of the program works according to my expectations but I get the following warning:
WARNING: You are using single-threaded workstation garbage collection, which means that parallel programs cannot scale. Please configure your program to use server garbage collection.
As recommended in a few threads in SO to add the following clause:
<ServerGarbageCollection>true</ServerGarbageCollection>
I have tried the following in my fsproj configuration:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Hopac" Version="0.4.1" />
</ItemGroup>
</Project>
Additionally I have also tried:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Hopac" Version="0.4.1" />
</ItemGroup>
</Project>
but the same warning persists. I am an absolute novice on .NET configuration files so am I making some obvious errors? Thanks
Create a runtimeconfig.template.json at the root of your project
{
"configProperties": {
"System.GC.Server": true
}
}
To verify is working, inside your code print:
printfn "%A" System.Runtime.GCSettings.IsServerGC
It should be true.
For some reason the SDK version of the setting does not work.

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 are old versions of a application handled with the PublishSingleFile option?

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>

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>

How can I publish site from command line with some publish profile?

Something like
msbuild /t:publish [use PublishProfileName] someproject.csproj
msbuild MyProject.csproj /t:PipelinePreDeployCopyAllFilesToOneFolder /p:Configuration=Release;_PackageTempDir=C:\temp\somelocation;AutoParameterizationWebConfigConnectionStrings=false
See MSBuild 2010 - how to publish web app to a specific location (nant)?
For Visual Studio 2012 you can use
msbuild MySolution.sln /p:DeployOnBuild=true;PublishProfile=Production;Password=foo
See ASP.NET Web Deployment using Visual Studio: Command Line Deployment
This is an alternative solution for achieving Pavel's solution but using MsBuild target in a MsBuild file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<OutputDirectory>$(DeploymentProject)\bin\$(Configuration)</OutputDirectory>
<OutputPath>C:\Inetpub\wwwroot</OutputPath>
</PropertyGroup>
<Target Name="build">
<MSBuild
Projects="Your Solution File.sln"
Properties="Configuration=$(Configuration);DeployOnBuild=true;DeployTarget=Package;_PackageTempDir=$(OutputPath);AutoParameterizationWebConfigConnectionStrings=false"
>
</MSBuild>
</Target>
</Project>

Resources