简体   繁体   English

如何将数据(SQL或LINQ)从WCF双工服务推送到Silverlight客户端

[英]How to push data (SQL or LINQ), from WCF Duplex Service to Silverlight client

I have made simple chat application to learn some new things, and what I want to do now, is to add user list that are currently logged into chat. 我已经制作了一个简单的聊天应用程序来学习一些新东西,现在我想做的是添加当前登录到聊天室的用户列表。 So I did this: 所以我这样做:

        public ObservableCollection<User> GetUserList()
    {
        ObservableCollection<User> users = new ObservableCollection<User>();

        IEnumerable<User> userEnu;
        userEnu = from u in _clientDic
                  select new User
                  {
                      UserName = u.Key
                  };

        foreach (User user in userEnu)
        {
            users.Add(user);
        }

        IEnumerable<IDuplexClient> clients;
        clients = from p in _clientDic
                  select p.Value;

        foreach (IDuplexClient item in clients)
        {
            item.DisplayUserList(users);
        }
        return users;
    }

There is also DataConctract that coressponding to User. 还有与用户相对应的DataConctract。 Anyway. 无论如何。 This method get all users that are present in dictionary and put them into ObservableCollection. 此方法获取字典中存在的所有用户,并将其放入ObservableCollection。

What I want to do, is to push data from this collection to user, if it change (users log or logout). 我想要做的是,如果数据更改(用户登录或注销),则将数据从该集合推送给用户。

So far I only managed to manually pull data from Server by calling 到目前为止,我仅设法通过调用从服务器手动提取数据

_client.GetUserListAsync()

manually. 手动。 Trough pushing button in silverlight app. 在silverlight应用程序中的槽按钮。 But that's not why I'm using duplex connection, to force user (or client app) for perdiodic calls to server, that check if there is new data or not. 但这不是为什么我使用双工连接来强制用户(或客户端应用程序)对服务器进行定期调用,以检查是否有新数据。 I want server to push any new data to client. 我希望服务器将任何新数据推送到客户端。

    [ServiceContract]
public interface IDuplexClient
{
    [OperationContract(IsOneWay = true)]
    void DiplayMessage(string message);

    [OperationContract(IsOneWay = true)]
    void DisplayUserList(ObservableCollection<User> userColl);
}

And here is service contract. 这是服务合同。 Honestly I don't really know if this is relevant, but I post it just in case. 老实说,我真的不知道这是否相关,但是我以防万一。

你可以使用pollingDuplexHttpBinding来做到这一点

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

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