Binary Serialization and BinaryFormatter with WebServices
2008-03-05 13:27
417 查看
http://www.eggheadcafe.com/articles/20040721.asp http://www.vckbase.com/document/viewdoc/?id=1239
“Web 的语义不是一种分离,而是当前 Web 的延伸,信息在其中被赋予了定义良好的内涵,使计算机和人之间能更好地协同工作。”
[align=right] ——Sir Tim Berners-Lee[/align]
最近我们碰到一些论坛贴子,其问题大概都是围绕着“如何通过 Web 服务(WebService)序列化和发送一幅图像”以及“BinaryFormatter 不 工作——出现编程序集版本异常”这样的话题。我觉得现在是时候用仔细斟酌过的例子代码来说明问题,而不是不断地重复回帖。本文中我创建了一个简单的 Web 服务,其中包含了两个方法:
GetImage —— 它有一个字符串参数,用于接收图像文件名(采用 Server.MapPath),将图像从文件系统读进字节数组,然后 将该图像保存在一个简单的可序列化的类 ImageClas 的 public 类型字节数组属性“myImage”中。这个类就是该方法的返回类型。
GetImageBytes —— 这个方法除了使用 BinaryFormatter 序列化 ImageClass 实例 并将得到的字节数组结果发送给调用者之外,其所做的事情与第一个方法完全一样。
首先把我们的图像存储在一个类实例属性中并序列化完整的类具有显著优点,这样做我们至少除了图象本身之外,我们还可以在网络上序列化和发送 附加的信息。
上述两个 WebMothods 方法的实现细节如下:
我们必须把 ImageClass 类放在一个由 WebServices 类(即调用者)引用的单独类库工程中,,这样我们便可以防止 BinaryFormatter “程序集”错误,因为当我们试图反序列化 Formatter 输出时,程序集名称,版本和文化信息都将会匹配。如果你试图将 ImageClass 类放在实际的 Web 服务项目中,那么将会给你带来本可以避免的令人头痛的问题。解决此种问题的方法同样适用远程接口——即必须把 可序列化的“东西或内容”放到某个单独的类库中。下面是 ImageClass 类的代码 :
为了测试我们创建的 Web 服务,下面拟使用一个 Windows 窗体应用程序,此应用程序将使用一个 WebReference 来引用我们的 Web 服务,有两个按钮,每个按钮执行各自的方法,下面是每个按钮的各自代码:
下面是方法调用后的结果,图片是我信手拿来的一张照片——在服务器上的一张名为“Pete.jpg”的图片:
“Web 的语义不是一种分离,而是当前 Web 的延伸,信息在其中被赋予了定义良好的内涵,使计算机和人之间能更好地协同工作。”
[align=right] ——Sir Tim Berners-Lee[/align]
最近我们碰到一些论坛贴子,其问题大概都是围绕着“如何通过 Web 服务(WebService)序列化和发送一幅图像”以及“BinaryFormatter 不 工作——出现编程序集版本异常”这样的话题。我觉得现在是时候用仔细斟酌过的例子代码来说明问题,而不是不断地重复回帖。本文中我创建了一个简单的 Web 服务,其中包含了两个方法:
GetImage —— 它有一个字符串参数,用于接收图像文件名(采用 Server.MapPath),将图像从文件系统读进字节数组,然后 将该图像保存在一个简单的可序列化的类 ImageClas 的 public 类型字节数组属性“myImage”中。这个类就是该方法的返回类型。
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”的图片:
相关文章推荐
- Binary Serialization and BinaryFormatter with WebServices
- Corporate Portals Empowered with XML and Web Services
- Android: Simplified source code for parsing and working with XML data and web services in Android
- Consuming XML and JSON web services (MyMoviesWithHttpClient)
- Android: Reading, using and working with XML data and web services in Android
- Tuning and Testing Enterprise Web Services with SoapUI and JMeter
- Restarting Web Services and Scheduled Tasks with a Batch File
- Build Secure Web Services With SOAP Headers and Extensions
- BizTalk Exception: Calling web services with Mixed One-Way and Solicit-Response Operations
- Sending files in chunks with MTOM Web Services and .NET 2.0
- 利用webservice和.net技术上传和下载文件--Sending files in chunks with MTOM Web Services and .NET 2.0 from:http://www.codeproject.com/soap/MTO
- XML Programming: Web Applications and Web Services With JSP and ASP
- A simple of Using XML Data Source type and Querying a Web Service with Reporting Services 2008
- Designing Web Services with the J2EE(TM) 1.4 Platform: JAX-RPC, SOAP, and XML Technologies
- Mobile Web Services: Architecture and Implementation
- Test and Analysis of Web Services
- Using Jersey (RESTful Web Services) and Spring (Dependency Injection) Together
- Issues with nova services and ‘timeout’ from Python eventlet w/ CentOS and RHEL
- PHP and Web Services 学习笔记
- How to build Multi-Language Web Sites with ASP.NET 2.0 and VS.Net 2005