加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Binary Serialization and BinaryFormatter with WebServices

发布时间:2020-12-17 02:44:48 所属栏目:安全 来源:网络整理
导读:?http://www.eggheadcafe.com/articles/20040721.asp http://www.vckbase.com/document/viewdoc/?id=1239 “Web 的语义不是一种分离,而是当前 Web 的延伸,信息在其中被赋予了定义良好的内涵,使计算机和人之间能更好地协同工作。” ——Sir Tim Berners-Le
?http://www.eggheadcafe.com/articles/20040721.asp
http://www.vckbase.com/document/viewdoc/?id=1239

  “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,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),FileAccess.Read);

Byte[] img= new Byte[fs.Length ];
try
{
fs.Read(img,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”的图片:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读