简体   繁体   中英

Compile c# project with 2 different sdk's + frameworks

I'm working on a project (let's call it project a) which contians Models etc. and a part of it is a REST-Api, so I'm using AspNetCore.
I have another project (project b) which shall use the models from project a. So what I'm trying to achieve is using parts of project a inside project b.

Now the complicated part.
Project a is hosted on GitLab and we're using GitLab-CI with auto deployment.
At this point I want to have a compiling of a lib with parts of project a at push and a push to NuGet as well. Project b shall use the created NuGet-Package (but that's not the problem here).

A split into a third project of said models etc. which are used in both project is not possible (I will leave out the reasons for this, that's not the matter here).

My idea is to create a second project in the solution of project a (let's call it project c) which is pointing to the sources of project a (but not linking to project a as a dependency).

So I'm now having 2.csproj-files in my solution which contains project a and project c.
.csproj-a needs to use Microsoft.NET.Sdk.Web as Sdk, .csproj-c the Microsoft.NET.Sdk. proj a should use netcoreapp3.1 as target framework, proj c netstandard2.1. My idea was to have the project-specific-parts in the.csproj-files and the shared parts in a files called make.targets which is included in the.csproj.

Now I'm getting errors while compiling:

PS C:\Users\Nicolai\Development\Server\wawi-api> dotnet build .\WawiApi.csproj -c Release -f netcoreapp3.1
Microsoft (R)-Build-Engine, Version 16.8.0+126527ff1 für .NET
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.

  Wiederherzustellende Projekte werden ermittelt...
  Alle Projekte sind für die Wiederherstellung auf dem neuesten Stand.
CSC : warning CS2002: Source file 'C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\WawiApi.Version.cs' specified multiple times [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
CSC : warning CS2002: Source file 'C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\.NETCoreApp,Version=v3.1.AssemblyAttributes.cs' specified multiple times [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netstandard2.1\.NETStandard,Version=v2.1.AssemblyAttributes.cs(4,12): error CS0579: Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netstandard2.1\WawiApi-Library.AssemblyInfo.cs(16,12): error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netstandard2.1\WawiApi-Library.AssemblyInfo.cs(17,12): error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netstandard2.1\WawiApi-Library.AssemblyInfo.cs(20,12): error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\WawiApi.AssemblyInfo.cs(15,12): error CS0579: Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\WawiApi.AssemblyInfo.cs(16,12): error CS0579: Duplicate 'System.Reflection.AssemblyConfigurationAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\WawiApi.AssemblyInfo.cs(17,12): error CS0579: Duplicate 'System.Reflection.AssemblyProductAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\WawiApi.AssemblyInfo.cs(18,12): error CS0579: Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]

Fehler beim Buildvorgang.

CSC : warning CS2002: Source file 'C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\WawiApi.Version.cs' specified multiple times [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
CSC : warning CS2002: Source file 'C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\.NETCoreApp,Version=v3.1.AssemblyAttributes.cs' specified multiple times [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netstandard2.1\.NETStandard,Version=v2.1.AssemblyAttributes.cs(4,12): error CS0579: Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netstandard2.1\WawiApi-Library.AssemblyInfo.cs(16,12): error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netstandard2.1\WawiApi-Library.AssemblyInfo.cs(17,12): error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netstandard2.1\WawiApi-Library.AssemblyInfo.cs(20,12): error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\WawiApi.AssemblyInfo.cs(15,12): error CS0579: Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\WawiApi.AssemblyInfo.cs(16,12): error CS0579: Duplicate 'System.Reflection.AssemblyConfigurationAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\WawiApi.AssemblyInfo.cs(17,12): error CS0579: Duplicate 'System.Reflection.AssemblyProductAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
C:\Users\Nicolai\Development\Server\wawi-api\obj\Release\netcoreapp3.1\WawiApi.AssemblyInfo.cs(18,12): error CS0579: Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute [C:\Users\Nicolai\Development\Server\wawi-api\WawiApi.csproj]
    2 Warnung(en)
    8 Fehler

I honestly can't see my mistake.

Here are a few more information about my config:

WawiApi.csproj (proj a)

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UserSecretsId>ni-ce-sec-ret-id</UserSecretsId>
    <LangVersion>8.0</LangVersion>
    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
    <Configurations>Debug;Release</Configurations>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="**/*.cs" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
    <PackageReference Include="Quartz" Version="3.0.7" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.4.1" />
    <PackageReference Include="Swashbuckle.AspNetCore.ReDoc" Version="5.4.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
  </ItemGroup>
  <Import Project=".\make.targets" />
</Project>

WawiApi-Library.csproj (proj c)

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <OutputType>Library</OutputType>
    <LangVersion>8.0</LangVersion>
    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
    <Configurations>Debug;Release</Configurations>
    <BuildDir>Library</BuildDir>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="**/*.cs" Exclude="Program.cs;Startup.cs;Controllers/*.cs" />
  </ItemGroup>
  <Import Project=".\make.targets" />
</Project>

make.targets

<Project>
     <ItemGroup>
        <PackageReference Include="LinqKit.Microsoft.EntityFrameworkCore" Version="3.0.0" />
        <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.3">
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
          <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.6" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="TimeZoneConverter" Version="3.2.0" />
  </ItemGroup>
</Project>

I figured out what my mistake was.
It seems like proj a & proj c are somehow linked, so I doublechecked the compiling-process and error-messages.
I changed the logic from "include everything but given files" to "include all files, but remove the unnecessary in proj c ".

In WawiApi-Library.proj (proj c) I changed the value from EnableDefaultCompileItems to true and removed the files I excluded before and don't include the other files (again). In WawiApi.proj (proj a) I also set the value of EnableDefaultCompileItems to true to include all files by default at compiling.

Long story short and changes I made:

WawiApi.proj (proj a)

<EnableDefaultCompileItems>false</EnableDefaultCompileItems>

to

<EnableDefaultCompileItems>true</EnableDefaultCompileItems>

to include all files by default

WawiApi-Library.proj (proj c)

<EnableDefaultCompileItems>false</EnableDefaultCompileItems>

to

<EnableDefaultCompileItems>true</EnableDefaultCompileItems>

to include all files by default
and

<Compile Include="**/*.cs" Exclude="Program.cs;Startup.cs;Controllers/*.cs" />

to

<Compile Remove="Program.cs;Startup.cs;[...]" />

to removed unnecessary files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM