通过.Net Core 3.1中的Web浏览器公开控制台应用程序信息 [英] Expose console app info through web browser in .Net core 3.1
问题描述
我有一个沉重的控制台应用程序,该应用程序执行多个线程,这些线程可以读取,执行计算并写入不同情况下的输出.现在,我试图为所有人创建一种方法,使每个人都可以使用localhost或http/https通过网络浏览器访问数据(实时或接近实时).
I have a heavy console app that executes several threads that read, perform calculations and write to outputs in different scenarios. Now I'm triying to simply create a way for everyone to be able to access to the data (real-time or near-real-time) through a web browser using either localhost or http/https.
我也尝试过遵循此资源作为其他.我实际上已经四处查看并发现了有关自我托管的信息,但是我真的不知道如何以简单的方式实现它.
I've tried following this resource as well as this other. I've actually looked around and found about self hosting, but I don't really know how to implement it in a simple manner.
为简便起见,我添加了一个非常简单的控制台应用程序示例:
for brevity's sake, I add a very simple example of a console app:
using System;
using System.Collections.Concurrent;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
public static int x = 0;
public static int y = 0;
public static int z = 0;
public static ConcurrentDictionary<String, int> ValuesDictionary = new ConcurrentDictionary<string, int>();
static void Main(string[] args)
{
//Function that updates the values in the dictionary
Thread createInputFileThread = new Thread(() => UpdateDictionaryThreadFunction(10000));
createInputFileThread.Start();
//How to start a simple web host here!
}
public static void UpdateDictionaryThreadFunction(int refreshTime)
{
while (true)
{
Thread.Sleep(refreshTime);
if (x < 100000 && y < 100000 && z < 100000)
{
ValuesDictionary.AddOrUpdate("ValueX", x, (key, oldValue) => oldValue + 1);
ValuesDictionary.AddOrUpdate("ValueY", y, (key, oldValue) => oldValue + 2);
ValuesDictionary.AddOrUpdate("ValueZ", z, (key, oldValue) => oldValue + 3);
}
else
{
ValuesDictionary.AddOrUpdate("ValueX", x, (key, oldValue) => 0);
ValuesDictionary.AddOrUpdate("ValueY", y, (key, oldValue) => 0);
ValuesDictionary.AddOrUpdate("ValueZ", z, (key, oldValue) => 0);
}
}
}
}
}
这里的目标是通过 localhost:someport
或 http://somename:someport
之类的方法来访问ValuesDictionary中的数据,并获得诸如以下内容:值将每x秒刷新一次)
The goal here would be to access the data in the ValuesDictionary through something like localhost:someport
or http://somename:someport
and obtain something like: (The values would be refreshed every x seconds)
名称 | 值 |
---|---|
ValueX | 34 |
ValueY | 42 |
ValueZ | 42 |
推荐答案
您使用.net core ..您想要一个在某些端口上提供TCP-IP的控制台应用程序.要将Kestrel链接到您的控制台应用程序,分配请求-接收器事件并在端口上启动它..要重复刷新客户端,您可以使用客户端本地JS和计时器.您的服务器可以为客户端的浏览器生成包含该JS部分的常规HTML.
You use .net core.. you want a console app that serves TCP-IP on some port.. a minimal solution I would say is to link Kestrel to your console app, assign the request-receiver event and launch it on your port.. to repeatedly refresh the client you can use client side local JS with a timer. Your server can generate the usual HTML incorporating that JS part for the client's browser.
在控制台应用程序中使用Kestrel的示例:
Example of using Kestrel in a Console app:
NOTE: this is an old fashioned "quick solution". Also with Kestrel, it is better to structure your app first, as a reporting Service, independent of the platform choices. You could then "plugin" that code in any platform you may need to use in the future (SignalR, Blazor, ASP, Azure etc)
这篇关于通过.Net Core 3.1中的Web浏览器公开控制台应用程序信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!