DotNet Core 3.1-具有共享src、obj和bin的多个项目



我有以下文件夹结构(遵循面向功能的组织(:

Solution
bin
obj
src
Module1
Shared.cs
Module2
Shared.cs
Module3
Shared.cs
Server
Program.cs
Startup.cs
Database
Program.cs
Solution.sln
Server.csproj
Database.csproj

对于Server项目,我使用Microsoft.NET.SdkMicrosoft.NET.Sdk.Web作为Database

这是实际的Server.csproj:

<Project>
<PropertyGroup>
<TargetFramework>NetCoreApp3.1</TargetFramework>
<Configuration Condition=" '$(Configuration)' == '' ">Development</Configuration>
<EnvironmentName>$(Configuration)</EnvironmentName>
<BaseOutputPath>bin/$(MSBuildProjectName)</BaseOutputPath>
<OutputPath>$(BaseOutputPath)/$(Configuration)</OutputPath>
<BaseIntermediateOutputPath>obj/$(MSBuildProjectName)</BaseIntermediateOutputPath>
<IntermediateOutputPath>$(BaseIntermediateOutputPath)/$(Configuration)</IntermediateOutputPath>
<AppendTargetFrameworkToOutputPath>False</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>False</AppendRuntimeIdentifierToOutputPath>
<EnableDefaultContentItems>False</EnableDefaultContentItems>
<Nullable>Enable</Nullable>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<RootNamespace>OeMis.Server</RootNamespace>
<StartupObject>OeMis.Server.ServerEntry</StartupObject>
</PropertyGroup>
<ItemGroup>
<Content Include="Configuration.$(Configuration).json" ExcludeFromSingleFile="True" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest"/>
<PackageReference Include="Dapper" Version="2.0.30"/>
<PackageReference Include="HotChocolate" Version="11.0.0-preview.95"/>
<PackageReference Include="HotChocolate.AspNetCore" Version="11.0.0-preview.95"/>
<PackageReference Include="HotChocolate.AspNetCore.Authorization" Version="11.0.0-preview.95"/>
<PackageReference Include="HotChocolate.AspNetCore.Playground" Version="11.0.0-preview.95"/>
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.0.0-preview1.20021.1"/>
</ItemGroup>
<Import Sdk="Microsoft.NET.Sdk.Web" Project="Sdk.props"/>
<Import Sdk="Microsoft.NET.Sdk.Web" Project="Sdk.targets"/>
</Project>

这是Database.csproj:

<Project>
<PropertyGroup>
<TargetFramework>NetCoreApp3.1</TargetFramework>
<Configuration Condition=" '$(Configuration)' == '' ">Development</Configuration>
<EnvironmentName>$(Configuration)</EnvironmentName>
<OutputType>Exe</OutputType>
<BaseOutputPath>bin/$(MSBuildProjectName)</BaseOutputPath>
<OutputPath>$(BaseOutputPath)/$(Configuration)</OutputPath>
<BaseIntermediateOutputPath>obj/$(MSBuildProjectName)</BaseIntermediateOutputPath>
<IntermediateOutputPath>$(BaseIntermediateOutputPath)/$(Configuration)</IntermediateOutputPath>
<AppendTargetFrameworkToOutputPath>False</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>False</AppendRuntimeIdentifierToOutputPath>
<EnableDefaultContentItems>False</EnableDefaultContentItems>
<Nullable>Enable</Nullable>
<RootNamespace>OeMis.Database</RootNamespace>
<StartupObject>OeMis.Database.DatabaseEntry</StartupObject>
</PropertyGroup>
<ItemGroup>
<Content Include="Configuration.$(Configuration).json" ExcludeFromSingleFile="True" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest"/>
<PackageReference Include="Dapper" Version="2.0.30"/>
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.0.0-preview1.20021.1"/>
<PackageReference Include="Bogus" Version="29.0.1"/>
</ItemGroup>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.props"/>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets"/>
</Project>

我总是在src/Server/Program.cs中遇到错误,就好像Database.csproj试图处理它一样。这些错误与Microsoft.NET.Sdk.Web中缺少引用类有关。

完整的项目可以在GitHub 上找到

这实际上是有道理的。您正试图构建具有不同依赖关系的相同源文件。

我的建议是从两者中提取公共/共享项,并将添加到一个名为shared的新项目中(在.Net标准中更可取(,并引用两个项目中的库。

在这里创建一个.net核心指南类库

TL;DL;

  1. 创建解决方案和客户端应用程序
  2. 创建类库项目
  3. 添加类库功能
  4. 设置
  5. 构建
  6. 添加类库引用
  7. 导入命名空间
  8. 调用函数

相关内容

最新更新