无法在 .NET Core 项目中加载 System.ServiceModel.ServiceBehaviorAttribute

Could not load System.ServiceModel.ServiceBehaviorAttribute in .NET Core project

我有一个 .NET Core 测试项目。在此,我需要调用 WebApi 和 WCF 服务(因为我们只迁移了一些项目,我需要使用新旧服务生成测试数据)。我已将 System.ServiceModel.Primitives 包添加到项目中,但我仍然遇到此异常:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.TypeLoadException: Could not load type 'System.ServiceModel.ServiceBehaviorAttribute' from assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

有人知道我可能错过了什么吗?

我发现我的项目中缺少很多引用,其中 none 直接与 WCF 相关。当用异常断点调试时,我一个接一个地添加了缺少的包。在某些情况下,我在网上发现 .NET Core 中只有一个异常,您无法为它做任何事情,只能忽略它。很奇怪,但最后我的测试是 运行.

我仍然不知道为什么会出现问题的异常,但是一旦所有必需的参考都可用,它就会消失。也许在此之前无法正确构建某些内容,因此未加载 WCF 包?

没关系,如果您收到这样的消息,只需在所有异常上启用断点并调试以查看实际情况。