Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:26:24+00:00 2026-06-11T17:26:24+00:00

I am working on an ASP.NET site where it is required that a single

  • 0

I am working on an ASP.NET site where it is required that a single Socket be created and kept connected across all pages for the current session. I have created and stored the socket as a session object and it remains connected after a page redirect, but the connection is closed whenever a postback occurs (such as a button click). Is there any way to keep the socket connection open even after a postback, as the requirement is that a single connection be used for the entire session and reconnecting the socket is not an option.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:26:25+00:00Added an answer on June 11, 2026 at 5:26 pm

    Session-state is brittle; in fact, what you describe won’t even work if you are using a serializing session-state provider (a socket is not serializable).

    So: rather than storing the socket in session, put the socket somewhere else – maybe a synchronized dictionary – and store just a key to it in session. For example:

    public static class SocketStore
    {
        static readonly Dictionary<string, Socket> socketStore
              = new Dictionary<string, Socket>();
        public static string Add(Socket socket)
        {
            if(socket == null) throw new ArgumentNullException("socket");
            string newKey = Guid.NewGuid().ToString();
            lock(socketStore) {
                socketStore.Add(newKey, socket);
            }
            return newKey;
        }
        public static Socket Get(string key)
        {
            if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
            Socket result;
            lock (socketStore)
            {
                if (!socketStore.TryGetValue(key, out result)) result = null;
            }
            return result;
        }
    }
    

    In usage, if you don’t have a socket (/key) already, create your connection, store it with Add, and then store the key in session. When fetching, you must handle the null case, and you will also want to add your own synchronization, to guard against the same socket being used by two concurrent requests from the same session (perfectly legal).

    However, as additional notes:

    • you will want to have some additional process to clean down old sockets from abandoned sessions, and zombie-sockets from healthy-looking sessions
    • you will need requests to keep coming back to the same server – so “sticky” load-balancing if you are in a cluster
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an asp.net web site that is based upon a single
I'm currently working on an ASP.NET site created by someone else. There is this
I'm working on a ASP.NET (C#) site that pulls most of its data from
I'm currently working on an ASP.NET web-site. In one of my pages, I had
I have a web site that is designed in asp.net and working fine in
I'm working on a web site where the new pages are ASP.NET and the
I'm feeling stupid. I've just created my first ASP.net 4.0 site after working my
I am working on an internal, custom ASP.NET site that was specified by the
I have an ASP.NET site that was working fine running on Windows Server 2003
In one of the ASP .Net site we are currently working we have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.