我想在调用两个客户端代理时在两个服务之间共享一个变量。 (并且每个会话也应该共享变量)我的示例代码如下,
**服务器**
服务1
ioctl(uinp_fd, UI_SET_EVBIT, EV_ABS);
for (i = 0; i <= ABS_CNT; i++) {
ioctl(uinp_fd, UI_SET_ABSBIT, i);
}
服务2
[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);
}
我正在为这两种服务使用'wsHttpBinding'。
**客户**
[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();
}
答案 0 :(得分:0)
用静态变量编写一个类 例如:
public static class SharedValues
{
public static String SharedValue = string.Empty;
}
然后使用
public void SETValue(string inputString)
{
SharedValues.SharedValue = inputString;
}
如果您希望使用锁定语句的并发协同工具