[英]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: 如果异步运行每个客户端连接处理程序:
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;
}
}
}
// fire the client manager and go to keep listening for client connections.
Task manager = Task.Run(async () => await Client.Start())
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.