Static Variables in WCF

I have some WCF services. These services run in ASP.NET. I want these services to have access to a static variable. My problem is that I'm not sure where the corresponding server-level storage engine is located. I do not want to use the database because of speed. But I want the static variables to remain in memory as long as possible. In fact, I would like it to stay until I restart my server, if possible.

Can someone provide me any ideas?

+8
c # wcf
source share
3 answers

You can use static variables in WCF, but you must correctly synchronize access to them, because they can potentially be accessed from multiple threads at the same time. Values ​​stored in static variables are available everywhere in AppDomain and remain in memory until the server restarts.

+14
source share

You might have something like this

public static class StaticVariables { private static string _variable1Key = "variable1"; public static Object Variable1 { get { return Application[_variable1Key]; } set { Application[_variable1Key] = value; } } } 

The application itself is thread safe, but the material you add to it may not be; so keep that in mind.

+4
source share

If all services are in the same ServiceContract , and if all the member variables in your service can be shared in all sessions, you can set ServiceBehavior for one instance .

 [ServiceBehavior( InstanceContextMode = InstanceContextMode.Single )] public class MyService : IMyServiceContract { } 
0
source share

All Articles