开发者

How can I get objects from ASP server to my Silverlight 3 app?

I have little Silverlight app that needs to make queries to server, is it possible to return objects to silverlight app or how can I communicat开发者_如何学Pythone with server?


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].


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜