Scott Hanselman

How to reference an existing .NET Framework Project in an ASP.NET Core 1.0 Web App

October 8, '16 Comments [29] Posted in ASP.NET | ASP.NET MVC
Sponsored By

I had a reader send me a question yesterday. She basically wanted to use her existing .NET Framework libraries in an ASP.NET Core application, and it wasn't super clear how to do it.

I have a quick question for you regarding asp.net core. We are rewriting our website using asp.net core, empty from the bottom up. We have 2 libraries written in .net 4.6 . One is our database model and repositories and the other is a project of internal utilities we use a lot. Unfortunately we cannot see how to reference these two projects in our .net core project.

It can be a little confusing. As I mentioned earlier this week, some people don't realize that ASP.NET Core 1.0 (that's the web framework bit) runs on either .NET Core or .NET Framework 4.6 aka "Full Framework."

ASP.NET Core 1.0 runs on ASP.NET 4.6 nicely

When you make a new web project in Visual Studio you see (today) this dialog. Note in the dropdown at the top you can select your minimum .NET Framework version. You can select 4.6.2, if you like, but I'll do 4.5.2 to be a little more compatible. It's up to you.

File New Project

This dialog could use clearer text and hopefully it will soon.

  • There's the regular ASP.NET Web Application at the top. That's ASP.NET 4.6 with MVC and Web API. It runs on the .NET Framework.
  • There's ASP.NET Core 1.0 running on .NET Core. That's cross platform. If you select that one you'll be able to run your app anywhere but you can't reference "Full" .NET Framework assemblies as they are just for Windows.  If you want to run anywhere you need to use .NET Standard APIs that will run anywhere.
  • There's ASP.NET Core 1.0 running on .NET Framework. That's the new ASP.NET Core 1.0 with unified MVC and Web API but running on the .NET Framework you run today on Windows.

As we see in the diagram above, ASP.NET Core 1.0 is the new streamlined ASP.NET  that can run on top of both .NET Framework (Windows) and .NET Core (Mac/Windows/Linux).

I'll chose ASP.NET Core on .NET Framework and I'll see this in my Solution Explorer:

Web App targeting .NET Framework 4.5.2

I've got another DLL that I made with the regular File | New Project | Class Library.

New Class Library

Then I reference it the usual way with Add Reference and it looks like this in the References node in Solution Explorer. Note the icon differences.

Adding ClassLibrary1 to the References Node in Solution Explorer

If we look in the project.json (Be aware that this will change for the better in the future when project.json's functionality is merged with csproj and msbuild) you'll note that the ClassLIbrary1 isn't listed under the top level dependencies node, but as a framework specific dependency like this:

{
"dependencies": {
"Microsoft.StuffAndThings": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
},

"frameworks": {
"net452": {
"dependencies": {
"ClassLibrary1": {
"target": "project"
}
}
}
}
}

Notice also that in this case it's a type="project" dependency in this case as I didn't build a NuGet package and reference that.

Since the .NET Core tooling is in preview there are some gotchas when doing this today.

  • Make sure that all your class libraries are targeting an appropriate version of the .NET Framework.
    • For example, don't have a 4.5.2 Web App targeting a 4.6.2 Class Library. This could bite you in subtle ways if things don't line up in production.
  • dotnet restore at the command line may well get confused and give you an error like:
    • Errors in D:\github\WebApplication2\src\WebApplication2\project.json - Unable to resolve 'ClassLibrary1' for '.NETFramework,Version=v4.5.2'.
    • Use Visual Studio to build or run msbuild at the command line.
  • You can restore packages from the command line with nuget.exe version 3.4.4 or greater if you restore on the .sln file like this:
    • D:\github\WebApplication2>nuget restore WebApplication2.sln
      MSBuild auto-detection: using msbuild version '14.0' from 'C:\Program Files (x86)\MSBuild\14.0\bin'.
    • I recommend you run nuget.exe to see what version you have and run nuget update -self to have it update itself.

These gotchas will be fixed when the tooling is finalized.

Hope this helps!


Sponsor: Big thanks to Telerik for sponsoring the blog this week! 60+ ASP.NET Core controls for every need. The most complete UI toolset for x-platform responsive web and cloud development. Try now 30 days for free!

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
Sunday, 09 October 2016 03:32:21 UTC
Does it differ in performance,
Whether the app be written in full .net framework or .net core ?

Performance is too important for us.

If i choose full .net is it then possible to referenced ef 6?
Amir
Sunday, 09 October 2016 04:59:23 UTC
What if you started as .net core, like the girl who asked the question and want to change to full framework ? Is it enough to update the project.json to. Resemble your example ?
Michael Bruyninckx
Sunday, 09 October 2016 12:59:26 UTC
I still cannot get this to work, even though I am able to add a reference to the "old" class library from the "new" asp.net core web project, both msbuild and visual studio cannot resolve the reference.
wangzq
Sunday, 09 October 2016 15:27:18 UTC
is there a word that has a meaning opposite to "supercalifragilisticexpialidocious"?

if there is, that's the word that i'd use to describe some of the .NET Core 1.0 gotchas.

KEY .NET FRAMEWORK libraries are NOT available YET for .NET CORE!(a)

Alternative libraries are available although many are works in progress.

That does not mean key .NET Framework libraries will not become available in "the future", however, .NET CORE 1.0 was RTM'd June 27th, 2016(b) and is missing, among other things, System.Drawing(c, d, e) and System.Net.Mail(f).

Programmers will want to do a lot of research and reading before committing enterprise applications to .NET Core 1.0.(g, h, i, j, k)

The sample references below are only the tip of the iceberg ... be careful that your legacy enterprise projects do not suffer the same fate as the Titanic while crossing over to .NET Core 1.0.

(a) http://www.hanselman.com/blog/NETCore10IsNowReleased.aspx

(b) https://blogs.msdn.microsoft.com/dotnet/2016/06/27/announcing-net-core-1-0/

(c) https://blogs.msdn.microsoft.com/dotnet/2016/02/10/porting-to-net-core/

(d) https://github.com/JimBobSquarePants/ImageProcessor (James Jackson-South)

(e) http://stackoverflow.com/questions/33344200/manipulating-images-with-net-core

(f) https://www.stevejgordon.co.uk/how-to-send-emails-in-asp-net-core-1-0

(g) https://docs.asp.net/en/latest/migration/rc1-to-rtm.html

(h) https://github.com/aspnet/Home/wiki/Roadmap and https://github.com/aspnet/Home/wiki/Roadmap

(i) https://docs.asp.net/en/latest/index.html

(j) https://xunit.github.io/docs/getting-started-dotnet-core.html Pay attention to the notes!

(k) https://github.com/moq/moq4/issues/168
Monday, 10 October 2016 07:59:30 UTC
I was having the same issue with dotnet restore from my VSTS environment. Its very annoying that you have to do the Nuget restore trick to get things building in VSTS. I'm not seeing any progress in the github issue, is this still on your list to fix before marking the tooling as release candidate?
Monday, 10 October 2016 08:28:36 UTC
What benefits does ASP.NET Core have over the full framework ASP.NET ?

jogchem
Monday, 10 October 2016 09:28:46 UTC
Thanks Scott for this post!

Im having problems with exact opposite thing: created a .net core class lib, and want to reference it from full .net class library. Added a reference, but intellisense doesn't see the namespace or any type from core lib, and can't compile it. Any ideas?

What's interesting, somehow worked once, then stopped working. But when it worked, it was complaining about Func<,>, that i need System.Runtime 4.0.2, which I added through nuget. But after it stopped working, couldn't reproduce it again.

thanks!
Monday, 10 October 2016 10:32:43 UTC
It's simply a horrible time for asp.net, .net core etc... I guess it will be easier once the tooling is updated. This has been the strength of Microsoft over the past 3 to 4 decades and why I've used it so much, but there's a push inside MS to work on an antiquated command line environment, throwing the opensource stuff in the mix. I get the feeling that there are too many rails fanboys in MS. What MS needs to do is listen to the VB people, they have always had this sense of developer productivity, but they have been sidelined because VB is probably not cool enough. We know this because core doesn't even support VB. I say MS needs to stop copying others to look cool and start doing what they do best and that's supporting the developer in his/her productivity goals.
Kevin Long
Monday, 10 October 2016 13:37:42 UTC
If we look in the project.json (Be aware that this will change for the better in the future when project.json's functionality is merged with csproj and msbuild)...


So does that mean that project.json will become more like the old-style projects with all the stuff hidden away and you do everything via property dialogs?

If so, that makes me sad. I was looking forward to being able to change the project definition and build script by editing the file (with decent intellisense) and having that be the change
John Ludlow
Monday, 10 October 2016 15:16:43 UTC
Gerry, I unfortunately have to agree. Although .NET Core (the set of assemblies) RTMed months ago, the ecosystem did not. For serious projects that would've otherwise relied on several NuGet packages, a medium to large team of developers, working continuous integration, etc., .NET Core is simply not ready for prime-time.

Microsoft declaring that something has RTMed has nearly always meant the tooling had also RTMed at the same time; however, that was not the case with .NET Core. The Visual Studio and command line tooling is far from robust, and downright buggy in many instances. Additionally, .NET Core developers--especially developers of nuget.org packages--are forced to learn and interact with project.json, a file that Microsoft has already said is deprecated in favor of MSBuild. And yet, what exactly will be replacing project.json is still an unknown.

It will be many, many months before .NET Core development is as easy and seamless as .NET Framework development is today. I look forward to deploying applications in Linux someday; that day is just not today.
Nathan Alden, Sr.
Tuesday, 11 October 2016 09:20:49 UTC
Has anyone tried to use their Class Library with EF6 and Stored Procedures, this would be so useful to get working. I Keep getting,

"No connection string named 'MyEntities' could be found in the application config file."

Sander Hammelburg
Tuesday, 11 October 2016 09:22:58 UTC
How about a blog post for references going the other way - .NET Framework class library --> .NET Core project.
Dave Slinn
Tuesday, 11 October 2016 12:46:25 UTC
If I understand correctly ASP.NET Core is next main version of ASP.NET and it can use main version of .NET Framework and "forked" version: .NET Core.
Why did Microsoft change its name to "Core" that suggest that it is "forked" too?
Grzegorz Danowski
Tuesday, 11 October 2016 14:41:25 UTC
Just wrote the blog post how to do the opposite thing, use Core lib from 4.6 project in the same solution: http://hudosvibe.net/post/using-core-class-library-from-.net-4.6-project. Hope it helps, but I'm wandering if there's a better solution ...
Tuesday, 11 October 2016 15:00:03 UTC
I followed your instructions (new project, new class library) and can't get it work.
When i tried to add my reference, I always get the error message which tell me to make a nuget package, etc ...
Did i forgot something ?
Mike
Tuesday, 11 October 2016 21:11:15 UTC
I'm hoping that somebody here can answer my question I posted on StackOverflow:

http://stackoverflow.com/questions/39278781/referencing-traditional-net-framework-from-net-core

When running .NET Core on the Traditional .NET Framework, I want to allow only a certain assembly access 4.x methods. I don't want to 'accidently' use 4.x methods throughout my project, I could designate only certain assemblies as ones that can access 4.x features.

I know there's something I'm missing in configuring the project.json, but I'm having a hard time figuring it out. Thanks!
Robert
Wednesday, 12 October 2016 09:57:51 UTC
Thank you very much. What I am searching for.
Wednesday, 12 October 2016 12:07:55 UTC
Hi

Just in the process of working on a new Web Api. For various reasons we have to use an older legacy object model, build for x86, targeting .Net 4.5.1.

The new WebApi is based on Asp.Net Core - and using the approach above works perfectly (after some initial struggling).

Only problem I had left was the toolchain, and being able to do a "dotnet restore" - but it sounds like that is soon fixed.

Keep up the good work.
Anders
Wednesday, 12 October 2016 12:14:00 UTC
thanks ..

http://backlinkrooz.ir
Wednesday, 12 October 2016 23:32:32 UTC
Sander Hammelburg, we have an N-tier web project, with our DAL as a .net4.6 core class library - we used the tools as https://github.com/mrahhal/Migrator.EF6 to help us get EntityFramework 6 working.
Adam Marshall (sagoo33)
Thursday, 13 October 2016 10:45:44 UTC
Hello Scott.
Probably a bit off topic but that's the best place I was able to find so far.

I'm trying to embed some json documents into a core assembly. The problem is that those documents are in a different git repository and I have them as submodule and it is outside the project folder:

repo
-submodules
--shared-documents
---unit-tests
----{some subfolders}
-tests
--unit-tests
---project.json

In project.json I have the following:
"buildOptions": {
"embed": [ "../../submodules/shared-documents/unit-tests/**" ]
}


Compiler doesn't like this and complains as follows:
CS1508: Resource identifier 'unit-tests.invalid.json' has already been used in this assembly.

Do you think it's possible to make this kind of trick with core project?

Thanks,
Igor
Igor Fedurkin
Friday, 14 October 2016 12:28:34 UTC
Thanks a lot sir, Really enjoyed your blog.
ajax control toolkit
Vibhor
Saturday, 15 October 2016 19:12:52 UTC
Thank you for posting this Very interesting article.It was very beneficial for me.Keep sharing such ideas in the future as well.

192.168.1.1
ajay
Monday, 17 October 2016 14:01:02 UTC
Hi Scott. I have a similar issue and I am targeting .NET Framework 4.5.1.
After reading your blog article on this subject I am a little unclear what TFS Online Build Process I need to change; my build works fine in VS2015 Update 3.

Please could you advise?

##[section]Starting: Run dotnet.exe
##[command]dotnet.exe restore
Welcome to .NET Core!
---------------------
Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available commands or go to https://aka.ms/dotnet-cli-docs.
Telemetry
--------------
The .NET Core tools collect usage data in order to improve your experience. The data is anonymous and does not include commandline arguments. The data is collected by Microsoft and shared with the community.
You can opt out of telemetry by setting a DOTNET_CLI_TELEMETRY_OPTOUT environment variable to 1 using your favorite shell.
You can read more about .NET Core tools telemetry @ https://aka.ms/dotnet-cli-telemetry.
Configuring...
-------------------
A command is running to initially populate your local package cache, to improve restore speed and enable offline access. This command will take up to a minute to complete and will only happen once.
Decompressing 100% 4726 ms
2016-10-17T13:23:05.7606879Z log : Restoring packages for C:\a\1\s\Mainline\src\MyNamespace.Web\project.json...
Expanding 100% 19475 ms
error: Unable to resolve 'MyNamespace.Common' for '.NETFramework,Version=v4.5.1'.
log : Restoring packages for tool 'BundlerMinifier.Core' in C:\a\1\s\Mainline\src\MyNamespace.Web\project.json...
log : Restoring packages for tool 'Microsoft.AspNetCore.Razor.Tools' in C:\a\1\s\Mainline\src\MyNamespace.Web\project.json...
log : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in C:\a\1\s\Mainline\src\MyNamespace.Web\project.json...
log : Writing lock file to disk. Path: C:\a\1\s\Mainline\src\MyNamespace.Web\project.lock.json
log : C:\a\1\s\Mainline\src\MyNamespace.Web\project.json
log : Restore failed in 4208ms.
Errors in C:\a\1\s\Mainline\src\MyNamespace.Web\project.json
Unable to resolve 'MyNamespace.Common' for '.NETFramework,Version=v4.5.1'.
##[error]Process completed with exit code 1.
##[section]Finishing: Run dotnet
Monday, 17 October 2016 20:07:18 UTC
While works for primitive, empty projects, this is not currently working in real life due to "read-only on copy bug" for 3rd party DLLs referenced in Full .Net Framework project.

https://github.com/dotnet/cli/pull/3794 - "error : failed to make project runnable".
sb
Friday, 21 October 2016 09:30:40 UTC
How do you feel about the move away from project.json to an MSBuild XML file?
Douglas Waugh
Saturday, 22 October 2016 15:31:05 UTC
Hi Scott,

I'm still really confused by what is compatible with what. I always see the graphic, the first one in your post, for .NET Core 1.0 with .NET 4.6, but then you reference .NET 4.5.2 to be a "little bit more compatible"? Why does the graphic have .NET 4.6 if .NET 4.5.2 is compatible with .NET Core 1.0? What am I missing?

I'm working to create a roadmap for our enterprise, but this makes it difficult to understand what we should be doing now to set ourselves up for a future move to .NET Core when other key pieces of it, libraries and tooling, become available.

If you could clear up this ambiguity for me, I would greatly appreciate it!
Jeff Block
Wednesday, 26 October 2016 03:43:32 UTC
This is kind of a cop-out answer. You're basically saying "How do you reference a 4.6 Library from a .NET Core project? Make sure it doesn't target the core framework!".

If you want people to actually be USING the new framework then there needs to be support in the Visual Studio tooling to allow developers to easily reference projects of any type. So I should be able to have the following combos:

  • .NET Core Framework Project should be able to reference libraries that are built against .NET Standard or Full .NET
  • .NET Standard Project should be able to reference libraries that are built against Full .NET or .NET Core Framework
  • Full .NET Framework Project should be able to reference libraries that are built against .NET Standard or .NET Core

    The fact that this situation (the state of the tooling) is SOOOO bad is shameful. By no means is .NET Core Framework really RTM. At best it's a glorified beta.
  • TheLievense
    Thursday, 03 November 2016 11:30:29 UTC
    This means I can only add .net framework 4.5.2 projects, but not .net core projects. :(
    Elaine
    Comments are closed.

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