将 ServiceStack 与完整的 .NET Framework 一起使用

Using ServiceStack with Full .NET Framework

当以完整的 .NET Framework 为目标时,我遇到了一些奇怪的 ServiceStack 依赖错误,csproj 文件:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <AssemblyName>Tsl.Example</AssemblyName>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.0.0" />
    <PackageReference Include="ServiceStack" Version="5.2.1" />
    <PackageReference Include="ServiceStack.Admin" Version="5.2.1" />
    <PackageReference Include="ServiceStack.Admin.Core" Version="5.2.1" />
    <PackageReference Include="ServiceStack.Api.Swagger" Version="5.2.1" />
    <PackageReference Include="ServiceStack.Api.Swagger.Core" Version="1.0.44" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Tsl.Example.Data\Tsl.Example.Data.csproj" />
    <ProjectReference Include="..\Tsl.Example.Model\Tsl.Example.Model.csproj" />
    <ProjectReference Include="..\Tsl.Example.Service\Tsl.Example.Service.csproj" />
  </ItemGroup>
</Project>

错误如下:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0433  The type 'AppHostBase' exists in both 'ServiceStack, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null' and 'ServiceStack, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587'   Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\AppHost.cs   19  Active
Error   CS0115  'AppHost.Configure(Container)': no suitable method found to override    Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\AppHost.cs   39  Active
Error   CS0433  The type 'Container' exists in both 'ServiceStack, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null' and 'ServiceStack, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587' Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\AppHost.cs   39  Active
Error   CS1729  'AppHostBase' does not contain a constructor that takes 2 arguments Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\AppHost.cs   29  Active
Error   CS0103  The name 'Plugins' does not exist in the current context    Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\AppHost.cs   45  Active
Error   CS0103  The name 'Plugins' does not exist in the current context    Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\AppHost.cs   47  Active
Error   CS0433  The type 'PostmanFeature' exists in both 'ServiceStack, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null' and 'ServiceStack, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587'    Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\AppHost.cs   47  Active
Error   CS0103  The name 'Plugins' does not exist in the current context    Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\AppHost.cs   48  Active
Error   CS0433  The type 'AdminFeature' exists in both 'ServiceStack.Admin, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null' and 'ServiceStack.Admin, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587'  Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\AppHost.cs   48  Active
Error   CS0103  The name 'Plugins' does not exist in the current context    Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\AppHost.cs   49  Active
Error   CS0433  The type 'ValidationFeature' exists in both 'ServiceStack, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null' and 'ServiceStack, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587' Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\AppHost.cs   49  Active
Error   CS0433  The type 'Licensing' exists in both 'ServiceStack.Text, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null' and 'ServiceStack.Text, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587'   Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\Program.cs   13  Active
Error   CS1503  Argument 2: cannot convert from 'Tsl.Example.AppHost' to 'ServiceStack.AppHostBase' Tsl.Example C:\projects\sandbox\api.example.tso.auth.atalasoft\src\Tsl.Example\Startup.cs   51  Active

我可以定位完整的 .NET Framework 和参考 ServiceStack 库吗?

如果你想 运行 classic ASP.NET 或 HttpListener 你应该只引用主要的 "ServiceStack.Admin" 包(即没有 .Core后缀),其中包含 .NET Standard 2.0 和 .NET Framework v4.5 版本。在 .NET Framework 应用程序中,它将使用 .NET v4.5 框架构建。

如果您想在 .NET Framework 上创建新的 ASP.NET Core 应用程序,您应该 reference .Core packages instead 它只包含 .NET标准 2.0 构建强制 .NET Framework 应用程序使用 运行ning 在 ASP.NET 核心应用程序中所需的 .NET 标准构建。

我强烈建议从 ASP.NET Core .NET Framework Templates 之一开始创建一个具有正确依赖关系的工作项目:

$ npm install -g @servicestack/cli

$ dotnet-new web-corefx AcmeNetFx

你应该永远不要引用两者,你也只能一起使用所有包的相同版本号,即你不能使用已弃用的ServiceStack.Api.Swagger.Core包v5.2.1 程序集。

ServiceStack.Api.Swagger 包含旧的 v1.2 version of Swagger, you likely want to reference ServiceStack.Api.OpenApi instead which implements the newer Swagger 2.0/Open API specification.