﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-ChengKing-文章分类-分布式编程</title><link>http://www.cnitblog.com/ChengKing/category/1237.html</link><description>ChengKing</description><language>zh-cn</language><lastBuildDate>Sun, 02 Oct 2011 12:39:12 GMT</lastBuildDate><pubDate>Sun, 02 Oct 2011 12:39:12 GMT</pubDate><ttl>60</ttl><item><title>分布式编程-&gt;Remoting的一个代码示例(借助Remoting实现发送信息功能) </title><link>http://www.cnitblog.com/ChengKing/articles/4014.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:49:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/4014.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/4014.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/4014.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/4014.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/4014.html</trackback:ping><description><![CDATA[<P>(一).说明<BR>&nbsp;&nbsp;&nbsp; 一个远程调用示例. <BR>&nbsp;&nbsp;&nbsp; 此示例实现功能:&nbsp; 客房端调用远程方法（远程方法可以弹&nbsp;&nbsp;&nbsp; 出自定义信息），实现发送信息功能. <BR>&nbsp;&nbsp;&nbsp; 实现原理概是这样的：客户端不能直接调用远程对象，它必须先通过信道请求服务端宿主程序,当收到客户端请求时，<BR>&nbsp;&nbsp;&nbsp; .net远程处理框架会在宿主组件的应用程序域中生成所需要的远程对象. 并执行远程对象中的方法.&nbsp;&nbsp;&nbsp;&nbsp; <BR>(二).实现方案<BR>&nbsp; 在之前先介绍几种类:<BR>&nbsp;&nbsp;&nbsp; 1.可序列化的类：&nbsp; 以&lt;serializable&gt;属性为标记，可以在进程/应用程序/计算机之间传送.<BR>&nbsp;&nbsp;&nbsp; 2.可远程调用的类: 直接或间接地继承 System.MarshalByRefObject类，可以被远程激活.<BR>&nbsp;&nbsp;&nbsp; 3.一般类:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 不能构建分布式，用于本地调用.<BR>&nbsp; 1.首先建立三个项目: <BR>&nbsp;&nbsp;&nbsp; RemoteObject: 提供远程对象，供客户端调用&nbsp; <BR>&nbsp;&nbsp;&nbsp; SimpleClient: 用于向服务端程序发出请求，调用远程对象 (winform)<BR>&nbsp;&nbsp;&nbsp; SimpleServer: 侦听客户端请求，并创建对象&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (winform) <BR>&nbsp; 2.在RemoteObject项目下面建立远程调用类: RemoteObject.cs<BR>&nbsp;&nbsp;&nbsp; 在SimpleClient项目下面建立: Form1.cs和SimpleClient.exe.config配置文件。 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 其中配置文件的作用是指定服务端地址和信道等信息，下面的代码里面有详细说明.<BR>&nbsp;&nbsp;&nbsp; 在SimpleServer项目下面建立: Form1.cs和SimpleServer.exe.config配置文件。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 其中配置文件的作用是指定接受请求客户端的地址和信道等信息，下面的代码里面有详细说明.&nbsp; <BR>(三).<BR>&nbsp; 各文件源代码:<BR>&nbsp; 1.RemoteObject.cs<BR>&nbsp;&nbsp;&nbsp; using System;<BR>&nbsp;&nbsp;&nbsp; using System.Windows.Forms;<BR>&nbsp;&nbsp;&nbsp; namespace RemoteObjects<BR>&nbsp;&nbsp;&nbsp; {&nbsp;<BR>&nbsp;public class RemoteObject : System.MarshalByRefObject&nbsp; //继承此类才能被远程激活调用<BR>&nbsp;{<BR>&nbsp;&nbsp;public RemoteObject()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;//远程调用方法,功能： 弹出自定义消息, 参数: str是从客户端传递过来的<BR>&nbsp;&nbsp;public static void Method(string str)<BR>&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MessageBox.Show(str);&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}&nbsp;&nbsp;<BR>&nbsp;}<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; 2.客户端工程SimpleClient项目中的Form1.cs:<BR>&nbsp;&nbsp;&nbsp; using System;<BR>&nbsp;&nbsp;&nbsp; using System.Drawing;<BR>&nbsp;&nbsp;&nbsp; using System.Collections;<BR>&nbsp;&nbsp;&nbsp; using System.ComponentModel;<BR>&nbsp;&nbsp;&nbsp; using System.Windows.Forms;<BR>&nbsp;&nbsp;&nbsp; using System.Data;<BR>&nbsp;&nbsp;&nbsp; using RemoteObjects;<BR>&nbsp;&nbsp;&nbsp; namespace SimpleClient<BR>&nbsp;&nbsp;&nbsp; {&nbsp;<BR>&nbsp;public class Form1 : System.Windows.Forms.Form<BR>&nbsp;{<BR>&nbsp;&nbsp;private System.Windows.Forms.TextBox textBox1;<BR>&nbsp;&nbsp;private System.Windows.Forms.Label label1;<BR>&nbsp;&nbsp;private System.Windows.Forms.Button button1;&nbsp;&nbsp;<BR>&nbsp;&nbsp;private System.ComponentModel.Container components = null;</P>
<P>&nbsp;&nbsp;public Form1()<BR>&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;InitializeComponent();<BR>&nbsp;&nbsp;}&nbsp;&nbsp;<BR>&nbsp;&nbsp;protected override void Dispose( bool disposing )<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if( disposing )<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (components != null) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;components.Dispose();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;base.Dispose( disposing );<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;#region Windows 窗体设计器生成的代码<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR>&nbsp;&nbsp;/// 此方法的内容。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void InitializeComponent()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;this.textBox1 = new System.Windows.Forms.TextBox();<BR>&nbsp;&nbsp;&nbsp;this.label1 = new System.Windows.Forms.Label();<BR>&nbsp;&nbsp;&nbsp;this.button1 = new System.Windows.Forms.Button();<BR>&nbsp;&nbsp;&nbsp;this.SuspendLayout();<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// textBox1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.textBox1.Location = new System.Drawing.Point(40, 88);<BR>&nbsp;&nbsp;&nbsp;this.textBox1.Multiline = true;<BR>&nbsp;&nbsp;&nbsp;this.textBox1.Name = "textBox1";<BR>&nbsp;&nbsp;&nbsp;this.textBox1.Size = new System.Drawing.Size(336, 176);<BR>&nbsp;&nbsp;&nbsp;this.textBox1.TabIndex = 0;<BR>&nbsp;&nbsp;&nbsp;this.textBox1.Text = "";<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// label1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.label1.Location = new System.Drawing.Point(40, 32);<BR>&nbsp;&nbsp;&nbsp;this.label1.Name = "label1";<BR>&nbsp;&nbsp;&nbsp;this.label1.TabIndex = 1;<BR>&nbsp;&nbsp;&nbsp;this.label1.Text = "请输入信息:";<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// button1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.button1.Location = new System.Drawing.Point(296, 32);<BR>&nbsp;&nbsp;&nbsp;this.button1.Name = "button1";<BR>&nbsp;&nbsp;&nbsp;this.button1.TabIndex = 2;<BR>&nbsp;&nbsp;&nbsp;this.button1.Text = "发送";<BR>&nbsp;&nbsp;&nbsp;this.button1.Click += new System.EventHandler(this.button1_Click);<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// Form1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<BR>&nbsp;&nbsp;&nbsp;this.ClientSize = new System.Drawing.Size(416, 302);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.button1);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.label1);<BR>&nbsp;&nbsp;&nbsp;this.Controls.Add(this.textBox1);<BR>&nbsp;&nbsp;&nbsp;this.Name = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.Text = "发送信息";<BR>&nbsp;&nbsp;&nbsp;this.Load += new System.EventHandler(this.Form1_Load);<BR>&nbsp;&nbsp;&nbsp;this.ResumeLayout(false);</P>
<P>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;#endregion<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;[STAThread]<BR>&nbsp;&nbsp;static void Main() <BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Application.Run(new Form1());&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;private void Form1_Load(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //载入信道，用于侦听客户端请求，配置文件记载客户端地址等信息.<BR>&nbsp;&nbsp;&nbsp;System.Runtime.Remoting.RemotingConfiguration.Configure("Simpleclient.exe.config");&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void button1_Click(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//开始调用远程对象(发送信息)<BR>&nbsp;&nbsp;&nbsp;RemoteObjects.RemoteObject.Method(this.textBox1.Text);<BR>&nbsp;&nbsp;}<BR>&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }<BR>3.客户端工程SimpleClient项目中的SimpleClient.exe.config:<BR>&nbsp;&nbsp; &lt;?xml version="1.0" encoding="utf-8" ?&gt;<BR>&nbsp;&nbsp; &lt;configuration&gt;<BR>&nbsp;&lt;system.runtime.remoting&gt;<BR>&nbsp;&nbsp;&lt;application name="simpleclient"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;!-- 指定请求的服务端地址和端口号--&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;!-- url中的:localhost是测试的本机，可以使用Intenet上的其它机器名或ip地址--&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;client url="tcp://localhost:8080/simpleserver"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;activated type="RemoteObjects.RemoteObject,RemoteObjects"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/activated&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/client&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;channels&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- 指定信道,有两种信道可选:Tcp(基于TCP协议)和Http(无连续连接协议)信道--&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;channel ref="tcp client"/&gt;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&lt;/channels&gt;<BR>&nbsp;&nbsp;&lt;/application&gt;<BR>&nbsp;&lt;/system.runtime.remoting&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;/configuration&gt;<BR>4.服务端工程SimpleServer项目中的Form1.cs:<BR>&nbsp;&nbsp; using System;<BR>&nbsp;&nbsp; using System.Drawing;<BR>&nbsp;&nbsp; using System.Collections;<BR>&nbsp;&nbsp; using System.ComponentModel;<BR>&nbsp;&nbsp; using System.Windows.Forms;<BR>&nbsp;&nbsp; using System.Data;<BR>&nbsp;&nbsp; using System.Runtime.Remoting;</P>
<P>&nbsp;&nbsp; namespace SimpleServer<BR>&nbsp;&nbsp; {<BR>&nbsp;/// &lt;summary&gt;<BR>&nbsp;/// Form1 的摘要说明。<BR>&nbsp;/// &lt;/summary&gt;<BR>&nbsp;public class Form1 : System.Windows.Forms.Form<BR>&nbsp;{<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 必需的设计器变量。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private System.ComponentModel.Container components = null;</P>
<P>&nbsp;&nbsp;public Form1()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// Windows 窗体设计器支持所必需的<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;InitializeComponent();</P>
<P>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;&nbsp;// TODO: 在 InitializeComponent 调用后添加任何构造函数代码<BR>&nbsp;&nbsp;&nbsp;//<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 清理所有正在使用的资源。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;protected override void Dispose( bool disposing )<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if( disposing )<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (components != null) <BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;components.Dispose();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;base.Dispose( disposing );<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;#region Windows 窗体设计器生成的代码<BR>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 设计器支持所需的方法 - 不要使用代码编辑器修改<BR>&nbsp;&nbsp;/// 此方法的内容。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;private void InitializeComponent()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;// Form1<BR>&nbsp;&nbsp;&nbsp;// <BR>&nbsp;&nbsp;&nbsp;this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<BR>&nbsp;&nbsp;&nbsp;this.ClientSize = new System.Drawing.Size(292, 266);<BR>&nbsp;&nbsp;&nbsp;this.Name = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.Text = "Form1";<BR>&nbsp;&nbsp;&nbsp;this.Load += new System.EventHandler(this.Form1_Load);</P>
<P>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;#endregion</P>
<P>&nbsp;&nbsp;/// &lt;summary&gt;<BR>&nbsp;&nbsp;/// 应用程序的主入口点。<BR>&nbsp;&nbsp;/// &lt;/summary&gt;<BR>&nbsp;&nbsp;[STAThread]<BR>&nbsp;&nbsp;static void Main() <BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;//Application.Run(new Form1());&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;Run();<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private static void Run()<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;System.Runtime.Remoting.RemotingConfiguration.Configure("SimpleServer.exe.config");<BR>&nbsp;&nbsp;}</P>
<P>&nbsp;&nbsp;private void Form1_Load(object sender, System.EventArgs e)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }<BR>5.服务端工程SimpleServer项目中的SimpleServer.exe.config:<BR>&nbsp; &lt;?xml version="1.0" encoding="utf-8" ?&gt;<BR>&nbsp; &lt;configuration&gt;<BR>&nbsp;&lt;system.runtime.remoting&gt;<BR>&nbsp;&nbsp;&lt;application name="simpleserver"&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;service&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;activated type="RemoteObjects.RemoteObject,RemoteObjects"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/activated&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/service&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;channels&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&lt;channel ref="tcp server" port="8080" /&gt;<BR>&nbsp;&nbsp;&nbsp;&lt;/channels&gt;<BR>&nbsp;&nbsp;&lt;/application&gt;<BR>&nbsp;&lt;/system.runtime.remoting&gt;<BR>&nbsp; &lt;/configuration&gt;<BR>(四).注意点<BR>&nbsp;&nbsp; 1.由于配置文件默认是添加在根目录下的，要把两个配置文件拷贝到:根目录/bin/debug下面,要让它与执行文件在同一个目录下面。<BR>&nbsp;&nbsp; 2.右击RemoteObject项目，选“常规”下的，输入类型为：“类库”.&nbsp; 它默认为应用程序，这里作为类库使用.&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp; 设置好后，按: Ctrl+Shift+B生成类库Dll.<BR>&nbsp;&nbsp; 3.在工程SimpleServer和SimpleClient中分别添加引用： 即将RemoteObject项目刚生成的DLL: RemoteObjects.dll添加到各自的工程中.<BR>&nbsp;&nbsp;&nbsp;&nbsp; 具体方法：右击“引用“-&gt;”添加引用“-&gt;"浏览"，找到生成的RemoteObjects.dll分别添加进来.<BR>&nbsp;&nbsp; 4.右击解决方案，选择“属性”-&gt; “选中多启动项目单选框”-&gt;"选SimpleServer和SimpleClient同时启动".<BR>&nbsp;&nbsp;&nbsp;&nbsp; 因为： 当服务端宿主程序运行时，客户端才能正确调用远程对象.<BR>&nbsp;&nbsp; 5.按F5运行.&nbsp; 输入信息，点“发送”按钮，就可以调用远程对象了.<BR>(五).扩展<BR>&nbsp;&nbsp;&nbsp;&nbsp; 可以修改客户端配置文件:&lt;client url="tcp://localhost:8080/simpleserver"&gt;&nbsp; 中的localhost为其它的机器名称，只要另一台<BR>&nbsp;&nbsp;&nbsp;&nbsp; 机器运行了宿主程序,并处于运行状态. 那么当调用时，会在服务端弹出信息，即实现了发送信息功能.</P>
<P></P>
<P>以上代码已经测试，不正确的地方望批评指正!</P><img src ="http://www.cnitblog.com/ChengKing/aggbug/4014.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:49 <a href="http://www.cnitblog.com/ChengKing/articles/4014.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>分布式编程-&gt;XML Webservice实现原理及相关知识</title><link>http://www.cnitblog.com/ChengKing/articles/4015.html</link><dc:creator>ZhengJian</dc:creator><author>ZhengJian</author><pubDate>Sun, 06 Nov 2005 04:49:00 GMT</pubDate><guid>http://www.cnitblog.com/ChengKing/articles/4015.html</guid><wfw:comment>http://www.cnitblog.com/ChengKing/comments/4015.html</wfw:comment><comments>http://www.cnitblog.com/ChengKing/articles/4015.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ChengKing/comments/commentRss/4015.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ChengKing/services/trackbacks/4015.html</trackback:ping><description><![CDATA[<P>好多书籍只介绍创建XML Webservice，并没有详细介绍XML WebService的工作原理以及各部分(例如IIS Asp.net)<BR>在XML WebService中所起的作用.<BR>学习后整理了一下,希望对读者有所帮助!</P>
<P>(一).XML WebService作用<BR>&nbsp; XML WebService在应用程序中所起的作用与.Net远程调用处理组件相同. <BR>&nbsp; 用户不能直接使用WebService,只能通过Asp.net Web应用程序或Windows桌面<BR>&nbsp; 客户端来调用.<BR>(二).XML WebService与.Net远程处理区别<BR>&nbsp; 1. XML WebService比.Net远程处理对象所受的限制更多。它类似于.Net远程处理<BR>&nbsp;&nbsp;&nbsp;&nbsp; 的单独调用对象的工作机制。 不能创建一个单独的或是由客户端激活的对象.<BR>&nbsp; 2.XML WebService的创建和设计比远程组件更容易/简单.<BR>&nbsp; 3.Net远程处理二进制通信要比XML WebService SOAP格式通信要快捷.<BR>&nbsp; 4.XML WebService较.Net远程处理扩展性强。 它支持以跨平台使用为目的的开放标准.<BR>&nbsp; 5.XML WebService不需要专门的宿主程序，而是由Asp.net承载。 可以访问一些重要的<BR>&nbsp;&nbsp;&nbsp; 平台服务，如：数据缓存/网络会话状态管理/身份验证/全局共享应用程序集合等。而.Net<BR>&nbsp;&nbsp;&nbsp; 远程处理则很难实现这些功能.<BR>&nbsp; 6.XML WebService运行在IIS和ASP.NET之上，使用http信道(80端口)与客户通信。<BR>&nbsp;&nbsp;&nbsp; 可以自由跨越防火墙.<BR>(三).XML WebService创建与调用过程<BR>&nbsp; I.服务端创建<BR>&nbsp;&nbsp;&nbsp;&nbsp; 1.使用IIS，在Web服务器上新建一个虚拟目录来存放XML Web服务.<BR>&nbsp;&nbsp;&nbsp;&nbsp; 2.建立XML WebService类，使用[WebMethod]属性来标记方法可以被远程调用.<BR>&nbsp;&nbsp;&nbsp;&nbsp; 3.在虚拟目录中部署XML Web服务的文件.<BR>&nbsp; II.客户端使用<BR>&nbsp;&nbsp;&nbsp;&nbsp; 1.客户端通过URL或文件查询或UDDI注册，发现XML WebService<BR>&nbsp;&nbsp;&nbsp;&nbsp; 2.客户端请求描述XML WebService的WSDL文档。<BR>&nbsp;&nbsp;&nbsp;&nbsp; 3.客户端在WSDL文档的基础上生成一个代理类。<BR>&nbsp;&nbsp;&nbsp;&nbsp; 4.客户端生成代理类的实例，并调用XML Webservice，发送消息并接受处理后结果.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 也就是说调用XML WebService是由客户端生成的代理类实例对象完成的.<BR>(四).IIS作用<BR>&nbsp; 1.IIS通过虚拟目录提供对Web服务器进行访问。简单的说： 就是将"c:\MyWeb"映射<BR>&nbsp;&nbsp;&nbsp; 一个URL地址形式的虚拟目录:"<A href="http://192.168.83.66/MyWeb">http://192.168.83.66/MyWeb</A>",供本机或Internet<BR>&nbsp;&nbsp;&nbsp; 上计算机访问Webservice.<BR>&nbsp; 2.虚拟目录的权限与普通目录不同。根据默认设置，不允许远程用户浏览虚拟目录，运行<BR>&nbsp;&nbsp;&nbsp; 可执行文件，新建文件和下载某些文件类型文件。可以根据需要自定义IIS虚拟目录权限设置.<BR>&nbsp; 3.IIS对Internet进行公开处理. IIS并不负责运行Asp或Asp.net布面 或XML Webservice,而是<BR>&nbsp;&nbsp;&nbsp; 维护一个注册的文件扩展名列表。如果IIS收到对某一种文件类型的请求，就把工作提交给<BR>&nbsp;&nbsp;&nbsp; Asp.net工作进程，由Asp.net工作进程处理剩下的工作.<BR>(五).XML WebService和SOAP标准支持的数据类型<BR>&nbsp; 不知道读者有没有遇到这种情况，在调用WebService并给一个方法传递了一个DataRow参数时，运行<BR>&nbsp; 时会抛出异常： "没法将参数序列化！"，如果把DataRow加入到DataSet中，并将DataSet作为参数<BR>&nbsp; 传递再运行就OK了。 这是因为：XML WebService只能对数据集DataSet对象类型进行XML序列化，<BR>&nbsp; 不能对DataRow对象类型进行XML序列化造成的错误.&nbsp; 所以了解一下XML WebService支持序列化的基<BR>&nbsp; 本数据类型是比较重要的.它支持的数据类型如下:<BR>&nbsp; 1.基本数据类型.&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 标准类型，如：int float bool DateTime string等基本数据类型<BR>&nbsp; 2.枚举.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 支持枚举Enum定义的类型<BR>&nbsp; 3.自定义对象.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 可以传递任意基于自定义类或结构创建的对象。 但要注意一点： 它只能传输数据成员(变量和属性).<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果定义了方法，则方法不能进行序列化传输,序列化后只剩下数据成员.<BR>&nbsp; 4.DataSet对象<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 支持DataSet,切记：不支持DataTable和DataRow,DataSet已经是XML Webservice能够支持的最小的可序列化对象.<BR>&nbsp; 5.XmlNode对象<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 基于XmlNode的对象可以表示XML文档的一部分.<BR>&nbsp; 6.数组和集合<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 可以使用任何被支持的类型的数组和简单集合，包括: DataSet对象/XmlNode对象和自定义对象.</P>
<P>另外，关于创建XML WebService实例代码示例，几乎任何一本涉及到WebService的书籍都会讲的。而且创建一个<BR>WebService非常简单. 这里就不多说了. </P>
<P><BR>哦,已经: 00:43点了，该睡觉了.&nbsp; :)<BR>&nbsp; <BR>&nbsp; <BR>&nbsp; </P><img src ="http://www.cnitblog.com/ChengKing/aggbug/4015.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ChengKing/" target="_blank">ZhengJian</a> 2005-11-06 12:49 <a href="http://www.cnitblog.com/ChengKing/articles/4015.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>