通过.Net Core 3.1中的Web浏览器公开控制台应用程序信息 [英] Expose console app info through web browser in .Net core 3.1

查看:78
本文介绍了通过.Net Core 3.1中的Web浏览器公开控制台应用程序信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个沉重的控制台应用程序,该应用程序执行多个线程,这些线程可以读取,执行计算并写入不同情况下的输出.现在,我试图为所有人创建一种方法,使每个人都可以使用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:

注意:这是老式的快速解决方案".同样,使用Kestrel,最好先构建应用程序作为报告服务,而不受平台选择的影响.然后,您可以插入"您将来可能需要使用的任何平台(SignalR,Blazor,ASP,Azure等)中的代码

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆