I got a C#/Docker project from a coworker and have to use it with my own service. It is an REST API. My coworker used Visual Studio Community 2019 for this. I now want to run it on a linux machine with docker installed. There is a docker file. When I run docker build I get an error message that there is no main method specified in the *.csproj file. Therefore I specified the startup object in the csproj file. It was missing before.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<StartupObject>Program</StartupObject> // <-- I added this line nothing else
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.4.10" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.9" />
</ItemGroup>
</Project>
But I still get an error message, when running docker build:
user@notebook:~/Schreibtisch/DeviceApi/DeviceApi$ sudo docker build -t deviceApi .
[...]
CSC : error CS1555: Could not find 'Program' specified for Main method [/src/DeviceApi/DeviceApi.csproj]
The project structure is as follows. There is a directory named DeviceApi. That's my working directory (as you can see from my command line aboce). In that directory there are these files next to the other project files: Dockerfile, DeviceApi.csproj, Program.cs (which contains the main method)
user@notebook:~/Schreibtisch/DeviceApi/DeviceApi$ ls
appsettings.Development.json DeviceApi.csproj Program.cs
appsettings.json DeviceApi.csproj.user Properties
bin Handler ResponseFilter
Controllers Startup.cs DBManager
Models Util Dockerfile
obj wwwroot
Here is the content of the Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch AS build
WORKDIR /src
COPY ["DeviceApi.csproj", "DeviceApi/"]
RUN dotnet restore "DeviceApi/DeviceApi.csproj"
COPY . .
WORKDIR "/src/DeviceApi"
RUN dotnet build "DeviceApi.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "DeviceApi.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "DeviceApi.dll"]
Here is the content of Program.cs:
namespace DeviceApi
{
public class Program
{
public static ILogger<Program> LOGGER = null;
public static IConfiguration CONF = null;
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
LOGGER = host.Services.GetRequiredService<ILogger<Program>>();
CONF = Startup.Configuration;
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => [...] ;
}
}
My coworker used Visual Studio for this. I didn't use C# before and only have little experience with Visual Studio. I guess/hope there is a small user error on my side, but I don't know what I did wrong.
Thanks for your effort.