I have been looking for a nice implementation of XML-RPC for silverlight for a while now. There are a few good resources out there such as XML-RPC.NET, but they don’t really work on Silverlight.
Silverlight is different, that it only has asynchronous network calls and you can’t really imitate sync calls, which makes the use of a RPC a little weird.
I have written a small XML-RPC library for Silverlight. Actually its just a single C# file which users can just drag and drop into their projects. The library is still under development, presently I am hacking together a parser, making sure it works with the most common scenarios.
Here is what the usage of this library looks like. You instantiate a service class, and then instantiate query requests as shown below:
XmlRpcService service
= new XmlRpcService ("http://betty.userland.com/RPC2");
XmlRpcRequest req
= new XmlRpcRequest (service, "examples.getStateName",
new object [] { 10 });
req.XmlRpcCallCompleteHandler
+= new XmlRpcCallComplete (req_XmlRpcCallCompleteHandler);
req.Execute (null);
Once your call completes or some error occurs, your event handler is invoked (Here I am just assuming that the call succeeded):
void req_XmlRpcCallCompleteHandler (XmlRpcResponse response, object userState) {
string result = (string)response.TryCast (typeof (string));
System.Diagnostics.Debug.WriteLine ("Result: " + result);
}
You try to cast the response into a string, if it cannot be casted you get back a null or the default value for value types. TryCast is pretty powerful, you can declare a class and have TryCast map the returned struct (associative array) to that class. E.g. lets say we have this fault class:
public class Fault {
public int FaultCode;
public string FaultString;
public override string ToString() {
return "Fault Code: " + FaultCode.ToString ()
+ ", Fault String: " + FaultString;
}
}
You can use TryCast to map a fault response to this class. Note that, the names of the members here actually match with those returned when a fault occurs (see XML-RPC spec), case is not important though. You can use your own classes to map with struct returned as XML-RPC responses, this should be pretty handy. I will include more details when I actually release the library.
So a more fault tolerant event handler would look something like this:
Fault f = (Fault)response.TryCast (typeof (Fault));
if (f != null) {
Debug.WriteLine ("Fault occured: " + f.ToString ());
if (f.FaultCode == -1) {
Debug.WriteLine ("OMG! -1 ERROR!");
}
}
else {
string result = (string)response.TryCast (typeof (string));
Debug.WriteLine ("Result: " + result);
}
I am still working on a few things with this library, I will post it up here as soon as I am done, I promise!
Stay tuned!
/
Pingback
by Silverlight Travel » XML-RPC for Silverlight
16 Feb 2009 at 15:22
[...] XmlRpcService service = new XmlRpcService (“http://betty.userland.com/RPC2″); XmlRpcRequest req = new XmlRpcRequest (service, “examples.getStateName”, new object [] { 10 }); req.XmlRpcCallCompleteHandler += new XmlRpcCallComplete (req_XmlRpcCallCompleteHandler); req.Execute (null); by verma more here [...]
Pingback
by Displaying geo-referenced Flickr images in Silverlight 4 using Bing Maps
25 Jun 2010 at 08:38
[...] to access their database of public images, in REST (see this article), SOAP (read here) and RPC (here) formats. For more details, go to their API documentation. A detailed explanation of accessing [...]