.NET application (WinForms) depending on ASP.NET Core server

Andi
   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.

Andi

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]

編集
0

コメントを追加

0

関連記事

分類Dev

Path to LocalAppData in ASP.Net Core application

分類Dev

Localizable asp.net core + reactjs application

分類Dev

How to submit a file to an ASP.NET Core application

分類Dev

OpenCover for ASP.Net Core application running on IIS Express

分類Dev

ngrok and https tunnel for asp.net core application

分類Dev

How to handle multiple SPA application in ASP.NET Core

分類Dev

Run async code during startup in a ASP.Net Core application

分類Dev

.NET CoreとASP.NET Core

分類Dev

ASP.NET Core hosting - 500 internal server error

分類Dev

ASP.NET CORE 2.1 Server timeout while debugging

分類Dev

ASP.NET Core 3.0 HttpContext.Current.Server.MapPath

分類Dev

ASP.NET Core storing image to SQL Server null

分類Dev

How to deploy asp.net application to docker container on Linux server?

分類Dev

Is it support Application cache in multiple server in asp.net(C#)?

分類Dev

ASP.NET Core ToHtmlString

分類Dev

Cookieless ASP.NET Core

分類Dev

Difference between using the ASP.NET Core Web Application (.NET Core) with net461 set as the only framework and using the (.NET Framework) template

分類Dev

Difference between using the ASP.NET Core Web Application (.NET Core) with net461 set as the only framework and using the (.NET Framework) template

分類Dev

.net core in visual studio works with console app but not with wpf or winforms

分類Dev

Fill other dropdown depending on previous asp.net mvc/jquery

分類Dev

Removing ASP.NET_SessionId from an ASP.NET application

分類Dev

Azure WebApp Asp.NET Core 2 error: An error occurred while starting the application

分類Dev

How to host the basic ASP.NET Core 2 WebAPI application on IIS

分類Dev

HTTP Error 502.5 - Process Failure in ASP.NET Core 2.1 application

分類Dev

ASP.Net Core application service only listening to Port 5000 on Ubuntu

分類Dev

ASP.NET Core Fetch POST FormData()application / x-www-form-urlencoded

分類Dev

How to decrypt .AspNetCore.Identity.Application cookie in ASP.NET Core 3.0?

分類Dev

Download file from ASP.NET Core api from Blazor client application

分類Dev

ASP.Net Core Razor Pages application not binding to property within model

Related 関連記事

  1. 1

    Path to LocalAppData in ASP.Net Core application

  2. 2

    Localizable asp.net core + reactjs application

  3. 3

    How to submit a file to an ASP.NET Core application

  4. 4

    OpenCover for ASP.Net Core application running on IIS Express

  5. 5

    ngrok and https tunnel for asp.net core application

  6. 6

    How to handle multiple SPA application in ASP.NET Core

  7. 7

    Run async code during startup in a ASP.Net Core application

  8. 8

    .NET CoreとASP.NET Core

  9. 9

    ASP.NET Core hosting - 500 internal server error

  10. 10

    ASP.NET CORE 2.1 Server timeout while debugging

  11. 11

    ASP.NET Core 3.0 HttpContext.Current.Server.MapPath

  12. 12

    ASP.NET Core storing image to SQL Server null

  13. 13

    How to deploy asp.net application to docker container on Linux server?

  14. 14

    Is it support Application cache in multiple server in asp.net(C#)?

  15. 15

    ASP.NET Core ToHtmlString

  16. 16

    Cookieless ASP.NET Core

  17. 17

    Difference between using the ASP.NET Core Web Application (.NET Core) with net461 set as the only framework and using the (.NET Framework) template

  18. 18

    Difference between using the ASP.NET Core Web Application (.NET Core) with net461 set as the only framework and using the (.NET Framework) template

  19. 19

    .net core in visual studio works with console app but not with wpf or winforms

  20. 20

    Fill other dropdown depending on previous asp.net mvc/jquery

  21. 21

    Removing ASP.NET_SessionId from an ASP.NET application

  22. 22

    Azure WebApp Asp.NET Core 2 error: An error occurred while starting the application

  23. 23

    How to host the basic ASP.NET Core 2 WebAPI application on IIS

  24. 24

    HTTP Error 502.5 - Process Failure in ASP.NET Core 2.1 application

  25. 25

    ASP.Net Core application service only listening to Port 5000 on Ubuntu

  26. 26

    ASP.NET Core Fetch POST FormData()application / x-www-form-urlencoded

  27. 27

    How to decrypt .AspNetCore.Identity.Application cookie in ASP.NET Core 3.0?

  28. 28

    Download file from ASP.NET Core api from Blazor client application

  29. 29

    ASP.Net Core Razor Pages application not binding to property within model

ホットタグ

アーカイブ