System.ServiceModel not found in .NET Core project

C#.NetWcf.Net Core

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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/

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBanksySanView Question on Stackoverflow
Solution 1 - C#silkfireView Answer on Stackoverflow
Solution 2 - C#Arghya CView Answer on Stackoverflow
Solution 3 - C#BogginView Answer on Stackoverflow
Solution 4 - C#saqib aminView Answer on Stackoverflow