how to make SqlRoleProvider.ApplicationName thread safe

F

Francis Reed

Hi

I'm currently working on a new portal for an online university, and we need
write code that manages roles for multiple applications. To tell the
SqlRoleProvider which Application we are currently managing I am using the
SqlRoleProvider.ApplicationName Property which is not thread safe. My
question is how do I make this property thread safe? Here's a sample of the
code.

....

Roles.Provider.ApplicationName = "Chemistry101";
Roles.AddUsersToRoles("Bob, Peter", "Student");
Roles.CreateRole("TeacherAssistant");


Roles.Provider.ApplicationName = "Chemistry239";
Roles.AddUsersToRoles("Josh, Sam", "Teacher");
Roles.AddUsersToRoles("Frank, Hui", "Student");
Roles.DeleteRole("LabMonitor");

....

Also could anyone tell me if the following class would make the
ApplicationName Property thread safe?

public class EconcordiaRoleProvider : SqlRoleProvider
{
private static Object mutex = new Object();

public EconcordiaRoleProvider() : base()
{

}

public override string ApplicationName
{
get
{
lock(EconcordiaRoleProvider.mutex)
{
return base.ApplicationName;
}
}
set
{
lock (EconcordiaRoleProvider.mutex)
{
base.ApplicationName = value;
}
}
}
}

Thank you for the time you have given this post, and I hope to hear from you
in the near future. If you have any advice or suggestions, please don't
hesitate to reply to this post.

Cheers
Francis
 
W

wfairl

Two things:
1.) Looks like you're going to be doing a lot more reads than writes,
use a readerwriterlock
2.) You should be using an instance variable to lock in your case (
lock(this)), not a static variable since you're synchronizing a
non-static resource.
 
E

Erik Funkenbusch

Hi

I'm currently working on a new portal for an online university, and we need
write code that manages roles for multiple applications. To tell the
SqlRoleProvider which Application we are currently managing I am using the
SqlRoleProvider.ApplicationName Property which is not thread safe. My
question is how do I make this property thread safe?

Why do you need it to be thread safe? Do you realize the merely having
muliple web users using it doesn't violate thread consistency? The only
reason you would need it to be thread safe is if you are launching
background threads within your pages, which frankly is a difficult thing to
track and manage anyways, and generally shouldn't be done unless you really
know what you are doing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top