Something like Session for a Console Application

8.5k Views Asked by At

I'm developing a console application for telegram bot.

I have a NextFiledStrig which tell the application what field should be asked from user. As multiple users are communicating with the bot or application, the NextFiledStrig change for all.

Is there something like session so I can store the NextFiledStrig in it for each user? Or I have to get its value from database over and over?

I worked ASP.net C# and I'm new to console application.

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.

3

There are 3 best solutions below

4
On

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;
}
5
On

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?

0
On

I implemented a session system for my client-server application using AsyncLocal I will try to explain it:

If you run each client connection handler asynchronously:

  • Create a class to hold your session data using 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.

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.