在.NET Core项目中找不到System.ServiceModel

我有一个.NET Core xUnit项目。 我试图从它调用WCF服务,但得到以下exception:

System.InvalidOperationException occurred HResult=0x80131509 Message=An error occurred while loading attribute 'ServiceContractAttribute' on type 'IMyContract'. Please see InnerException for more details. Inner Exception 1: FileNotFoundException: Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified. 

它适用于具有相同Nuget Package System.ServiceModel.Http.4.3.0的Framework 4.7项目。

不幸的是,.NET Standard和.NET Core都不推荐使用System.ServiceModel 。 它已被ASP.NET Core取代。

也许你必须重新设计你的WCF服务才能与.NET Core兼容。

如果您使用的是.NET Standard 2.0 (这是我测试过的),您可以安装兼容的NuGet软件包。

System.ServiceModel.Primitives (目前为v4.4.0)中提供了基本服务模型。

如果需要,还要安装System.ServiceModel.Http

Microsoft WCF Web服务引用提供程序包装SvcUtil.exe,并将从您的端点生成.NET Standard项目。 查看项目文件,您将看到适合您的ServiceModel引用。

   netstandard1.4          

当我需要这样做时,我能够在我的.NET Core项目中使用生成的类库。