Click here to Skip to main content
15,867,931 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi every body,

is there any way to share a variable on wcf?

Example:

I've a wcf service like this:
[ServiceContract]
interface Iwcfservice
{
     [ContractOperation]
     void method1();

     [ContractOperation]
     void method2();
}

class wcfservice : Iwcfservice
{
     public void method1()
     {
     }

     public void method2()
     {
     }
}


Now, i would like method1() and method2() to use a shared variable. How can i do that? According to my knowledge, method1() and method2() will be called in two thread.
Posted
Updated 14-May-11 22:24pm
v3

Try running your web service in a single instance mode.
For details, see 3 ways to do WCF instance management (Per call, Per session and Single)[^].
 
Share this answer
 
Comments
thanhvinh0906 15-May-11 6:10am    
If i use InstanceContextMode.Single, will i have to synchronize the global variable? Because it will be accessed from other threads.
Abhinav S 15-May-11 8:52am    
Yes. You will need some form synchronization for sure. But remember, the same thing will be required by an other method.
Hi,
You can simply use custom properties in your Iwcfservice Interface. For example:
C#
[ServiceContract]
interface Iwcfservice
{
     [ContractOperation]
     void method1();
 
     [ContractOperation]
     void method2();
 int Count { get; set; } // Property interface
 string Name { get; set; } // Property interface 

}
 
Share this answer
 
Comments
#realJSOP 15-May-11 5:58am    
Or you can put those properties into your derived class.
[no name] 15-May-11 6:11am    
I 100% agreed with John.
thanhvinh0906 15-May-11 7:45am    
If so, will i have to synchronize it?
Mazertula 6-Nov-11 16:04pm    
No

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900