How to call Silverlight methods from WCF services
I'm new in Silverlight and 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.
I've used WCF to connect to database. But I'm first adding all the database data in collection list, and then sending that list object to silverlight, which silverlight is iterating.
WCF Service code to connect to database in inserting the data in List collection:
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.C开发者_开发问答reateCommand())
{
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:
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? 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.
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. 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.
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.
This topic describes how to create a duplex Windows Communication Foundation (WCF) service that can communicate with a Silverlight client. A duplex service maintains a callback channel to the Silverlight client, which allows the service to make calls to the client. 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.
精彩评论