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

117
我有一个.NET Core xUnit项目。我试图从中调用一个WCF服务,但是遇到了以下异常:
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包System.ServiceModel.Http.4.3.0的Framework 4.7项目一起使用。

Translated text:

它可以与一个使用相同Nuget包System.ServiceModel.Http.4.3.0的Framework 4.7项目一起使用。


1
@silkfire 我就怕出现这种情况。这个问题有没有被记录在哪里?同一个包在不同的框架中安装,却能获得不同的功能集合,这太疯狂了。 - BanksySan
1
@silkfire 如果那是“答案”,那么请随意写下来。 - BanksySan
请访问Microsoft的.NET API参考网站,了解.NET Core支持哪些内容以及仅限于.NET Framework的内容。 - jspinella
2
@BanksySan 看起来我是正确的,事实上它已经从主框架中删除了;考虑到这一点,当我声称没有解决方案时,这是非常不幸的,因为微软已经将相关程序集作为可选包在NuGet上提供。请随意将我的被接受的答案更改为下面得票最多的答案。 - silkfire
只需在终端窗口中运行此命令(dotnet add package System.ServiceModel.Primitives)。 - Wesam
4个回答

178

现在,Microsoft已经将相应程序集作为NuGet包提供。

System.ServiceModel.Primitives是基本包;如有必要,请将其他包添加到您的项目中。

输入图像描述

更新(2022年4月28日)

WCF已经部分移植到.NET 5+,并提供了名为CoreWCF的官方库:https://devblogs.microsoft.com/dotnet/corewcf-v1-released/


2
"已弃用" -- 你有相关的参考资料吗?根据 GitHub 的评论,似乎服务器端的 WCF 还没有正式被弃用:https://github.com/dotnet/wcf/issues/1200 - Peder Rice
1
在 'System.ServiceModel' 中找不到 'IDispatchMessageInspector' 和 'IServiceBehavior'。有任何解决方法吗?尝试使用以下代码:_channelFactory.Endpoint.EndpointBehaviors.Add(new CustomInspectorBehavior()); - Rajamohan Rajendran
26
对于那些像我一样想知道为什么这个回答有那么多踩的人,现在已经更新了完全不同的信息。 - adam0101
只是一条注释 - 自 .NET Core 2.0 以来,System.ServiceModel 已经不断进行了移植更新,包括新的 .NET 5.0 框架(NETSTANDARD 而非 ASP.NET)。 - Coruscate5
如有需要,请将其他内容添加到您的项目中。- System.ServiceModel.Http. - yob

98

如果你正在使用 .NET Standard 2.0 (这是我测试过的版本),你可以安装兼容的 NuGet 包。

基本的服务模型可在 System.ServiceModel.Primitives 中获取(目前版本为 v4.4.0)。

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


2
谢谢!我通过安装servicemodel.primitives包解决了我的问题。 - Jota.Toledo
4
在使用 netcoreapp2.0 时,添加这些引用时会出现以下错误:System.PlatformNotSupportedException: 不支持配置文件。 - Wouter Vanherck
我的一个库有"System.ServiceModel.Http",直接集成时可以工作。当我将其添加为Nuget包时,我还需要将"System.ServiceModel.Http"添加到我的实际项目中 -_-。 - Heinzlmaen

20

Microsoft WCF Web Service Reference Provider将包装SvcUtil.exe,并将从您的端点生成一个.NET Standard项目。查看项目文件,您将看到适用于您的ServiceModel引用。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="System.ServiceModel.Duplex" Version="4.3.0" />
    <PackageReference Include="System.ServiceModel.Http" Version="4.3.0" />
    <PackageReference Include="System.ServiceModel.NetTcp" Version="4.3.0" />
    <PackageReference Include="System.ServiceModel.Security" Version="4.3.0" />
    <PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
  </ItemGroup>
</Project>

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


好主意!我会尝试的。 - BanksySan
感谢CWagner更新了链接。 - Boggin
1
或者,如果您想要命令行控制,则使用 dotnet svcutil:https://learn.microsoft.com/da-dk/dotnet/core/additional-tools/dotnet-svcutil-guide - Arve Systad

-1

我曾经遇到过同样的问题,我不得不将.asmx Web服务添加到我的类库ASP.NET Core项目中。我尝试通过添加连接服务选项直接添加引用,但是我无法看到任何配置文件,并且还有很多引用错误。以下是我解决问题的步骤:

1-创建库项目名称为Service Mapping 2-创建文件夹Web References,然后在其中创建文件夹ClientHotel 3-从视图中打开Visual Studio中的终端--->终端---命令提示符。 4-通过命令svcutil http://abctest.asmx(.asmx URL)在ClientHotel文件夹中集成asmx服务。

5-它将创建.cs类和output.config文件以及其中的引用。 6-现在我有了引用错误,例如the type or namespace servicecontractattribute does not exist in namespace System.ServiceModel等。

7-安装System.ServiceModel.Primitives包,然后所有引用错误都将消失。

这就是我如何将.asmx服务添加到我的类库项目中的方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接