Mam*_*d R 2 c# service wcf windows-services nettcpbinding
我有nettcpbinding服务并使用Windows服务托管它.该服务必须在网络上工作,它处理来自100多个客户端的传入消息.
问题:我想拥有一个所有会话都可以访问它的属性.像这样 :
class a
{
list<string> strList=new list<string>();
class b{}
class c{}
...
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,所有类都可以访问strList.我希望有一个列表,所有会话都可以访问它(添加或删除该列表中的东西).
服务配置是缓冲的,没有安全性.和服务属性在这里:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
[ServiceContract(SessionMode = SessionMode.Required)]
Run Code Online (Sandbox Code Playgroud)
编辑: 我不想创建那些只是一个例子的类.我只需要一个列表,所有会话都可以访问它.当你有服务类将为每个客户端创建的InstanceContextMode.PerSession服务,然后每个客户端都有自己的服务类现在我希望每个创建的会话可以访问一个公共列表.
EDIT2: 此列表在服务器中,只是服务器可以访问它不需要发送列表到客户端.它是用于计算某些东西的服务器变量.
您可以在服务类中使用静态属性,例如:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
[ServiceContract(SessionMode = SessionMode.Required)]
public class MyService : IMyService {
// this is your static data store which is accessible from all your sessions
private static List<string> strList = new List<string>();
// an object to synchronize access to strList
private static object syncLock = new object();
public void AddAction(string data) {
// be sure to synchronize access to the static member:
lock(syncLock) {
strList.Add(data);
}
}
}
Run Code Online (Sandbox Code Playgroud)
WCF将为连接到您的服务的每个新客户端创建一个新的MyService实例.他们都可以访问静态属性.