1.Create a simple project with .net core 2.1
2.Change LangVersion to 7.3
3.Try use something from c# 7.3 and build application with multistage build using visualstudio generated dockerfile
Run like when you run without docker
dotnet build says to use c# 7.0 or above
@iceoz - Can you share what your Dockerfile looks like? Can you run the docker run --rm microsoft/dotnet:2.1-sdk dotnet --version and paste the resulting version that is emitted? I'm curious if you have an older sdk image cached on you machine. You might try pulling the latest 2.1 sdk image - docker pull microsoft/dotnet:2.1-sdk
@MichaelSimons --version returned 2.1.401
@MichaelSimons my dockerfile
FROM microsoft/dotnet:2.1-runtime AS base
WORKDIR /app
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY Teste.Integrador/Teste.Integrador.csproj Teste.Integrador/
RUN dotnet restore Teste.Integrador/Teste.Integrador.csproj
COPY . .
WORKDIR /src/Teste.Integrador
RUN dotnet build Teste.Integrador.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish Teste.Integrador.csproj -c Release -o /app
FROM base AS final
ENV APPSETTINGS__SENTRY__PROJECTURL=http://75ca5d3b65724046814abc123b6a24d9:81368c9023d048619eb6fad27dcb0ffe@localhost:8030/37
ENV APPSETTINGS__GRAYLOG__SERVERADDRESS=192.168.231.114
ENV APPSETTINGS__GRAYLOG__SERVERPORT=12201
ENV APPSETTINGS__REDIS__SERVERADDRESS=10.18.0.70
ENV APPSETTINGS__REDIS__SERVERPORT=6379
ENV APPSETTINGS__REDIS__SERVERDB=13
ENV APPSETTINGS__REDIS__PASSWORD=abcd1234
ENV APPSETTINGS__REDIS__TIMEOUT=10000
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Teste.Integrador.dll"]
@iceoz - can you share your csproj and the build output? I tried your scenario locally and it works as I expect. A standalone set of steps to reproduce the problem would be helpful.
Docker build
PS C:\Source\Teste\Teste.Integrador> docker build -t Teste.integrador:1.0.1 .
Sending build context to Docker daemon 2.633MB
Step 1/23 : FROM microsoft/dotnet:2.1-runtime AS base
---> 870fda08c907
Step 2/23 : WORKDIR /app
---> Using cache
---> e05918a18307
Step 3/23 : FROM microsoft/dotnet:2.1-sdk AS build
---> bde01d9ed6eb
Step 4/23 : WORKDIR /src
---> Using cache
---> f0180d50cd09
Step 5/23 : COPY Teste.Integrador/Teste.Integrador.csproj Teste.Integrador/
---> 5b6cb7ac2955
Step 6/23 : RUN dotnet restore Teste.Integrador/Teste.Integrador.csproj
---> Running in f2da03e979f9
Restoring packages for /src/Teste.Integrador/Teste.Integrador.csproj...
Installing System.Security.Cryptography.ProtectedData 4.5.0.
Installing System.Security.Cryptography.ProtectedData 4.4.0.
Installing System.Configuration.ConfigurationManager 4.5.0.
Installing System.Configuration.ConfigurationManager 4.4.0.
Installing Newtonsoft.Json 10.0.3.
Installing System.Collections.NonGeneric 4.0.1.
Installing System.Text.Encoding.CodePages 4.4.0.
Installing Flurl 2.8.0.
Installing System.Diagnostics.PerformanceCounter 4.5.0.
Installing Pipelines.Sockets.Unofficial 1.0.0.
Installing SharpRaven 2.4.0.
Installing Serilog 2.0.0.
Installing RabbitMQ.Client 5.1.0.
Installing Serilog.Sinks.Console 3.0.1.
Installing Serilog 2.5.0.
Installing System.Reflection.TypeExtensions 4.4.0.
Installing Serilog.Exceptions 4.1.0.
Installing Serilog 2.7.1.
Installing Serilog.Sinks.ColoredConsole 3.0.1.
Installing Serilog.Sinks.Graylog 2.0.2.
Installing Serilog.Sinks.Sentry 2.1.6.
Installing StackExchange.Redis 2.0.495.
Installing Flurl.Http 2.4.0.
Generating MSBuild file /src/Teste.Integrador/obj/Teste.Integrador.csproj.nuget.g.props.
Generating MSBuild file /src/Teste.Integrador/obj/Teste.Integrador.csproj.nuget.g.targets.
Restore completed in 17.67 sec for /src/Teste.Integrador/Teste.Integrador.csproj.
Removing intermediate container f2da03e979f9
---> 6d4852e62065
Step 7/23 : COPY . .
---> 4c62fcf72936
Step 8/23 : WORKDIR /src/Teste.Integrador
---> Running in 4cc89b371ed9
Removing intermediate container 4cc89b371ed9
---> bf834c776417
Step 9/23 : RUN dotnet build Teste.Integrador.csproj -c Release -o /app
---> Running in a9a42dcc1115
Microsoft (R) Build Engine version 15.8.166+gd4e8d81a88 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restoring packages for /src/Teste.Integrador/Teste.Integrador.csproj...
Generating MSBuild file /src/Teste.Integrador/obj/Teste.Integrador.csproj.nuget.g.props.
Generating MSBuild file /src/Teste.Integrador/obj/Teste.Integrador.csproj.nuget.g.targets.
Restore completed in 297.84 ms for /src/Teste.Integrador/Teste.Integrador.csproj.
Program.cs(10,30): error CS8107: Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater. [/src/Teste.Integrador/Teste.Integrador.csproj]
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/Teste.Integrador/Teste.Integrador.csproj]
Build FAILED.
Program.cs(10,30): error CS8107: Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater. [/src/Teste.Integrador/Teste.Integrador.csproj]
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/Teste.Integrador/Teste.Integrador.csproj]
0 Warning(s)
2 Error(s)
Time Elapsed 00:00:02.81
The command '/bin/sh -c dotnet build Teste.Integrador.csproj -c Release -o /app' returned a non-zero code: 1
.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Flurl.Http" Version="2.4.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" />
<PackageReference Include="RabbitMQ.Client" Version="5.1.0" />
<PackageReference Include="Serilog.Exceptions" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Graylog" Version="2.0.2" />
<PackageReference Include="Serilog.Sinks.Sentry" Version="2.1.6" />
<PackageReference Include="StackExchange.Redis" Version="2.0.495" />
</ItemGroup>
</Project>
this is the code from program.cs
using Inlog.City.Integrador.Infra.Config;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading.Tasks;
namespace Teste.Integrador
{
internal static class Program
{
private static async Task Main(string[] args)
{
var hostBuilder = new HostBuilder().ConfigureServices((hostContext, services) =>
{
var configuration = ConfigResolver.CreateConfig();
hostContext.Configuration = configuration;
IoCResolver.CreateServiceProvider(configuration, services);
});
await hostBuilder.RunConsoleAsync();
}
}
}
@iceoz - Why are you conditionally defining the LangVersion property?
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>7.3</LangVersion>
</PropertyGroup>
Your Dockerfile is compiling Release
RUN dotnet build Teste.Integrador.csproj -c Release -o /app
Remove the condition and your Dockerfile will compile.
@MichaelSimons I'm using visual studio 2017, and to make things works using c# 7.3 i need to go in project => properties => build => advanced and set the language version to 7.3 or C# latest minor version. when i did that visual studio put the language version inside .csproject, it's no me.
either way removing language version and run the build again not work, removing all propertygroup also not working, same error on docker build
@iceoz If you want to use VS to make the change, then change your Configuration to All Configuration at the top of the build properties page. Alternatively you can edit the csproj directly. You want to keep the LangVersion but set it in a PropertyGroup that doesn't have a Condition such as the following.
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
If you build in release mode locally you will run into the same issue the way your project is currently defined.
now it's working, thanks
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Most helpful comment
@iceoz If you want to use VS to make the change, then change your
ConfigurationtoAll Configurationat the top of the build properties page. Alternatively you can edit the csproj directly. You want to keep the LangVersion but set it in a PropertyGroup that doesn't have a Condition such as the following.