dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.67k stars 1.06k forks source link

Providing --additional-deps for netstandard2.0 class library with System.Runtime.Caching throws System.PlatformNotSupportedException #9557

Closed woodne closed 4 years ago

woodne commented 6 years ago

I have a situation in which I have a host application, that hosts a variety of microservices, which are created as class libraries. In my config file, I specify my unity configuration which registers the types for each service in the container, then during startup, the application host does all the startup.

This is an application originally developed against .net framework and WebApi 2, but I have ported it to .net core and ASP.NET Core MVC and it uses the System.Runtime.Caching assembly.

When I run the host application as below

dotnet --additional-deps MyClassLibrary.deps.json MyHostApplication.dll

I am getting the following exception:

System.PlatformNotSupportedException: System.Runtime.Caching is not supported on this platform.

The (abbreviated) deps.json for the class library looks like:

      "System.Runtime.Caching/4.5.0": {
        "runtime": {
          "lib/netstandard2.0/System.Runtime.Caching.dll": {
            "assemblyVersion": "4.0.0.0",
            "fileVersion": "4.6.26515.6"
          }
        }
      },

and the netcoreapp looks like:

      "System.Runtime.Caching/4.5.0": {
        "dependencies": {
          "System.Configuration.ConfigurationManager": "4.5.0"
        },
        "runtime": {
          "lib/netstandard2.0/System.Runtime.Caching.dll": {
            "assemblyVersion": "4.0.0.0",
            "fileVersion": "4.6.26515.6"
          }
        },
        "runtimeTargets": {
          "runtimes/unix/lib/netcoreapp2.0/System.Runtime.Caching.dll": {
            "rid": "unix",
            "assetType": "runtime",
            "assemblyVersion": "4.0.0.0",
            "fileVersion": "4.6.26515.6"
          },
          "runtimes/win/lib/netcoreapp2.0/System.Runtime.Caching.dll": {
            "rid": "win",
            "assetType": "runtime",
            "assemblyVersion": "4.0.0.0",
            "fileVersion": "4.6.26515.6"
          }
        }
      },

If I change the class library to target netcoreapp2.1 instead of netstandard2.0, it works fine, and without the --additional-deps flag, it seems like it is using the netcoreapp2.0 runtime version of the System.Runtime.Caching assembly, and it works too.

Steps to reproduce

clone https://github.com/woodne/host-app-caching

Execute ./repro.sh

See that it throws

Expected behavior

Shouldn't throw? I would expect that it uses the .netcore runtime instead of .netstandard

Actual behavior

Throws

Environment data

dotnet --info output:


.NET Core SDK (reflecting any global.json):
 Version:   2.1.300
 Commit:    adab45bf0c

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.12
 OS Platform: Darwin
 RID:         osx.10.12-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.1.300/

Host (useful for support):
  Version: 2.1.0
  Commit:  caa7b7e2ba

.NET Core SDKs installed:
  2.1.4 [/usr/local/share/dotnet/sdk]
  2.1.105 [/usr/local/share/dotnet/sdk]
  2.1.300-preview2-008533 [/usr/local/share/dotnet/sdk]
  2.1.300 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.0-preview2-final [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.0-preview2-final [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.0.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.7 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.0-preview2-26406-04 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download```
livarcocc commented 6 years ago

You are passing the deps.json of the library to the host and it will potentially use that deps.json as well. Which leads to your problem. Libraries in general are not runnable and in particular, netstandard2.0 is not runnable.

This is expected with the current architecture of .NET Core. I would maybe suggest targeting netcoreapp2.1 in your libraries if you want to use their deps.json.