views:

79

answers:

3

I have little Silverlight app that needs to make quries to server, is it possible to return objects to silverlight app or how can I communicate with server?

A: 

In my opinion it might be best to use web services to ship whatever is needed to your Silverlight application. I suggest that you use the WebClient class in combination with the URI class to obtain the data. Example:

Uri uri = new Uri(//the url of you webservice, UriKind.RelativeOrAbsolute);

Now create an instance of the WebClient class and add a callback to be called when the read from the web service is completed:

WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(CallbackMethod);
wc.OpenReadAsync(uri);

When the data is retrieved from the server, the CallbackMethod is called. The method has an EventArgs object that contains a property called result. You can obtain your data using that property.

Genady Sergeev
+1  A: 

Use a WCF Service. As long as your objects are Serializable, the runtime will encode and decode them for you transparently.

A simple Silverlight-enabled WCF service looks something like this:

using System.ServiceModel;
using System.ServiceModel.Activation;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode =
    AspNetCompatibilityRequirementsMode.Allowed)]
public class YourService
{
    [OperationContract]
    public string DoStuff(string arg)
    {
        return arg + "stuff";
    }
}

You can replace "string" with your datatype by creating a [DataContract].

RickNZ
A: 

Silverlight doesnt need ASP at all to function, if you have a database on a seperate server check out WCF, and then get Silverlight to communicate with the WCF service and then the service with the database, if you want something more transparent, then try out WCF RIA services, this allows you to have a middle-tier approach to data access in silverlight

Neil