How do I reference a UWP+NET46 portable library from a .NET 4.6 console application?
Asked Answered
M

1

6

I have a portable class library project that targets .NET 4.6 and Universal Windows Platform. This class library contains just one class with the following line of code in its constructor:

Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));

Now I create a new .NET 4.6 console application project in the same solution and add a project reference to the portable class library. Calling the method that houses the above line of code results in the following exception at runtime:

Could not load file or assembly 'System.IO.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

What am I doing wrong here? There are no compile-time errors or warnings.

Things I have tried: add missing(?) NuGet package manually

It seems that System.IO.FileSystem is a library delivered via NuGet, as part of the Microsoft.NETCore mega-package. Okay, perhaps I need to explicitly add this package to any project that uses my portable class library. I attempt to do so.

Could not install package 'Microsoft.NETCore.Platforms 1.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

No luck with this approach.

Things I have tried: create a project.json file

While there is no clear info on the web, I read a few tidbits about a new project.json based NuGet harness or build system. Just to experiment, I created the following project.json file in my console application project:

{
  "dependencies": {
   },
  "frameworks": {
    "net46": { }
  },
  "runtimes": {
    "win-anycpu": { }
  }
}

It works! The runtime error goes away! However, I soon found that this was either not the right solution or not a complete solution. I started writing some code to read configuration section values, which involved making use of the IConfigurationSectionHandler interface, and got the following compile-time error:

error CS0246: The type or namespace name 'IConfigurationSectionHandler' could not be found (are you missing a using directive or an assembly reference?)

This interface is part of the System assembly. I see a reference to this assembly, but it has a yellow exclamation mark icon, and a warning appears in the warnings window:

The referenced component 'System' could not be found.

This is where I ran out of ideas. Am I missing something totally obvious?

Mizzenmast answered 18/8, 2015 at 6:30 Comment(2)
Well, not hard to repro. Targeting dnx is pretty courageous right now, it is beta quality. You could limp along by copying System.IO.FileSystem.dll and System.IO.FileSystem.Primitives.dll from your dnx runtime directory but this is well less than ideal. Just file a bug so they can fix it.Spoondrift
@HansPassant DNX is not required for this to reproduce, so it does not appear DNX related. Everything is the same even if I take out targeting DNX. I edited my question to remove this potential point of confusion.Mizzenmast
M
1

I have found the solution. My initial attempt was to install the Microsoft.NETCore package into the console application, resulting in the error shown in my original post.

However, if I install only the narrowly-scoped packages, e.g. System.IO.FileSystem, then I achieve success and the application works correctly. Apparently there is something special about the Microsoft.NETCore "master package" that prevents it from correctly installing into dependent projects.

Mizzenmast answered 21/8, 2015 at 6:25 Comment(2)
Not sure why this was previously downvoted. Worked like a charm for me.Exaggeration
I'm having a similar issue where I can't reference a Win 8.1 library project from a .NET 4.6 application--could this answer be relevant here?Julius

© 2022 - 2024 — McMap. All rights reserved.