简体   繁体   English

需要在执行期间保留信息的WCF服务

[英]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 我正在为我的高年级考试开发一个基于c#的自我托管WCF服务,我已经完成了服务器和客户端的编码(在控制台中,然后我将在UWP中升级客户端),服务器启动,客户端连接到它但是每次客户端调用函数时,服务库中的变量都将重置

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 列表giocatori总是只有一个成员,所以游戏无法启动,即使我连接5个客户端,他们也会收到他们的ID,但服务器会分别看到它们

connettiti works fine, it gives the ID, but avviare won't get that condition true connettiti工作正常,它提供了ID,但avviare不会得到这个条件

so, how can i make variables in the WCF service library keep their values for the entire execution of the server? 那么,我如何在WCF服务库中创建变量来保存服务器整个执行的值?

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. 在主机端,转到实现服务合同的服务,并使用service behavior属性设置其InstanceContextMode。

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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