在 .Net core 3.1 中通过 Web 浏览器公开控制台应用程序信息
Expose console app info through web browser in .Net core 3.1
我有一个重型控制台应用程序,它执行多个线程,这些线程在不同的场景中读取、执行计算和写入输出。现在我正在尝试简单地创建一种方法,让每个人都能够使用 localhost 或 http/https.
通过 Web 浏览器访问数据(实时或近实时)
我试过关注 this resource as well as this other。我实际上环顾四周发现了自托管,但我真的不知道如何以简单的方式实现它。
为了简洁起见,我添加了一个非常简单的控制台应用程序示例:
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 秒刷新一次)
Name
Value
ValueX
34
ValueY
42
ValueZ
42
检查 SignalR 是否满足此类要求
你使用 .net 核心..你想要一个在某个端口上提供 TCP-IP 服务的控制台应用程序..我想说的一个最小的解决方案是 link Kestrel 到你的控制台应用程序,分配请求-接收器事件并在您的端口上启动它。要重复刷新客户端,您可以使用客户端本地 JS 和计时器。您的服务器可以为客户端的浏览器生成通常的 HTML 合并该 JS 部分。
在控制台应用程序中使用 Kestrel 的示例:
注意:这是老式的“快速解决方案”。此外,对于 Kestrel,最好首先构建您的应用程序,作为报告服务,独立于平台选择。然后,您可以将该代码“插入”到您将来可能需要使用的任何平台(SignalR、Blazor、ASP、Azure 等)
我有一个重型控制台应用程序,它执行多个线程,这些线程在不同的场景中读取、执行计算和写入输出。现在我正在尝试简单地创建一种方法,让每个人都能够使用 localhost 或 http/https.
通过 Web 浏览器访问数据(实时或近实时)我试过关注 this resource as well as this other。我实际上环顾四周发现了自托管,但我真的不知道如何以简单的方式实现它。
为了简洁起见,我添加了一个非常简单的控制台应用程序示例:
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 秒刷新一次)
Name | Value |
---|---|
ValueX | 34 |
ValueY | 42 |
ValueZ | 42 |
检查 SignalR 是否满足此类要求
你使用 .net 核心..你想要一个在某个端口上提供 TCP-IP 服务的控制台应用程序..我想说的一个最小的解决方案是 link Kestrel 到你的控制台应用程序,分配请求-接收器事件并在您的端口上启动它。要重复刷新客户端,您可以使用客户端本地 JS 和计时器。您的服务器可以为客户端的浏览器生成通常的 HTML 合并该 JS 部分。
在控制台应用程序中使用 Kestrel 的示例:
注意:这是老式的“快速解决方案”。此外,对于 Kestrel,最好首先构建您的应用程序,作为报告服务,独立于平台选择。然后,您可以将该代码“插入”到您将来可能需要使用的任何平台(SignalR、Blazor、ASP、Azure 等)