Control Panel -----------> Server
.NET Framework depends on ASP.NET Core
⚡ FAIL ⚡
I have a web server built with ASP.NET Core 2.1, because it should run both on a normal desktop computer (Windows 10) and a Raspberry Pi (Windows 10 IoT Core). Works fine so far.
Just in the case the server is running on a desktop computer, I want to provide a control panel for the server based on WinForms (.NET Framework 4.7). Therefore (and for other additional functionality needed for the desktop version) I created a second project using .NET Framework 4.7, which includes the server project as a dependency.
Unfortunately, this does not work. Visual Studio tells me, that the target version "netcoreapp2.1" of the server is not compatible with the version ".NETFramework,Version=v4.7" of the control panel. Also referencing the server DLL instead of the project does not work (compiles fine, but runtime error when calling the server).
Is there an elegant solution (beside creating two separate exe files and using some form of interprocess communication)?
EDIT: I know I can move shared code into a .NET Standard class library project. But my problem is the ASP.NET Core specific code, like say calling Server.Start()
from the control panel, where Server
is specific to ASP.NET Core.
TL;DR: Use a ".NET Standard" project for the ASP.NET Core server and install the ASP.NET Core dependencies also on the WinForms project.
Long answer:
The problem is, that Visual Studio creates the ASP.NET Core project based on ".NET Core", but not on ".NET Standard". So we can not depend on it. But we can change this. This is the whole project setup:
1) As the control panel project, create a solution with a WinForms (".NET Framework") project. Let's call the project WinForms
.
2) Add a ASP.NET Core web application project to the solution. Let's call it Server
. As a template, select the "API" template and choose the ".NET Core" version "ASP.Net Core 2.1".
3) In the WinForms
project, add a dependency on the Server
project. Add a line for launching the server by calling Server.Program.Main(new string[0]);
4) In the Server
project, in the Main
method of Program.cs
, I launch the web host by calling RunAsync()
instead of Run()
, so that the call does not block and my Winforms project will be able to proceed.
When you try to compile WinForms
project now, you see exactly the error which I described in my question: The ".NET Framework" project does not accept a ".NET Core" project as a dependency. Luckily, the Server
project actually complies to the ".NET Standard", so we can change this now:
4) Close Visual Studio. Open Server.csproj
in a text editor and replace the line <TargetFramework>netcoreapp2.1</TargetFramework>
by <TargetFramework>netstandard2.0</TargetFramework>
.
5) Open the solution in Visual Studio again. Now we see problems with the NuGet packages in the Server
project. Open the NuGet package manager and uninstall "Microsoft.AspNetCore.App". Instead, install both "Microsoft.AspNetCore" and "Microsoft.AspNetCore.Mvc". If you have a look at their dependencies, you will see that they both conform to the ".NET Standard".
6) Now the solution compiles fine. But when running the WinForms
project, I get a runtime exception when the server is called. Some assembly files are missing. I guess, this is because we need the ".NET Framework" variants of those assembly files.
7) Therefore, in the WinForms
project also open the NuGet package manager and install both dependencies "Microsoft.AspNetCore" and "Microsoft.AspNetCore.Mvc" there, too! You will see that the ".NET Framework" variants are downloaded.
8) Run the WinForms
project, and now, it works :-D
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加