114

I have a .NET Core xUnit project. I'm trying to call a WCF service from it but get the following 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.

It works with a Framework 4.7 project with the same Nuget Package System.ServiceModel.Http.4.3.0.

BanksySan
  • 27,362
  • 33
  • 117
  • 216
  • 1
    @silkfire I feared that. Has that been documented anywhere? It's crazy that I can install the same package in different frameworks and get a different set of functionality. – BanksySan Aug 07 '17 at 11:32
  • 1
    @silkfire if that is _the answer_ than feel free to put it down. – BanksySan Aug 07 '17 at 11:51
  • See [Microsoft's .NET API Reference](https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel?view=netcore-2.0) website for info on what's supported in .NET Core and what's .NET Framework only – jspinella Aug 07 '17 at 13:10
  • 2
    @BanksySan Apparently it seems I was correct pertaining to the fact that it *has* been removed from the main framework itself; with that in mind, it was deeply unfortunate that I stated that there was no solution to this, when in fact Microsoft had made the relevant assemblies available on NuGet as optional packages instead. Feel free to change my accepted answer to the one with the most upvotes below. – silkfire Jan 16 '19 at 14:16
  • or just run this in terminal window (dotnet add package System.ServiceModel.Primitives) – Wesam Sep 21 '19 at 18:39

4 Answers4

173

Microsoft has made available the relevant assemblies as packages on NuGet now.

System.ServiceModel.Primitives is the base package; add the others if necessary to your project.

enter image description here

Update (April 28th, 2022):

WCF has been partially ported to .NET 5+ with an official library called CoreWCF: https://devblogs.microsoft.com/dotnet/corewcf-v1-released/

silkfire
  • 24,585
  • 15
  • 82
  • 105
  • 2
    "deprecated" -- do you have a reference for that? Per GitHub comments, it sounds like server-side WCF has not officially been deprecated: https://github.com/dotnet/wcf/issues/1200 – Peder Rice Aug 11 '17 at 12:37
  • 1
    type 'IDispatchMessageInspector' and 'IServiceBehavior' not found in the 'System.ServiceModel'. any idea how to resolve _channelFactory.Endpoint.EndpointBehaviors.Add(new CustomInspectorBehavior()); – Rajamohan Rajendran Jan 24 '19 at 06:52
  • 25
    For those wondering, as I did, why this answer has so many downvotes, it has been updated with completely different information. – adam0101 Mar 01 '19 at 17:45
  • Just as a note - System.ServiceModel has had continuous porting updates since .NET Core 2.0, includes the new .NET 5.0 framework (the NETSTANDARD one, not ASP.NET) – Coruscate5 Aug 10 '21 at 16:11
  • "add the others if necessary to your project" - System.ServiceModel.Http. – yob Jul 13 '23 at 16:14
98

If you are using .NET Standard 2.0 (that's what I tested with), you can install compatible NuGet packages.

The basic service model is available in System.ServiceModel.Primitives (currently v4.4.0).

If required, install System.ServiceModel.Http as well.

Arghya C
  • 9,805
  • 2
  • 47
  • 66
  • 2
    Thanks! I solver my issue by installer servicemodel.primitives package – Jota.Toledo Jun 07 '18 at 11:49
  • 4
    When adding those references while using `netcoreapp2.0`. The following error gets thrown: `System.PlatformNotSupportedException: Configuration files are not supported.`. – Wouter Vanherck Jun 25 '18 at 14:44
  • A library of mine has "System.ServiceModel.Http" and works when directly integrated. When I add it as Nuget package, I also need to add "System.ServiceModel.Http" to my actual project -_- . – Heinzlmaen Aug 18 '20 at 09:38
18

The Microsoft WCF Web Service Reference Provider wraps SvcUtil.exe and will generate a .NET Standard project from your endpoint. Look in the project file and you'll see the ServiceModel references that will work for you.

<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>

When I needed to do this I was able to use the generated class library in my .NET Core project.

CWagner
  • 398
  • 2
  • 13
Boggin
  • 3,251
  • 3
  • 33
  • 48
-1

I had same issue, i had to add .asmx web service into my class library asp.net core project, i tried to add reference directly by add connected service option but i wan unable to see any config file and lots of references errors also, below are the steps by which i solve my problem.

1-Create library project name Service Mapping 2-Create folder Web References and then create inside folder ClientHotel 3-Open terminal in visual studio from view ---> terminal --- command prompt. 4-Integrate asmx service by command svcutil http://abctest.asmx (.asmx URL) inside inside ClientHotel folder.

5-it will create .cs class and output.config file with references in it. 6-Now i had errors of references like the type or namespace servicecontractattribute does not exist in namespace System.ServiceModel etc.

7-Install Package System.ServiceModel.Primitives Then all references errors will be gone.

That is how i was able to add .asmx service to my class library project.

saqib amin
  • 41
  • 1
  • 10