How to reference an existing .NET Framework Project in an ASP.NET Core 1.0 Web App
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."
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.
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:
I've got another DLL that I made with the regular File | New Project | 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.
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:
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.
- You can also download specific NuGet versions at https://dist.nuget.org/index.html
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!