Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8200894
  • 博文数量: 1227
  • 博客积分: 10026
  • 博客等级: 上将
  • 技术积分: 20273
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-16 12:40
文章分类

全部博文(1227)

文章存档

2010年(1)

2008年(1226)

我的朋友

分类: C/C++

2008-04-23 21:52:49

用 Web 服务进行二进制序列化和 BinaryFormatter

作者:Peter A. Bromberg, Ph.D.
编译:



原文出自:

  “Web 的语义不是一种分离,而是当前 Web 的延伸,信息在其中被赋予了定义良好的内涵,使计算机和人之间能更好地协同工作。”

——Sir Tim Berners-Lee

  最近我们碰到一些论坛贴子,其问题大概都是围绕着“如何通过 Web 服务(WebService)序列化和发送一幅图像”以及“BinaryFormatter 不 工作——出现编程序集版本异常”这样的话题。我觉得现在是时候用仔细斟酌过的例子代码来说明问题,而不是不断地重复回帖。本文中我创建了一个简单的 Web 服务,其中包含了两个方法:
  1. GetImage —— 它有一个字符串参数,用于接收图像文件名(采用 Server.MapPath),将图像从文件系统读进字节数组,然后 将该图像保存在一个简单的可序列化的类 ImageClas 的 public 类型字节数组属性“myImage”中。这个类就是该方法的返回类型。
  2. GetImageBytes —— 这个方法除了使用 BinaryFormatter 序列化 ImageClass 实例 并将得到的字节数组结果发送给调用者之外,其所做的事情与第一个方法完全一样。

  首先把我们的图像存储在一个类实例属性中并序列化完整的类具有显著优点,这样做我们至少除了图象本身之外,我们还可以在网络上序列化和发送 附加的信息。

上述两个 WebMothods 方法的实现细节如下:

[WebMethod]

public byte[] GetImageBytes(string strImageName)

{

	ImageClass ic=new ImageClass(); 

	FileStream fs = new FileStream(Server.MapPath(strImageName), 

		FileMode.OpenOrCreate, FileAccess.Read);

	

	Byte[] img= new Byte[fs.Length ]; 

	try

	{ 

		fs.Read(img, 0, Convert.ToInt32(fs.Length));

	}

	

	catch(Exception ex)

	{

		Debug.WriteLine(ex.Message ex.StackTrace);

	}

	

	fs.Close(); 

	ic.myImage=img; 

	BinaryFormatter bf = new BinaryFormatter();

	MemoryStream ms = new MemoryStream();

	bf.Serialize(ms,ic);

	return ms.ToArray();

}



[WebMethod]

public ImageClass GetImage(string strImageName)

{

	ImageClass ic=new ImageClass(); 

	FileStream fs = new FileStream(Server.MapPath(strImageName), 

		FileMode.OpenOrCreate, FileAccess.Read);

	

	Byte[] img= new Byte[fs.Length ];

	try

	{

		fs.Read(img, 0, Convert.ToInt32(fs.Length));

	}

	catch(Exception ex)

	{

		Debug.WriteLine(ex.Message ex.StackTrace);

	}

	

	fs.Close(); 

	ic.myImage=img;

	return ic; 

}

  我们必须把 ImageClass 类放在一个由 WebServices 类(即调用者)引用的单独类库工程中,,这样我们便可以防止 BinaryFormatter “程序集”错误,因为当我们试图反序列化 Formatter 输出时,程序集名称,版本和文化信息都将会匹配。如果你试图将 ImageClass 类放在实际的 Web 服务项目中,那么将会给你带来本可以避免的令人头痛的问题。解决此种问题的方法同样适用远程接口——即必须把 可序列化的“东西或内容”放到某个单独的类库中。下面是 ImageClass 类的代码 :
using System;

using System.Reflection;

using System.Drawing;



namespace ImageClassAssy

{

	[Serializable]

	public class ImageClass

	{

		public byte[] myImage;

		public ImageClass()

		{ 

		}

	}

}

  为了测试我们创建的 Web 服务,下面拟使用一个 Windows 窗体应用程序,此应用程序将使用一个 WebReference 来引用我们的 Web 服务,有两个按钮,每个按钮执行各自的方法,下面是每个按钮的各自代码:
private void button1_Click(object sender, System.EventArgs e)

{

	// GetImage Method

	BinaryFormatterSvc.Service1 s= new BinaryFormatterSvc.Service1(); 

	BinaryFormatterSvc.ImageClass icc=s.GetImage(this.textBox1.Text); 

	byte[] byt=icc.myImage;

	MemoryStream ms = new MemoryStream(byt);

	Bitmap b=(Bitmap) Image.FromStream(ms);

	pictureBox1.Image=b; 

}



private void button2_Click(object sender, System.EventArgs e)

{

	//GetImageBytes (BinaryFormatter) method

	BinaryFormatterSvc.Service1 s= new BinaryFormatterSvc.Service1(); 

	byte[] byt=s.GetImageBytes(this.textBox1.Text);

	MemoryStream ms = new MemoryStream(byt);

	BinaryFormatter bf= new BinaryFormatter();

	ImageClass ic=(ImageClass)bf.Deserialize(ms);

	MemoryStream ms2 = new MemoryStream(ic.myImage);

	Bitmap b=(Bitmap) Image.FromStream(ms2); 

	pictureBox1.Image=b;

} 

  下面是方法调用后的结果,图片是我信手拿来的一张照片——在服务器上的一张名为“Pete.jpg”的图片:



  顺便提醒一下,不要忘记在我们的 Windows 窗体应用程序中添加对 ImageClass 程序集的引用,以便让我们的客户端知道怎样反序列化它所接收的类型,以及 将所接收到的类型反序列化成什么,以及还原出图像用于显示。

翻译:侯勇
地址:曲阜师范大学日照分校计算机系
邮编:276800
电话:0633-8711769
E-mail:aspnetcs@eyou.com
阅读(279) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~