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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:51:03+00:00 2026-05-12T09:51:03+00:00

I have a WCF service with a security class for getting some of the

  • 0

I have a WCF service with a security class for getting some of the attributes of the calling user. However I’m quite bad when it comes to thread safety – to this point, I haven’t needed to do much with it, and only have a rudimentary theoretical understanding of the problems of multi-threading.

Given the following function:

public class SecurityService
{
    public static Guid GetCurrentUserID()
    {
        if (Thread.CurrentPrincipal is MyCustomPrincipal)
        {
            MyCustomIdentity identity = null;
            MyCustomPrincipal principal = (MyCustomPrincipal)Thread.CurrentPrincipal;
            if (principal != null)
            {
                identity = (MyCustomIdentity)principal.Identity;
            }

            if (identity != null)
            {
                return identity.UUID;
            }
        }
        return Guid.Empty;
    }
}

Is there any chance that something could go wrong in there if the method is being called at the same time from 2 different threads? In my nightmares I see terrible consequences if these methods go wrong, like someone accidentally getting someone else’s data or suddenly becoming a system administrator. A colleague (who also he was not an expert, but he’s better than me) thought it would probably be okay because there’s not really any shared resources that are being accessed there.

Or this one, which will access the database – could this go awry?

    public static User GetCurrentUser()
    {
        var uuid = GetCurrentUserID();
        if (uuid != null)
        {
            var rUser = new UserRepository();
            return rUser.GetByID(uuid);
        }
        return null;
    }

There’s a lot of discussion about the principals of threading, but I tend to fall down and get confused when it comes to actually applying it, and knowing when to apply it. Any help appreciated.

I can explain more about the context/purpose of these functions if it’s not clear.

EDIT: The rUser.GetByID() function basically calls through to a repository that looks up the database using NHibernate. So I guess the database here is a “shared resource”, but not really one that gets locked or modified for this operation… in which case I guess it’s okay…?

  • 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-12T09:51:03+00:00Added an answer on May 12, 2026 at 9:51 am

    From what I see, the first example only accesses thread-local storage and stack-based variables, while the second one only accesses stack-based variables.

    Both should be thread-safe.

    I can’t tell if GetByID is thread safe or not. Look to see if it accesses any shared/static resources. If it does, it’s not thread-safe without some additional code to protect those resources.

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

Sidebar

Related Questions

I have A WCF service that has a class that inherits System.Web.Security.RoleProvider. In this
I have a WCF service: <%@ ServiceHost Language=C# Debug=true Service=IWW.MIGTurbo2.WCF.Security.SecurityBroker Factory=System.ServiceModel.Activation.WebScriptServiceHostFactory %> This works
I am trying to configure WCF service with security. I have generated 2 certificates
I am a WCF / Security Newb. I have created a WCF service which
I have WCF service. Here is configuration <basicHttpBinding> <binding name=EmergencyRegistratorBinding> <security mode=TransportCredentialOnly> <transport clientCredentialType=Windows
I have a WCF web service with WS-* security and I need to write
Let's say I have some classes defined as follows: class Security { Boolean AuthenticateUser(String
I have a WCF Service that requires a security token issued by a separate
I have a WCF Service with a binding endpoint with security mode set to
Hi! I have a WCF Service that runs over TCP with Certificate security like

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.