我为什么会收到错误信息“类型'IReturn<>'在未引用的程序集中定义“组件”。在VIsualStudio 2017中使用ServiceStack [英] Why am I getting error "The type 'IReturn<>' is defined in an assembly that is not referenced" using ServiceStack in VIsualStudio 2017

查看:137
本文介绍了我为什么会收到错误信息“类型'IReturn<>'在未引用的程序集中定义“组件”。在VIsualStudio 2017中使用ServiceStack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Visual Studio 2017 15.6.3。

I am running Visual Studio 2017 15.6.3.

我有一个.NET Standard 2.0 DLL项目,其中包含与ServiceStack一起使用的Request和Response类。 Request类实现IReturn<>。

I have a .NET Standard 2.0 DLL project which contains Request and Response classes for use with ServiceStack. The Request classes implement IReturn<>.

我有一个.NET Core 2.0控制台EXE项目,该项目引用了.NET Standard DLL。该EXE使用ServiceStack JsonServiceClient将请求发送到ServiceStack服务。

I have a .NET Core 2.0 console EXE project which references the .NET Standard DLL. This EXE uses a ServiceStack JsonServiceClient to send requests to a ServiceStack service. It compiles and works fine.

我添加了一个.NET Framework 4.6.1控制台EXE项目,该项目也引用了.NET Standard 2.0 DLL。它必须是Framework应用程序,因为它引用了与Core或Standard不兼容的其他DLL。该EXE使用ServiceStack JsonServiceClient将请求发送到与.NET Core EXE完全相同的ServiceStack服务,但是该程序无法编译。 Framework 4.6.1应该支持.NET Standard 2.0 DLL,但是由于某种原因,它与IReturn<>接口存在冲突。

I added a .NET Framework 4.6.1 console EXE project which also references the .NET Standard 2.0 DLL. It has to be a Framework app because it references other DLLs which are not compatible with Core or Standard. This EXE uses a ServiceStack JsonServiceClient to send requests to a ServiceStack service exactly like the .NET Core EXE, but this program will not compile. Framework 4.6.1 is supposed to support .NET Standard 2.0 DLLs, but for some reason it has a conflict with the IReturn<> interface.

  var extentRequest = new ExtentRequest { ... };
  using (var client = new JsonServiceClient(baseUrl))
  {
    return client.Post(extentRequest);
  }

返回的错误是:类型'IReturn<>'在您必须添加对程序集'ServiceStack.Interfaces,Version = 5.0.0.0,Culture = neutral,PublicKeyToken = null'的引用。

The error returned is: "The type 'IReturn<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'ServiceStack.Interfaces, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null'."

我想不出它为什么不起作用的任何原因:

I can't think of any reason why it isn't working:


  1. 当我查看该项目的引用列表时,请参阅ServiceStack.Interfaces及其属性,说它是5.0.0.0版。

  2. 我可以在Framework可执行项目代码中使用窥视定义,并按照继承链的方式来查找IReturn<>,以便它知道类型。

  3. 所有三个项目都使用通过NuGet获得的ServiceStack版本5.0.2。

  4. 所有项目都针对x64配置和编译。

  1. When I look at the list of References for the project, I see ServiceStack.Interfaces and its Properties say it is version 5.0.0.0.
  2. I can use "Peek Definition" within the Framework executable project code and work my way through the chain of inheritance to find IReturn<> so it knows the type.
  3. All three projects use ServiceStack version 5.0.2 acquired with NuGet.
  4. All projects are configured and compiled for x64.

我认为问题是Framework版本和Standard版本之间的某种不匹配。谁能告诉我为什么会出现此错误?

I assume the problem is some sort of mismatch between Framework and Standard versions. Can anyone tell me why I'm getting this error?

推荐答案

您不能共享 .NET Framework。 dll .NET Core .NET Standard 项目中具有.NET Framework依赖性,反之亦然。

You can't share a .NET Framework .dll with .NET Framework dependencies in .NET Core or .NET Standard projects and vice-versa.

大多数ServiceStack NuGet软件包都包含.NET v4.5和.NET Standard 2.0版本:

Most ServiceStack NuGet packages contain both .NET v4.5 and .NET Standard 2.0 builds:


  • net45 -包含对运行 ASP.NET Web或自托管 HttpListener 应用程序主机
  • 的支持
  • netstandard2.0 -包含仅在 ASP.NET Core 应用程序主机上运行的支持

  • net45 - Contains support for running ASP.NET Web or Self-Hosting HttpListener App Hosts
  • netstandard2.0 - Contains support for only running on ASP.NET Core App Hosts

仅.NET v4.5构建版本支持在经典ASP.NET 或SelfHost HttpListener主机(即 AspNetRequest / AspNetResponse )。您不能在经典ASP.NET Web项目中的ServiceStack上使用.NET Standard构建,因为该构建实际上并不包含所需的依赖关系和功能-在.NET Standard 2.0中不可用(仅涵盖正在运行 ASP.NET Core 应用)。

Only the .NET v4.5 builds contains support for hosting on classic ASP.NET or SelfHost HttpListener Hosts (i.e. AspNetRequest/AspNetResponse). You cannot use .NET Standard builds on ServiceStack in classic ASP.NET Web projects as the builds do not physically contain the dependencies and functionality needed - which isn't available in .NET Standard 2.0 (which only covers running ASP.NET Core Apps).

有两种解决方案可以在.NET Framework v4.6.1 +中共享同一项目和.NET Standard / .NET Core项目:

There are 2 solutions for being able to share the same project in .NET Framework v4.6.1+ and .NET Standard / .NET Core projects:

能够与.NET Framework项目共享相同的 .dll ,您需要仅引用.NET Standard .Core 软件包,以便在安装NuGet软件包时,您的 .NET v4.6.1 + 项目引用与您的.NET Standard或.NET Core项目所使用的.NET Standard 2.0 dll。

To be able to share the same .dll with .NET Framework projects you need to either reference the .NET Standard only .Core packages so that when NuGet packages are installed your .NET v4.6.1+ project is referencing the same .NET Standard 2.0 dlls as your .NET Standard or .NET Core projects are using.

另一种方法是维护同时创建.NET Framework和.NET Standard版本的多目标项目。这是 Hello Mobile共享网关项目用来同时支持这两种方法。 NET Framework和Mobile .NET Standard客户端以及 ServiceStack Server.Common项目使用与以下相同的ServiceStack Server实现:

The alternative is to maintain multi-targeted projects which creates both .NET Framework and .NET Standard builds. This is the approach that the Hello Mobile Shared Gateway project uses to support both .NET Framework and Mobile .NET Standard clients and the ServiceStack Server.Common project uses for the same ServiceStack Server implementation to be used in:

  • Server.NetCore - hosting the ServiceStack Services in a ASP.NET Core 2.0 App
  • Server.NetCoreFx - hosting in a ASP.NET Core App on the .NET Framework
  • Server.AspNet - hosting classic ASP.NET Framework Web Applications
  • Server.HttpListener - host in a .NET Framework Self-Hosting HttpListener AppHost

这篇关于我为什么会收到错误信息“类型'IReturn&lt;&gt;'在未引用的程序集中定义“组件”。在VIsualStudio 2017中使用ServiceStack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆