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 8354985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:43:24+00:00 2026-06-09T09:43:24+00:00

I want to use a pool of non-thread-safe encoder instances across multiple concurrent ASP.NET

  • 0

I want to use a pool of non-thread-safe encoder instances across multiple concurrent ASP.NET requests, without reinitializing them. If I didn’t care about initialization costs, I’d do this:

public class ResultController {
    public JsonResult GetResults() {
        List<MyViewModel> items = new List<MyViewModel>();
        // It obviously does more than this in real life
        for(id = 0; id < 1000; id++) {
            items.Add(new MyViewModel(id));
        }
        return Json(items);
    }
}

public class MyViewModel() {
    public string CodedId { get; set; }
    public MyViewModel(int id) {
        // This "new" is the concern
        CodedId = new ExpensiveToInitializeCodec().InexpensiveEncode(id);
    }
}

Works fine. All locals, no worries about threading, and no-one outside my model needs to understand that the value is presented encoded. However, a quick performance test shows that each initialization takes about .129ms, while an encode itself takes less than .006ms. (FYI, the codec is TripleDESCryptoServiceProvider).

I want to limit that initialization cost without passing pre-initialized objects around (such as into constructors to improve performance, but breaking separation of concerns). Here’s what I currently do, and it obviously gets messy outside this simple example:

public class ResultController {
    public JsonResult GetResults() {
        List<MyViewModel> items = new List<MyViewModel>();
        ExpensiveToInitializeCodec codec = new ExpensiveToInitializeCodec();
        for(id = 0; id < 1000; id++) {
            items.Add(new MyViewModel(id, codec));
        }
        return Json(items);
    }
}

public class MyViewModel() {
    public string CodedId { get; set; }
    public MyViewModel(int id, ExpensiveToInitializeCodec codec) {
        CodedId = codec.InexpensiveEncode(id);
    }
}

I could leverage the well-known ASP.NET per-request cache pattern:

public class MyViewModel() {
    public string CodedId { get; set; }
    public MyViewModel(int id) {
        CodedId = ExpensiveToInitializeCodec.Get().InexpensiveEncode(id);
    }
}

public class ExpensiveToInitializeCodec {
    public static ExpensiveToInitializeCodec Get() {
        ExpensiveToInitializeCodec codec = HttpContext.Current.Items["codec"];
        if (codec == null) {
            codec = new ExpensiveToInitializeCodec();
            HttpContext.Current.Items["codec"] = codec;
        }
        return codec;
    }
}

But that’s still wasteful to run in a tight loop: How much computation is behind a HttpContext.Current call?

It also seemed like a per-thread solution may be more precise than a per-request solution. Any suggestions that are compatible with an ASP.NET request?

Outside of ASP but still in the .NET space, one person’s answer was ThreadStatic: Using ThreadStatic to replace expensive locals — good idea? . However, http://blog.idm.fr/2010/03/aspnet-thread-agility-or-why-threadstatic-should-not-be-used.html apparently precludes that solution in ASP.NET. A similar question to mine Is there any way to imitate ThreadStatic for use with HttpContext.Current.Items? went unanswered.

EDIT: It looks like I may be able to use ThreadStatic if I make sure my codec usage isn’t interleaved with I/O operations, but I’m not sure how safe that is.

I’m reaching now, but some other approaches I’ve thought of include 1) providing a custom serialization/deserialization for items with an EncodedAttribute, 2) implementing my own static TripleDES single-block encryptor with no instance initialization overhead, 3) against my preferences, passing an externally-initialized encryptor into the constructor for each item, 4) implementing an IEncryptable interface and re-enumerating the items after the results are populated, 5) perform all encryption external to the viewmodel, implemented everywhere the viewmodel is used.

  • 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-09T09:43:25+00:00Added an answer on June 9, 2026 at 9:43 am

    Is the performance cost of accessing HttpContext.Current.Items really an issue or you are just speculating? According to the numbers you gave and the numbers in the question you gave and then numbers in the linked question it should not be:
    – You have 129 ms for single initialization + 6ms for 1000 iterations
    – HttpContext takes like 0.1ms over 10K iterations (i.e. 0.01 in your case)

    Thread static is a bad idea. While it may work you will not be able to use async controllers and similar features in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a thread pool to both initiate/cancel overlapped read operations --
I want use a single php file to handle all of my voting requests.
I use DBCP pool and I want use testOnBorrow and testOnReturn to test if
I want to use connection pool with JPA/TopLink in my web app running on
In my project, I want to use object pool for different types of objects
I am researching how to use a connection pool in C#.Net (we are using
I have a fixed thread pool that runs 7 concurrent threads at any time
I was looking for open source thread pool lib which I can use to
I want to use a JDBC connection pool. The most important factor is that
I want to use Server Managed connection pool in my web application,Now i'm using

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.