Frage

i have nettcpbinding service and host it with windows service. the service must work on network and it handle incoming message comes from more than 100 clients.

Problem : I want have a property that all session can access to it . like this :

class a
{
   list<string> strList=new list<string>();

class b{}
class c{}
...
}

in this example all class can access to strList. i want have a list that all session can access to it (add or remove thing in that list).

service configuration is buffered and none security . and service attribute is here :

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
[ServiceContract(SessionMode = SessionMode.Required)]

EDIT : i dont want create that classes that was only one example. i just need have a list that all session can access to it.when you have InstanceContextMode.PerSession service that service class will create per each client then each client have their own service class now i want each session that created can access one public list .

EDIT2: this list is in server and just server can access it dont need send list to client. it is server variable for calculate some thing .

War es hilfreich?

Lösung

You can use a static property in your service class for example:

[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);
        }
    }

}

WCF will create a new instance of MyService for each new client connecting to your service. They all have access to the static property.

Andere Tipps

You would probably want your ConcurrencyMode set to Multiple, and you will need to ensure you treat your object as multithreaded, since that is what you are basically setting up. There is a pattern to avoid deadlock in this situation that requires multiple locking. See This MSDN article for details.

[SerivceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
class MyService : IMyContract
{

//Here is your shared object
static List<string> _strList = new List<string>();

//Only access the property for thread safety, not the variable
static List<string> StrList
{
    get
    {
        lock(typeof(MyService)
        {
            return _strList;
        }
    }
    set
    {
        lock(typeof(MyService)
        {
            _strList = value;
        }
    }
}

void DoSomeThing()
{
    lock(typeof(MyService))
    {
        //Do something with your list here, other threads are blocked from accessing
        // objects in lock
        StrList.Add("Something");
    }
}
}

If you are looking at using a collection though, I would suggest looking at the Concurrency Collection objects, as these are made for multithreaded access. Those objects take out the need for locking, as they have self contained locking, which is nice in the case you forget in the future.

EDIT: Just to add for information, locking will block other threads from executing code in that lock or accessing a locked property. For this reason, it is not suggested to to lengthy opertions in a lock as it can slow down ALL clients. Additionally, your InstanceContextMode being set to PerCall will create a new instance of your service class for EVERY CALL to your service. Inversly, if you don't have any activity and all your service instance get cleaned up or closed out, then you will also lose your shared object.

I think you should build a DataContract

Example :

  [DataContract]
    public class A
    {
     [DataMember]
     list<string> strList { get; set; }

     class B
       {
       }

     class C
       {
       }
   }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top