What's the most portable way to make a Silverlight & Regular .NET REST client

前端 未结 5 815
臣服心动
臣服心动 2021-02-10 04:42

I\'m trying to get a Server application to expose some status information using WCF. In particular I\'m after using WCF services with RESTful \"API\". I\'m hitting somewhat of a

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-10 05:02

    If this is a simple Xml REST service, why not use the WebClient in Silverlight to capture the XML using Linq to XML? I know you said its messy, but it all depends on how you look at it. if you change your service interface at anytime you're going to have to update your code in multiple places. Thats just the way it is.

    So to do this, you will need to capture the data in an async fashion from the WebClient and then parse it with LINQ to XML.

    Time Heuer has a good example on his site: http://timheuer.com/blog/archive/2008/03/14/calling-web-services-with-silverlight-2.aspx

    Essentially, it looks like this:

    WebClient rest = new WebClient();
    rest.DownloadStringCompleted += new DownloadStringCompletedEventHandler(rest_DownloadStringCompleted);
    rest.DownloadStringAsync(new Uri("http://example.org/current-status/xml"));
    

    Then in your "rest_DownloadStringCompleted" you'd parse the string as XML. Like so:

    string data = e.Result;
    string url = string.Empty;
    
    XDocument doc = XDocument.Parse(e.Result);
    var myResults = from results in doc.Descendants("myXmlElement") ... blah blah blah 
    

    I've done the same thing with home grown REST Services from WCF and Silverlight and it worked great.

提交回复
热议问题