target .net framework class library with System.ServiceModel dependency from a .net core application

1.5k views Asked by At

It should be possible to reference .net framework class libraries from .net core 2 projects in Visual Studio 2017 but I get a runtime exception when trying reference a class library dependent System.ServiceModel.

Create a “Console App (.NET Core)”. Visual studio set target framework to ".NET Core 2.0".

Create a “Class Library (.Net Framework)”. Visual studio set target framework to ".NET Framework 4.6.1".

Reference “System.ServiceModel 4.0.0.0” from the class library. Fill Class1 in the class library with the following:

public class Class1
{
    public void Test()
    {
        System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress("");
    }
}

Reference the class library from the console app. From the main method of the console app call the test method in Class1:

class Program
{
    static void Main(string[] args)
    {
        new Class1().Test();
    }
}

Build and run. An exception is thrown then the Test() method is tried executed:

System.IO.FileNotFoundException occurred HResult=0x80070002
Message=Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified. Source= StackTrace: at ClassLibrary1.Class1.Test() in c:\users\rth\source\repos\ConsoleApp2\ClassLibrary1\Class1.cs:line 15 at ConsoleApp1.Program.Main(String[] args) in c:\users\rth\source\repos\ConsoleApp2\ConsoleApp1\Program.cs:line 11

System.ServiceModel is not in the /bin/Debug folder of either project.

I have tried to manually copy the System.ServiceModel to the /bin/debug folder of the console app but get the same error.

How do I reference a class library that reference System.ServiceModel without getting runtime exceptions?

1

There are 1 answers

3
Dmitry Pavlov On

To make the code above working without the exception:

  1. Update your Class Library (.Net Framework) to Class Library (.NET Standard 2.0)
  2. Add NuGet package System.ServiceModel.Http (version v4.4.0) to Class Library (.NET Standard 2.0) dependencies.