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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:38:07+00:00 2026-05-26T11:38:07+00:00

I have a set of WCF services setup using wsHttpBinding with IIS and have

  • 0

I have a set of WCF services setup using wsHttpBinding with IIS and have implemented custom security (using message headers) to determine the user who is making the request. It has always been my understanding, based on a few years of netTcpBinding experience (using a Windows service to host, not IIS), that every request received by a WCF endpoint ends up on a new thread. Using this assumption, my custom security class (which implements IDispatchMessageInspector) sets the thread identity based on finding the “token” (a Guid) in the message header like so:

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
{
     string token = string.Empty;
     if (request.Headers.FindHeader("SecurityToken", Primitives.SerializationNamespace) > -1)
         token = request.Headers.GetHeader<string>("SecurityToken", Primitives.SerializationNamespace);

     if (!string.IsNullOrEmpty(token))
        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(token, "AppName"), new string[] { "testrole" });
    else
        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("guest", "AppName"), new string[] { });
     }

     return null;
 }

In testing this though, and despite my internal variable (which contains the user object) being marked ThreadStatic, new requests seem to retain the previous caller’s identity.

Here’s an example:

[ThreadStatic]
private static User _CurrentUser = null;

internal static User CurrentUser
    {
        get
        {
            //this holds the value of the previous caller's identity still, even though the
            //thread's Identity.Name is now equal to the new caller's Guid
            if (_CurrentUser == null)
                    _CurrentUser = UserData.GetByToken(Thread.CurrentPrincipal.Identity.Name);


            return _CurrentUser;
        }
    }

I also tried marking all of my service implementation’s (directly behind the .svc file) with the following attribute but that didn’t change anything:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

Any ideas on how I can guarantee every request is on a new thread? Or is there a better way to store/determine who the user making the request is without any possible overlap?

  • 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-05-26T11:38:08+00:00Added an answer on May 26, 2026 at 11:38 am

    Using ThreadStatic variable in the environment where you don’t have threading under your control is way to disaster. Both WCF (IIS and self-hosted) and ASP.NET internally use thread pool = each request is served by separate thread but these threads are reused for subsequent requests and there is not way to avoid it.

    Use custom OperationContext extension to store your user instead of ThreadStatic variable.

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

Sidebar

Related Questions

I have an OData Service setup using WCF Data Services (v2) that is hosted
I have a set of WCF web services connected to dynamically by a desktop
I have a set of existing WCF services hosted in a .NET 3.0 app.
I have a WCF client and service using HTTPS over the wsHttpBinding. One common
I have a WCF service that is using a custom ServiceAuthorizationManager . The custom
I am developing a secure set of service using WCF wsHttpBinding. I had to
I have a WCF RESTful web service (using the webHttpBinding) running under IIS, exposed
I have various bits of functionality implemented in WCF web services which are currently
I have a WCF Service, hosted inside of IIS, using NHibernate for data access.
I have a self-hosted WCF service with the InstanceContextMode set to PerSession. How can

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.