What is the scope of a Static Class?
Asked Answered
H

5

20

I have an assembly that may be used by more than one process at a time. If I am using a static class, would the multiple processes all use the same "instance" of that class?

Since the processes are separate, would these be running under difference Application Domains, hence have the static "instances" separate?

The pudding in the details here is that the assembly is being used by a custom BizTalk adapter that my be set to process the messages in parallel batches. That is what I am calling "multiple processes" above.

Homesteader answered 12/11, 2008 at 16:25 Comment(0)
T
12

Multiple threads would share an instance. For this reason a static class can be convenient for passing state between threads, but you need to be very careful not to introduce race conditions (Monitor or lock your properties).

However, multiple processes should be in separate AppDomains and therefore each have their own instance.

Tula answered 12/11, 2008 at 16:30 Comment(2)
What about a web application. If I use the same static class defined in DLL #1 from two OTHER separate assemblies (DLL #2 and DLL #3), being #3 a website application and #2 a DLL referenced by the website, would they share the same static class values?Vulnerable
@LordofScripts Yes, because they are all in the same AppDomainTula
F
13

Static classes exist once per application domain. In your case, it would depend on whether the adapter is using multiple threads in the same application domain (thus sharing a single instance of the static class) or using multiple processes (thus having separate instances of the static class).

Fruge answered 12/11, 2008 at 16:30 Comment(0)
T
12

Multiple threads would share an instance. For this reason a static class can be convenient for passing state between threads, but you need to be very careful not to introduce race conditions (Monitor or lock your properties).

However, multiple processes should be in separate AppDomains and therefore each have their own instance.

Tula answered 12/11, 2008 at 16:30 Comment(2)
What about a web application. If I use the same static class defined in DLL #1 from two OTHER separate assemblies (DLL #2 and DLL #3), being #3 a website application and #2 a DLL referenced by the website, would they share the same static class values?Vulnerable
@LordofScripts Yes, because they are all in the same AppDomainTula
W
6

"I have an assembly that may be used by more than one process at a time. If I am using a static class, would the multiple processes all use the same "instance" of that class?"

No, they all have separate instances.

"Since the processes are separate, would these be running under difference Application Domains, hence have the static "instances" separate?"

Yes.

Woad answered 12/11, 2008 at 16:31 Comment(0)
C
3

The scope of a static class is limited to the application domain. Each app domain will have its own copy of any static variables you might have. If your "processes" are threads within the same app domain, then they will share the static values. But if they are actual separate Windows processes, then they will have different app domains and hence separate copies.

Cockade answered 12/11, 2008 at 16:31 Comment(0)
C
2

You may want to look at the Singleton pattern. The gist seems to be you want to control the number of service instances.

I'm guessing that you want a separate dll/project servicing all the client requests. You could use static class/singleton/multiton to implement the desired functionality. It really depends on what you are trying to accomplish.

Clitoris answered 12/11, 2008 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.