简体   繁体   中英

WCF service which needs to keep information during execution

i'm developing a self-hosted c# based WCF service for my senior year exam, i have finished coding both server and client(both in console, then i will upgrade the client in UWP), the server starts, clients connects to it but variables in the service library will reset each time a client calls a function

public static int connettiti()
    {
        Random rand = new Random();
        int ID = rand.Next(1, 999999);
        giocatori.Add(ID);
        contagiocatori++;
        if (giocatori.Count == 0)
        {
            codicevincitore = ID;
        }
        else if (giocatori.Count > 2)
        {
            return 1000000;
        }
        return ID;
    }
    public static bool avviare()
    {
        bool avvia = false;
        int count = giocatori.Count;
        if (count == 2)
        {
            avvia = true;
        }
        return avvia;
    }

List giocatori has Always only one member, so game won't start, even if I connect 5 clients, they receive their IDs, but the server sees them separately

connettiti works fine, it gives the ID, but avviare won't get that condition true

so, how can i make variables in the WCF service library keep their values for the entire execution of the server?

By default, each call will take place on a new instance of the service. On your host side, go to the service that implements your service contract and use the service behaviour attribute to set its InstanceContextMode.

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

https://msdn.microsoft.com/en-us/library/ms731193(v=vs.110).aspx

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM