I want to share a variable between two services when calling two client proxies. (And also variable should be shared per session) My sample code as below,
** Server **
Service 1
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyService : IMyService
{
public string SharedValue = string.Empty;
public void SETValue(string inputString)
{
SharedValue = inputString;
}
}
[ServiceContract(SessionMode = SessionMode.Required)]
public interface IMyService
{
[OperationContract(IsOneWay = true)]
void SETValue(string inputString);
}
Service 2
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyService2 : MyService, IMyService2
{
public string GETSharedValue()
{
return base.SharedValue; //This value got Empty
}
}
[ServiceContract(SessionMode = SessionMode.Required)]
public interface IMyService2
{
[OperationContract]
string GETSharedValue();
}
I'm using 'wsHttpBinding' for both services.
** Client **
private string sharedValue = string.Empty;
MyServiceReference.MyServiceClient client = null;
MyService2Reference.MyService2Client client2 = null;
public Form1()
{
InitializeComponent();
client = new MyServiceReference.MyServiceClient();
client2 = new MyService2Reference.MyService2Client();
}
private void button1_Click(object sender, EventArgs e)
{
client.SETValue("Hello this is client 1");
sharedValue = client2.GETSharedValue();
lblResult.Text = sharedValue;
}
write a class with a static variable eg:
public static class SharedValues
{
public static String SharedValue = string.Empty;
}
then use
public void SETValue(string inputString)
{
SharedValues.SharedValue = inputString;
}
if you want concurrency cosider using a lock statement
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.