2017-08-07 170 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

相關問題