Home > Blockchain >  Class Library, .NET 3.1 core : Error CS5001 Program does not contain a static Main Method
Class Library, .NET 3.1 core : Error CS5001 Program does not contain a static Main Method

Time:09-30

I realize that similar questions have various answers, however in my case I have a .net Core 3.1 project and my project is a Class Library, so why is the compiler requesting me to have a Main entry point? The answers in other questions basically suggest to use Class Library, but that does not seem to work on .net Core 3.1

enter image description here

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Tutorials_UnitTest</RootNamespace>
<IsPackable>false</IsPackable>
<OutputType>Library</OutputType>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

 <ItemGroup>
 <PackageReference Include="coverlet.collector" Version="3.1.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers ;buildtransitive</IncludeAssets>
 </PackageReference>
 <PackageReference Include="coverlet.msbuild" Version="3.1.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Tutorials\Tutorials.csproj" />
</ItemGroup>

</Project>

NOTE: I have tried clean rebuild, restarting Visual Studio, deleting all /bin, /obj directories, but still fails.

CodePudding user response:

This is caused by the unit test framework packages:

<ItemGroup>
  <PackageReference Include="coverlet.collector" Version="1.3.0">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers ;buildtransitive</IncludeAssets>
  </PackageReference>
  <PackageReference Include="coverlet.msbuild" Version="2.9.0">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  </PackageReference>
  <PackageReference Include="NUnit" Version="3.12.0" />
  <PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
  <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>

Specifically, it's the "coverlet.collector" that causes the issue.

If your library is NOT a unit test library (which it appears it is not) then just remove that entire <ItemGroup>.

Otherwise, you can fix it by adding the following to the <PropertyGroup> part of the project file:

<GenerateProgramFile>false</GenerateProgramFile>
  • Related