4

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?

Rolf
  • 1,219
  • 2
  • 13
  • 23
  • System.ServiceModel is in the GAC, and the Framework directory, at least on my machine. You may have referenced an assembly with a specific version requirement that you are not meeting. Check your GAC, and your framework directories. Then check to make sure your reference isn't targeted as a specific version. – Kevin Hirst Sep 11 '17 at 20:02
  • @Kevin-hirst: When I look at properties for the reference to System.ServiceModel then path is C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.ServiceModel.dll. If I take this file and place it under /bin/debug in the console app then it still doesn’t work and the assembly loader are supposed to look here. One thing to notice: there is no problem if I create a .net framework console app (not .net core) and reference the same class library. – Rolf Sep 11 '17 at 20:54
  • If I change the target framework of the console app to .NET Framework 4.6.1 the it works. – Rolf Sep 12 '17 at 08:22

1 Answers1

0

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.
Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
  • 1
    Yes .NET Standard 2.0 libraries can be referenced just fine. I however need to reference Sys-tem.ServiceModel version 4.0.0.0 specifically because version 4.4.0 of System.ServiceModel.* does not contain the functionality I need: NetTcpSecurity with SecurityMode.Message and MessageCredentialType.Windows. Sorry for not being crystal clear on this. I have added the specific version number to the question. – Rolf Sep 14 '17 at 06:47
  • @Rolf you might want to check if this this workaround with `SimpleSOAPClient` does the trick you need https://stackoverflow.com/a/45988249/804385 – Dmitry Pavlov Sep 14 '17 at 10:58
  • The basic service model is available in `System.ServiceModel.Primitives` NuGet package, which is compatible with .NET Standard 2.0 – Arghya C Jan 05 '18 at 11:23