Scott Hanselman

Referencing .NET Standard Assemblies from both .NET Core and .NET Framework

August 21, '17 Comments [39] Posted in DotNetCore | NuGet | Open Source
Sponsored By

Lots of .NET Projects sharing a .NET Standard LibraryI like getting great questions in email but I LOVE getting great questions in email with a complete and clear code repro (reproduction) that's in a git somewhere. Then I can just clone, build (many many bonus points for a clean build) and check out the bug.

I got a great .NET Core question and repro here https://github.com/ScarlettCode/Example. I forked it, fixed it, and submitted a PR. Here's the question and issue and today's fix.

The project has a C# library project (an assembly) that is written to the .NET Standard 2.0. You'll recall that the .NET Standard isn't a runtime or a library in itself, but rather an interface. They are saying that this library will work anywhere that the .NET Standard is supported, like Linux, Mac, and Windows.

Here's that main .NET Standard Library called "Example.Data" written in C#.

Then he had:

  • Windows Forms (WinForms) application in VB.NET using .NET "full" Framework 4.6
  • Console Application also using .NET Framework 4.6
  • Console Application using .NET Core 2.0

Each of these apps is referring to the Example.Data library. The Example.Data library then pulls in a database access library in the form of Microsoft.EntityFrameworkCore.InMemory via NuGet.

WinForms app -> Data Access library -> Some other library. A->B->C where B and C are packages from NuGet.

The .NET Core console builds and runs great. However, when the other projects are run you get this error:

Can't load
Could not load file or assembly
'Microsoft.EntityFrameworkCore, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=adb9793829ddae60'
or one of its dependencies. The system cannot find
the file specified.

Pretty low level error, right? First thing is to check the bin folder (the results of the compile) for a project that doesn't run. Looks like there's no Microsoft.EntityFrameworkCore there. Why not? It's assembly "C" downstream of "A" and "B". EntityFramework's assembly is referred to by the Example.Data assembly...but why didn't it get copied in?

The "full" Framework projects are using the older .csproj format and by default, they use package.config to manage dependencies. The newer projects can reference Packages as first-class references. So we need to tell ALL projects in this solution to manage and restore their packages as "PackageReferences."

I can open up the .csproj file for the Framework projects and add this line within the first <PropertyGroup> like this to change the restore style:

 <RestoreProjectStyle>PackageReference</RestoreProjectStyle>

As Oren wisely says:

"Using .NET Standard requires you to use PackageReference to eliminate the pain of “lots of packages” as well as properly handle transitive dependencies. While you may be able to use .NET Standard without PackageReference, I wouldn’t recommend it."

I can also change the default within VS's Package Management options here in this dialog.

 <RestoreProjectStyle>PackageReference</RestoreProjectStyle> Default Package management format

Hope this helps.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Tuesday, 22 August 2017 02:04:01 UTC
Great advice, thank you.
Pablo
Tuesday, 22 August 2017 06:57:08 UTC
To which version/SKU of Visual Studio does this refer to? I'm assuming VS2015 is out.
Tuesday, 22 August 2017 07:04:48 UTC
This seems like something Visual Studio should be able to figure out and automatically set for you, i.e. if any .NET standard references always use package reference format and don't allow it to be changed.

In fact given this issue why not make package reference the default format going forward? Presume there is some kind of backwards compatibility thing, but would there be no way for MS to "upgrade" the project to make it work?
Peter
Tuesday, 22 August 2017 11:25:30 UTC
Timely! I just ran into this this weekend while working on porting an open source project to .NET standard. Very helpful. Thanks!
Dave Erwin
Tuesday, 22 August 2017 12:30:22 UTC
I can't say I'm a big fan of this solution. The support for PackageReference in old style csproj projects seems very flaky last time I tried (with 2017.2). The references would sometimes spontaneously vanish in VS and AssemblyReference tags in nuspecs were sometimes being ignored.

I will try to produce a reproducible example of these at some point.

Hopefully this will all be fixed once this uservoice is completed to give the new csproj sugar to all .net projects.

Talking of A->B->C, I've raised a nuget issue that highlights a risk of runtime exceptions with the new transitive references.
Mark Adamson
Tuesday, 22 August 2017 12:32:50 UTC
Also note that PackageReference is officially not supported for anything other than .net core, .net standard and UWP according to the microsoft documentation:

At present, package references are supported in Visual Studio 2017 only, for .NET Core projects, .NET Standard projects, and UWP projects targeting Windows 10 Build 15063 (Creators Update).
Mark Adamson
Tuesday, 22 August 2017 13:25:19 UTC
I remember the ancient times (2015) when things as simple as assembly references just worked.
Tuesday, 22 August 2017 14:56:02 UTC
I agree with Peter. This should be handled better by VS and I wonder why it's not the default? Mark makes good points too, and I think we all can commiserate with Bryan Slatneer.

I guess it's just growing pains.
Bob
Tuesday, 22 August 2017 17:26:23 UTC
@Mark It may only be "officially" supported for those types, but if you change the nugget settings as described, other projects will use it. I did notice that for new projects created under that nuget setting, there is no RestoreProjectStyle tag in the csproj
Mike
Tuesday, 22 August 2017 17:37:33 UTC
@Mike I guess the thing is that when I have run into flaky bugs with it, which I have, it's not clear that these can be raised as bugs against NuGet. I tried to comment to ask for clarification on the most recent NuGet blog post but my comment never appeared :(.
Mark Adamson
Tuesday, 22 August 2017 18:31:25 UTC
Combination of both is just awesome
Thanks, i love reading your articles.
I read most recent articles
thanks
Tuesday, 22 August 2017 22:19:02 UTC
Great thing to know. However most of the issues I've found with .NET Core and existing libraries is the build server packaging say on Team City with options like this restoring packages not working, or having to include files in source control from wwwroot due to not being restored as part of build.
Justin King
Wednesday, 23 August 2017 00:40:40 UTC
And here I thought I was the last VB developer on earth :)

When is VB coming to asp.net core? A little under a year ago at AngleBrackets in Vegas, I asked Scott Hunter directly if I should learn C# and he told me point blank that VB was coming to .NET core / ASP.NET core. Now that 2.0 is out I'm wondering if I should still be holding my breath...
Wednesday, 23 August 2017 05:27:55 UTC
hey ! tnx for this great post !
Wednesday, 23 August 2017 06:03:04 UTC
Hey!
Great post!!! Nice job done by you, It's very helpful post. Thanks for sharing keeps it up.
Epson Customer Support Number
Wednesday, 23 August 2017 06:24:49 UTC
The functionality to reference .NET Core projects from the full framework projects was the most important thing I was waiting for in RC2, ever since it was announced at one of asp.net community stand-ups. If you consider a scenario of porting parts of a large solution to .NET Core, this is definitely a blocker: you can't really port ~100 projects simultaneously, and you can't do it gradually if the tooling doesn't allow referencing new projects from classic projects (or vice versa).

I must admit, what I'm currently seeing in the RC2 tooling is kind of disappointing =( So... is there a new estimation on when we can expect .NET Framework -> .NET Core project references to start working properly?

Create admin panels in clicks without knowledge of Programming
Wednesday, 23 August 2017 09:50:29 UTC
Oh man I just spent an hour this morning fixing this manually by adding all the NuGet references to the full framework project. Then I check my RSS reader and see your fix. Thanks!

Matt
Wednesday, 23 August 2017 17:26:51 UTC
This article requires a bit of clarification.

1) If you change the Default package management format to PackageReference, do you need to manually add <RestoreProjectStyle>PackageReference</RestoreProjectStyle> to every project?

2) As Mark Adamson has pointed out, the documentation seems to indicate that PackageReference is NOT supported for "legacy" .NET framework projects. Is this still the case, or has this OFFICIALLY changed in one of the recent VS 2017 updates?

Thanks.
Jack Bond
Wednesday, 23 August 2017 19:46:16 UTC
I'll have to double check this with VS 2017.3, because for now, as mentioned by @Mark, it seems very buggy in VS2017.2: I have weird "references" with the nuget icon appearing (and not disappearing even by removing everything related in the csproj; I event wonder if some information is not kept in .vs because it survives cleanup and vs restarts!)...then at runtime, the app complaining it can't load some .NET Standard Lib (the assembly was copied to my bin/debug even though I target .NET 4.6)

If this indeed works it'll indeed be a time-saver.
Wednesday, 23 August 2017 22:52:52 UTC
I'll echo Olivier. Tons of reference wierdness just as he describes. Couldn't get a web api 4.6 sln to consume a netstandard 1.6 nuget after 2 long days of trying. Just doesn't seem ready for prime time. Great idea though. Hope it works eventually.
Michael Andrews
Thursday, 24 August 2017 03:58:21 UTC
Thanks for the post, package referencing is a bit confusing on .NET Core.
Thursday, 24 August 2017 14:50:56 UTC
great post admin thanks for this really happy. online tv app for pc.
great online Tv app for your windows PC
mobdro apk 2017 download
mobdro for smart tv
mobdro apk 2017 download
mobdro apk 2017 download
mobdro apk 2017 download
Saturday, 26 August 2017 10:19:47 UTC
Gracious man I simply spent a hour at the beginning of today settling this physically by adding all the NuGet references to the full system venture. At that point I check my RSS peruser and see your fix. Much obliged! Vikas Kashyap SEO Company in Delhi
Sunday, 27 August 2017 20:47:59 UTC
Hello, It is a very interesting post. I like this post and I am glad to visit this post. Thank you. https://canonsupportnumber.org/remove-stuck-paper-from-printer/
Ajit Chaudhary
Monday, 28 August 2017 13:40:18 UTC
Thank-you for your great advice
Monday, 28 August 2017 13:50:20 UTC
Great post, thanks for this article
Monday, 28 August 2017 13:53:48 UTC
very good post.
Monday, 28 August 2017 13:54:37 UTC
Thanks!
Monday, 28 August 2017 15:24:57 UTC
Hello Scott,

Another one masterpiece of content in your blog! You always made awesome posts.

Thanks for sharing your knowledge with us.
I love your blog.

Cheers,
Rafa
Monday, 28 August 2017 21:11:05 UTC
How would you fix NU1701 warnings? Package was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.
Stuart
Tuesday, 29 August 2017 07:47:11 UTC
Your article is very useful. Thanks for the post
Tuesday, 29 August 2017 08:20:44 UTC
Thank you for the knowledge. I am having a topic related to it next week.
Wednesday, 30 August 2017 11:14:44 UTC
Thursday, 31 August 2017 00:15:21 UTC
Hi, this issue drove me crazy a few weeks ago. I eventually found the solution. This article would have save me a few hours. I hope it does for other people. Thanks for posting!
Thursday, 31 August 2017 17:38:16 UTC
While converting a solution to netstd 2.0 from 1.6 I had almost reached the stage of giving up.

Found this article amd tried it out, specifically the nuget package format option.

But I still have no intellisense from my netstd 2.0 projects just ???.

Plan B. Use the netstd 2.0 project as a base. Create a 4.7 and core lib to shadow the std lib. Add the contents of the std lib to the shadows as links.

At last in my core and fx apps/libs I have intellisense.

Once MS sort out intellisense then its just a question of deleting the shadow projects. So frustrating that their quality control is not good enough.

Andy Smith
Friday, 01 September 2017 11:38:29 UTC
Re my previous concerning the failure of intellisense.

It appears this was down to Resharper not MS. The Resharper upgrade released on 24th August fixes this issue.
Andy Smith
Saturday, 02 September 2017 10:27:59 UTC
There's an announcement about this now and some listed workarounds: https://github.com/dotnet/standard/issues/481
Mark Adamson
Tuesday, 05 September 2017 17:04:17 UTC
So that's what MSFT made out of VS/.Net: We have to edit project files manually to add references in a way they actually work.
Peter
Tuesday, 05 September 2017 22:14:08 UTC
Confused with .NET Standard, Core, Full Framework, ... versions ...


.NET Standard is a specification, not an implementation

https://weblog.west-wind.com/posts/2016/Nov/23/NET-Standard-20-Making-Sense-of-NET-Again
preguntoncojonero
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.