C#WCF多个客户端接收所有相同的数据

时间:2016-10-19 13:05:38

标签: c# wcf service client

我的问题是我有一个WCF服务和倍数 le客户端可以连接/订阅该服务并获取所有随机数据 通过服务回调...当客户订阅服务时,计时器启动,那就是每个客户端都获得所有不同的数据,但这就是我想要实现的......我做错了什么?

当一个客户订阅该服务时会发生这种情况:

public SubscribeResult Subscribe()
        {
            SubscribeResult retVal = new SubscribeResult();
            try
            {
                retVal.ClientID = Guid.NewGuid().ToString();
                clientID = retVal.ClientID;
                ServiceCallback = OperationContext.Current.GetCallbackChannel<ITraceCallback>();

                    ti = new Timer();
                    ti.Elapsed += Ti_Elapsed;
                    ti.Interval = 1000;
                    ti.Enabled = true;
                    ti.Start();

                retVal.Success = true;
                SubscribeClientList.Add(new SubscribeItem {CLIENTID = clientID, SENT = true });
                connectedClients += 1;
            }
            catch (Exception ex)
            {
                retVal.Success = false;
                Console.WriteLine("Something went horribly wrong! \n" + ex.Message);
            }
            return retVal;
        }

当计时器达到零时会发生这种情况:

private void Ti_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                if (isSending == false)
                {
                    isSending = true;
                    // generate message
                    CreateRandomString crs = new CreateRandomString();
                    string msg = crs.CreateString(rnd.Next(15, 25));
                    // increase entry
                    entry += 1;
                    // create timestamp of creation
                    DateTime creation = DateTime.Now;
                    // generate level
                    int muchwow = rnd.Next(1, 4);
                    string lvl = "undefined";
                    switch (muchwow)
                    {
                        case 1:
                            lvl = "Error";
                            break;
                        case 2:
                            lvl = "Warning";
                            break;
                        case 3:
                            lvl = "Information";
                            break;
                    }

                    bool sucess = ServiceCallback.Message(entry, creation, lvl, msg);

                    if (sucess == true)
                    {
                        CreateRandomString messagePreview = new CreateRandomString();
                        string prev = messagePreview.TruncString(msg, 20);
                        Console.WriteLine("[" + DateTime.Now + "] : [" + clientID + "] : " + "'" + prev + "' : " + "(" + lvl + ")");

                        sucess = false;
                    }

                    isSending = false;
                }

0 个答案:

没有答案