看了不少同行博客里关于Remoting文章。有很多很优秀的文章,Bruce Zhang's Blog(http://www.cnblogs.com/wayfarer/category/1235.html)是很好的学习资源
Remoting 的词根---Remote 即Remote Object ,分布式对象.这样我们可以在服务器应用程序上通过网络调用方法,而不是发送数据,这是Web Service与Remoting技术的目标,个人觉得Web Serivce主要用于在不同平台之间的通信,如Java与.Net平台之间的互操作,Remoting 在.Net平台的分布式操作具有性能优势.同时是用于对等通信的一种好技术
Remoting 使用的技术
1,XML
2,SOAP(简单对象传输协议)
3,序列化(添加[Serializable]或实现ISerializable接口)
先看下对象的序列化吧,此Demo来之视频教程
先建立一个可序列化的对象:(添加[Serializable]属性或实现ISerializable接口)
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace SerializableDemo
6{
7 [Serializable]
8 public class Sumof
9 {
10 public Sumof()
11 {
12
13 }
14 public DecimalList decimalList = new DecimalList();
15
16 public decimal sum, avg;
17
18 public void Calculate()
19 {
20
21 this.sum = 0;
22 foreach (decimal m in decimalList)
23 {
24 sum += m;
25 }
26 this.avg = sum / decimalList.Count;
27
28 }
29
30 }
31
32 [Serializable]
33 public class DecimalList : List<decimal>
34 {
35 }
36}
37
2using System.Collections.Generic;
3using System.Text;
4
5namespace SerializableDemo
6{
7 [Serializable]
8 public class Sumof
9 {
10 public Sumof()
11 {
12
13 }
14 public DecimalList decimalList = new DecimalList();
15
16 public decimal sum, avg;
17
18 public void Calculate()
19 {
20
21 this.sum = 0;
22 foreach (decimal m in decimalList)
23 {
24 sum += m;
25 }
26 this.avg = sum / decimalList.Count;
27
28 }
29
30 }
31
32 [Serializable]
33 public class DecimalList : List<decimal>
34 {
35 }
36}
37
主窗体:
序列化为二进制文件:
1private void button2_Click(object sender, EventArgs e)
2 {
3 FileStream fileStream = new FileStream("DoSum.bin",FileMode.Create);
4 BinaryFormatter binaryFormatter = new BinaryFormatter();//二进制格式化器
5 binaryFormatter.Serialize(fileStream,sobj);
6 fileStream.Close();
7
8 }
Soap与Xml2 {
3 FileStream fileStream = new FileStream("DoSum.bin",FileMode.Create);
4 BinaryFormatter binaryFormatter = new BinaryFormatter();//二进制格式化器
5 binaryFormatter.Serialize(fileStream,sobj);
6 fileStream.Close();
7
8 }
1 private void button3_Click(object sender, EventArgs e)
2 {
3 FileStream fileStream = new FileStream("DoSum_Soap.xml", FileMode.Create);
4 SoapFormatter Formatter = new SoapFormatter();//SOAP格式化器
5 Formatter.Serialize(fileStream, sobj);
6 fileStream.Close();
7
8 }
9
10 private void button4_Click(object sender, EventArgs e)
11 {
12 FileStream fileStream = new FileStream("DoSum.xml", FileMode.Create);
13 XmlSerializer Formatter = new XmlSerializer(typeof(Sumof));
14 Formatter.Serialize(fileStream, sobj);
15 fileStream.Close();
16 }
2 {
3 FileStream fileStream = new FileStream("DoSum_Soap.xml", FileMode.Create);
4 SoapFormatter Formatter = new SoapFormatter();//SOAP格式化器
5 Formatter.Serialize(fileStream, sobj);
6 fileStream.Close();
7
8 }
9
10 private void button4_Click(object sender, EventArgs e)
11 {
12 FileStream fileStream = new FileStream("DoSum.xml", FileMode.Create);
13 XmlSerializer Formatter = new XmlSerializer(typeof(Sumof));
14 Formatter.Serialize(fileStream, sobj);
15 fileStream.Close();
16 }
运行程序,先生成对象,再序列化为所要的格式,由于没有指定位置,生成的文件将保存在应用程序EXE文件所在目录,其中序列化为Soap与Xml格式所生成的文件如下:
Soap:
1<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
2<SOAP-ENV:Body>
3<a1:Sumof id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SerializableDemo/SerializableDemo%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
4<decimalList href="#ref-3"/>
5<sum>55</sum>
6<avg>5</avg>
7</a1:Sumof>
8<a1:DecimalList id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SerializableDemo/SerializableDemo%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
9<List_x0060_1_x002B__items href="#ref-4"/>
10<List_x0060_1_x002B__size>11</List_x0060_1_x002B__size>
11<List_x0060_1_x002B__version>11</List_x0060_1_x002B__version>
12</a1:DecimalList>
13<SOAP-ENC:Array id="ref-4" SOAP-ENC:arrayType="xsd:decimal[16]">
14<item>0</item>
15<item>1</item>
16<item>2</item>
17<item>3</item>
18<item>4</item>
19<item>5</item>
20<item>6</item>
21<item>7</item>
22<item>8</item>
23<item>9</item>
24<item>10</item>
25<item>0</item>
26<item>0</item>
27<item>0</item>
28<item>0</item>
29<item>0</item>
30</SOAP-ENC:Array>
31</SOAP-ENV:Body>
32</SOAP-ENV:Envelope>
33
2<SOAP-ENV:Body>
3<a1:Sumof id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SerializableDemo/SerializableDemo%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
4<decimalList href="#ref-3"/>
5<sum>55</sum>
6<avg>5</avg>
7</a1:Sumof>
8<a1:DecimalList id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SerializableDemo/SerializableDemo%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
9<List_x0060_1_x002B__items href="#ref-4"/>
10<List_x0060_1_x002B__size>11</List_x0060_1_x002B__size>
11<List_x0060_1_x002B__version>11</List_x0060_1_x002B__version>
12</a1:DecimalList>
13<SOAP-ENC:Array id="ref-4" SOAP-ENC:arrayType="xsd:decimal[16]">
14<item>0</item>
15<item>1</item>
16<item>2</item>
17<item>3</item>
18<item>4</item>
19<item>5</item>
20<item>6</item>
21<item>7</item>
22<item>8</item>
23<item>9</item>
24<item>10</item>
25<item>0</item>
26<item>0</item>
27<item>0</item>
28<item>0</item>
29<item>0</item>
30</SOAP-ENC:Array>
31</SOAP-ENV:Body>
32</SOAP-ENV:Envelope>
33
xml
1<?xml version="1.0"?>
2<Sumof xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3 <decimalList>
4 <decimal>0</decimal>
5 <decimal>1</decimal>
6 <decimal>2</decimal>
7 <decimal>3</decimal>
8 <decimal>4</decimal>
9 <decimal>5</decimal>
10 <decimal>6</decimal>
11 <decimal>7</decimal>
12 <decimal>8</decimal>
13 <decimal>9</decimal>
14 <decimal>10</decimal>
15 </decimalList>
16 <sum>55</sum>
17 <avg>5</avg>
18</Sumof>
2<Sumof xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3 <decimalList>
4 <decimal>0</decimal>
5 <decimal>1</decimal>
6 <decimal>2</decimal>
7 <decimal>3</decimal>
8 <decimal>4</decimal>
9 <decimal>5</decimal>
10 <decimal>6</decimal>
11 <decimal>7</decimal>
12 <decimal>8</decimal>
13 <decimal>9</decimal>
14 <decimal>10</decimal>
15 </decimalList>
16 <sum>55</sum>
17 <avg>5</avg>
18</Sumof>
此例子的完整Demo下载:
/Files/mshwu/SerializableDemo.rar