简体   繁体   English

如何从WCF服务调用Silverlight方法

[英]How to call Silverlight methods from WCF services

I'm new in Silverlight and WCF. 我是Silverlight和WCF的新手。 I've created a simple Silverlight application from where I'm displaying the alert message box with data from list object collection after a wait of one second. 我创建了一个简单的Silverlight应用程序,等待一秒钟后,我将在其中显示警报消息框以及来自列表对象集合的数据。

I've used WCF to connect to database. 我已经使用WCF连接到数据库。 But I'm first adding all the database data in collection list, and then sending that list object to silverlight, which silverlight is iterating. 但是我首先将所有数据库数据添加到集合列表中,然后将该列表对象发送到Silverlight,而Silverlight正在迭代该对象。

WCF Service code to connect to database in inserting the data in List collection: 在列表集合中插入数据时要连接到数据库的WCF服务代码:

public List<int> GetData()
        {
            List<int> list = new List<int>();
            using (SqlConnection connection = new SqlConnection())
            {
                connection.ConnectionString =
                    System.Configuration.ConfigurationManager.ConnectionStrings["sqlConnection"].ToString();
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.Text;
                    command.CommandText = "Select * from insertItem";
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            list.Add(reader.GetInt32(1));

                        }
                    }
                }

            }
            return list;
        }

Silverlight code to access list class from WCF service and displaying it in message box after a wait of one second: Silverlight代码从WCF服务访问列表类,并在等待一秒钟后在消息框中显示它:

Service1Client client = new Service1Client();
           client.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(DisplayResults);
           client.GetDataAsync(1);

 private void DisplayResults(object sender, GetDataCompletedEventArgs e)
        {
            timer.Stop();
            ObservableCollection<int> list = e.Result;
            foreach (int i in list)
            {

                HtmlPage.Window.Alert(i.ToString());
                Thread.Sleep(1000);
            }

        }

Could anyone please tell me how can I display the data directly to silverlight from WCF services without waiting for first putting all the data in List class collection, and then displaying it? 有人可以告诉我如何直接从WCF服务向Silverlight显示数据,而不必等待首先将所有数据放入List类集合中,然后再显示它们吗? What could have been a better approach? 有什么可以是更好的方法?

I am not sure if you are asking if what you are doing is best practice. 我不确定您是否要问自己在做什么是最佳实践。 Yes, you need to collect data from a database first to be able to sent it through a webservice to your caller. 是的,您需要首先从数据库收集数据,然后才能通过网络服务将其发送给呼叫者。 You did that, and thats fine. 您做到了,就这样。 You could have used some mapper maybe, but as that part works, you should be fine. 您可能已经使用了一些映射器,但是当该部分工作时,您应该没问题。 Why change it? 为什么要改变它? What is the problem? 问题是什么? Is it slow? 慢吗? Must be something different, maybe its too much data you are trying to send? 必须有所不同,也许您尝试发送的数据太多? Try putting some constraints in, only retrieve the first 15 items, and continue requesting more as your user browses through the results. 尝试施加一些约束,仅检索前15个项目,并在用户浏览结果时继续请求更多。

If you are concerned that it takes too long to add stuff to a list, dont be, that is probably not the problem but rather the size of data requested. 如果您担心将内容添加到列表花费的时间太长,请不要这样做,这可能不是问题,而是请求的数据大小。

To display the data, you just add a capable control and set the datasource to the list. 要显示数据,只需添加一个功能强大的控件并将数据源设置为列表即可。 Done! 完成!

Here is a walkthrough , and here is an article covering that topic by your silverlight Guru no1, ScottGu himself. 这是一个演练 ,这是Silverlight Guru no1(ScottGu本人)介绍该主题的文章 Should help you getting started. 应该可以帮助您入门。

If you really want to change how you access your data, you might as well take a look at RIA Services for Silverlight , though that is considered an advanced technique. 如果您确实想更改访问数据的方式,则最好看看RIA Services for Silverlight ,尽管这被认为是一种高级技术。

If that doesnt help you, you might want to refine your question. 如果那没有帮助您,您可能想完善您的问题。 So, what do you think needs to be changed in your current design? 那么,您认为您当前的设计需要改变什么? What doesnt work out as expected? 什么没有按预期进行?

EDIT: After you have answered my comment i think you might achieve what you want to do by implementing a Duplex Service . 编辑:在您回答我的评论后,我认为您可以通过实现Duplex Service来实现您想要的功能。

This topic describes how to create a duplex Windows Communication Foundation (WCF) service that can communicate with a Silverlight client . 本主题介绍如何创建可以与Silverlight客户端进行通信的双工Windows Communication Foundation(WCF)服务 A duplex service maintains a callback channel to the Silverlight client , which allows the service to make calls to the client . 双工服务维护到Silverlight客户端的回调通道 ,允许该服务向客户端进行呼叫 Duplex services have numerous applications including, for example, a chat server for instant messaging or a monitoring service that sends notifications to the client. 双工服务具有许多应用程序,例如包括用于即时消息传递的聊天服务器或将通知发送到客户端的监视服务。 This sample provides a service that allows a client to order a specified number of product items by name. 此样本提供了一项服务,该服务使客户可以按名称订购指定数量的产品。 It simulates processing the order and then calls back to the client with status on the order. 它模拟了订单的处理,然后以订单状态回叫给客户。

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

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