简体   繁体   English

类似于控制台应用程序的会话

[英]Something like Session for a Console Application

I'm developing a console application for telegram bot. 我正在为电报机器人开发控制台应用程序。

I have a NextFiledStrig which tell the application what field should be asked from user. 我有一个NextFiledStrig ,它告诉应用程序应该从用户那里询问什么字段。 As multiple users are communicating with the bot or application, the NextFiledStrig change for all. 当多个用户与机器人或应用程序通信时, NextFiledStrig会全部更改。

Is there something like session so I can store the NextFiledStrig in it for each user? 是否有类似会话的内容,以便我可以为每个用户在其中存储NextFiledStrig Or I have to get its value from database over and over? 还是我必须不断从数据库中获取其价值?

I worked ASP.net C# and I'm new to console application. 我使用过ASP.net C#,但我是控制台应用程序的新手。

I searched a lot but I couldn't find any thing solve my problem. 我进行了很多搜索,但找不到任何能解决我问题的方法。 Many people was answered: its a console application and there is just one user for it and you don't need session. 许多人被回答:它是一个控制台应用程序,只有一个用户,您不需要会话。

There is no session in a console application. 控制台应用程序中没有会话。 But you can use a simple dictionary to store your data: 但是您可以使用一个简单的字典来存储数据:

private static Dictionary<User, NextFiledStrig> s_nextFiledByUser = new Dictionary<User, NextFiledStrig>();

private static GetNextFiledForUser(User user) {
    NextFiledStrig value;
    bool isCached = s_nextFiledByUser.TryGetValue(user, out value);
    if (!isCached) {
        value = new NextFiledStrig(); //Replace this with the code that will get the value
        nextFiledByUser.Add(user, value);
    }
    return value;
}

If you have to use only console application, don't think. 如果您只需要使用控制台应用程序,那就不要想。 There is nothing like that. 没有那样的事。 You can only use database to store the value for key. 您只能使用数据库存储密钥的值。 But tell me how this bot would be invoked by users? 但是,请告诉我该机器人将如何被用户调用?

I implemented a session system for my client-server application using AsyncLocal I will try to explain it: 我使用AsyncLocal为我的客户端服务器应用程序实现了会话系统,我将尝试解释一下:

If you run each client connection handler asynchronously: 如果异步运行每个客户端连接处理程序:

  • Create a class to hold your session data using AsyncLocal 使用AsyncLocal创建一个类来保存您的会话数据
public class Session
{
    private static AsyncLocal<IDictionary<string,object>> _Data = new AsyncLocal<Dictionary<string,object>>();

    public static IDictionary Data
    {
        set
        {
            _Data.Value = value;
        }
        get
        {
            return _Data.Value;
        }
    }
}
  • Manage each Client connection asyncronously: 异步管理每个客户端连接:
// fire the client manager and go to keep listening for client connections.
Task manager = Task.Run(async () => await Client.Start())
  • Assign the Session data to a new dictionary: 将会话数据分配给新词典:
public async Task Start()
{
    Session.Data = new Dictionary<string,object>();

    // Do your receive, manage the bytes, etc.
}

This way, Session.Data will be unique on that async task and sub tasks even if is an static property. 这样,即使是静态属性,Session.Data在该异步任务和子任务上也将是唯一的。

As an example: 举个例子:

Session.Data = new Dictionary<string,object>() {{"value", 1}}

Console.WriteLine(Session.Data["value"].toString()); // will be 1

Task manager1 = Task.Run(async () => 
{
    Console.WriteLine(Session.Data["value"].toString()); // will be 1
    Session.Data = new Dictionary<string,object>() {{"value", 2}}
    Console.WriteLine(Session.Data["value"].toString()); // will be 2

    Task manager2 = Task.Run(async () => 
    {
        Console.WriteLine(Session.Data["value"].toString()); // will be 2
        Session.Data = new Dictionary<string,object>() {{"value", 3}}
        Console.WriteLine(Session.Data["value"].toString()); // will be 3
    }

    Task manager3 = Task.Run(async () => 
    {
        Console.WriteLine(Session.Data["value"].toString()); // will be 2
        Session.Data = new Dictionary<string,object>() {{"value", 4}}
        Console.WriteLine(Session.Data["value"].toString()); // will be 4
    }

    Console.WriteLine(Session.Data["value"].toString()); // will be 2
}

Console.WriteLine(Session.Data["value"].toString()); // will be 1

Hope this helps you or at least points on a good direction. 希望这可以帮助您或至少指向正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Windows应用程序中有类似SESSION的东西吗? - Is there something like SESSION in Windows application? 使控制台应用程序的行为类似于Windows应用程序 - Making a console application behave like a Windows application 通过Servicebus之类的模块在应用程序内部进行通信 - Modules communication inside application by something like servicebus C#控制台应用程序验证会话 - C# console application authentication session C#控制台应用程序设计就像htop一样 - C# console application design like htop 控制台应用程序服务-我需要注意些什么吗? - Console Application to Service - do I have to note something? 如何在WPF应用程序中执行类似屏幕保护程序的操作? - How to do something like a screensaver in my WPF-Application? C# 扩展适用于控制台应用程序,但不适用于 MVC Web 应用程序。 我错过了什么吗? - C# Extensions working in console application but not in MVC web application. Am I missing something? ASP.NET Web窗体,等效或类似的内容,例如网页上的文本控制台 - ASP.NET Web Forms, Something equivalent or similar like a Console for Text on a webpage Web API-在应用程序的整个生命周期(如会话)中保存用户数据 - Web api - Saving user data throughout the life of the application like Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM