使用XML RPC进行远程调用
使用XML RPC进行远程调用
因为最近研究博客系统,对XML RPC的机制有了更深入的一些了解。下面总结一下
1. 什么是XML -RPC?为什么需要它?
有关详细的介绍,你可以参考下面的说明。简单来说,XML-RPC是一个简单的协议和标准,它通过标准的 HTTP请求和响应来实现,数据是以xml格式传递的。它的优点是跨平台。如果仅仅在.NET平台,那么它也许并不是一个最好的选择,而另外一种技术 SOAP Web Service可能更加好一些
http://www.xml-rpc.net/faq/xmlrpcnetfaq.html
1.1 What is XML-RPC?
To quote the XML-RPC.com site:
"It's a spec and a set of implementations that allow software running on disparate operating systems, running in different environments to make procedure calls over the Internet. It's remote procedure calling using HTTP as the transport and XML as the encoding. XML-RPC is designed to be as simple as possible, while allowing complex data structures to be transmitted, processed and returned.";
1.2 What is XML-RPC.NET?
XML-RPC.NET is a .NET class library for implementing XML-RPC clients and servers.
1.3 Why use XML-RPC instead of SOAP?
If your clients and servers are all running in the .NET environment there is no point in using XML-RPC: .NET provides excellent support for SOAP and XML-RPC doesn't have any features not provided by SOAP (other than simplicity).
If you use .NET clients and want to connect to XML-RPC servers running under any OS then XML-RPC.NET is a good choice.
If you want to implement a server in the .NET environment which is to be connected to by clients running in other environments, say Unix or Java, then XML-RPC may be an appropriate choice. SOAP is supported in many different environments but is considerably more complicated than XML-RPC and presents more opportunity for interop problems
我们在这里要感谢Cook这个人,是他将XML-RPC进一步标准化,目前我们大部分人使用的XML-RPC组件,都叫做CookComputing.XmlRpc
这里还有一个介绍(中文的)
http://www.mengyan.org/blog/archives/2005/07/12/30.html
2. 博客系统中的XML RPC
下面有一篇文章很好地介绍了该过程
http://nayyeri.net/blog/implement-metaweblog-api-in-asp-net/
典型的博客客户端都实现了该协议。例如Windows Live Writer等等。下面三个图片就展示了如何在WLW中添加一个博客账户的界面。甚至连Word 2007也实现了该功能。
3. 实现自定义的XML RPC
抛开博客系统,如果我们需要在分布式开发中使用XML-RPC技术,那么该怎么办呢?
3.1 创建接口
using CookComputing.XmlRpc; namespace interfaces { public interface IMath { [XmlRpcMethod("Imath.add")] [return:XmlRpcReturnValue(Description="返回两个值加法的结果")] int Add( [XmlRpcParameter(Description="第一个参数")] int a, [XmlRpcParameter(Description="第二个参数")] int b); } }
3.2 创建服务【需要添加CookComputing这个程序集,以及interfaces这个程序集引用】
using CookComputing.XmlRpc; namespace Services { [XmlRpcService(AutoDocumentation=true,Description="这是我的服务")] public class MathService:XmlRpcService,interfaces.IMath { #region IMath 成员 int interfaces.IMath.Add(int a, int b) { return a + b; } #endregion } }
这里除了实现接口,不要忘记继承XmlRpcService这个类
我们这里简单地把该服务宿主在一个网站里面,那么修改该web.config文件
<httpHandlers> <add verb="*" type="Services.MathService" path="MathService.axd"/> </httpHandlers>
3.3 创建客户端
using System; using CookComputing.XmlRpc; namespace Client { class Program { static void Main(string[] args) { string url = "http://localhost:1500/MathService.axd"; IMath mathservice = XmlRpcProxyGen.Create<IMath>(); mathservice.Url = url; Console.WriteLine(mathservice.Add(100, 200)); Console.Read(); } } public interface IMath:IXmlRpcProxy { [XmlRpcMethod("Imath.add")] [return: XmlRpcReturnValue(Description = "返回两个值加法的结果")] int Add( [XmlRpcParameter(Description = "第一个参数")] int a, [XmlRpcParameter(Description = "第二个参数")] int b); } } 这里有一个关键,就是在客户程序中的那个接口要实现IXmlRpcProxy,否则是无法指定Url的。
这个客户端代理接口中的XmlRpcMethod必须与服务端是对应的