使用XML RPC进行远程调用

来源:互联网 发布:dmsp ols 数据 编辑:程序博客网 时间:2024/05/04 01:48
该篇文章是我于2009年6月10日通过自己编写的工具,批量从位于在博客园的博客站点(http://chenxizhang.cnblogs.com)同步而来。文章中的图片地址仍然是链接到博客园的。特此说明!

陈希章

原文地址:http://www.cnblogs.com/chenxizhang/archive/2008/08/23/1274499.html
原文标题:使用XML RPC进行远程调用
原文发表:2008/8/22 23:44:00

因为最近研究博客系统,对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也实现了该功能。

image image image

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>
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

3.3 创建客户端.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

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();            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必须与服务端是对应的
 
 
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }作者:陈希章
出处:http://blog.csdn.net/chen_xizhang
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原创粉丝点击