2017-08-07 173 views
3

我有一个.NET Core xUnit项目。我想打电话给从中WCF服务,但出现以下情况例外:.NET Core项目中找不到System.ServiceModel

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. 

它与一个框架4.7的项目有相同的NuGet包System.ServiceModel.Http.4.3.0

+0

System.ServiceModel在.NET Standard和.NET Core中都被弃用。它已被ASP.NET Core取代。也许你必须重建你的WCF服务才能与.NET Core兼容。 – silkfire

+0

@silkfire我担心。有没有记录在任何地方?我很疯狂,可以在不同的框架中安装相同的软件包并获得不同的功能。 – BanksySan

+1

@silkfire如果这是答案,那就随意放下吧。 – BanksySan

回答

0

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

也许你将不得不重新设计你的WCF服务以与.NET Core兼容。

+0

“已弃用” - 您有参考吗?根据GitHub的评论,这听起来像服务器端的WCF还没有正式被弃用:https://github.com/dotnet/wcf/issues/1200 –

4

Microsoft WCF Web Service Reference Provider包装SvcUtil.exe,并将从您的端点生成一个.NET标准项目。查看项目文件,您将看到适用于您的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项目中使用生成的类库。

+0

好思想!我会尝试的。 – BanksySan

2

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

基本服务模式可在System.ServiceModel.Primitives(目前为v4.4.0)。

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

相关问题