Binary Serialization and BinaryFormatter with WebServices

原创 2004年09月19日 09:54:00

"The Semantic Web is not a separate Web but an extension of the current one, in which information is given well-defined meaning, better enabling computers and people to work in cooperation." -- Sir Tim Berners-Lee

Recently we have had some forum posts with questions that generally seem to revolve around the topic of "How do I serialize and send an image over a WebService" and "BinaryFormatter doesn't work - gives Assembly Version exception".


New Articles & Tips
ASP.NET 1.1 SQLCacheDependency With SQLite
Build a C# SQL Server Database Export Utility
SQL Server Reporting Services - Lessons Learned
Dr. Dotnetsky's Cool .NET Tips and Tricks # 18
.NET Compact Framework Save Signature To File
HOWTO: Register an Assembly in the GAC
.NET Framework 1.1 SP1 - Issues
Circular References / Memory Leaks /other baddies
Rsources Section Now Open for Testing!
.NET Compact Framework App.Config Workaround
<SCRIPT type=text/javascript><!-- google_ad_client = "pub-8682474657542641"; google_alternate_ad_url = "http://www.eggheadcafe.com/alternateads.asp"; google_ad_width = 300; google_ad_height = 250; google_ad_format = "300x250_as"; google_ad_channel ="0679176943"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "6D99CA"; google_color_url = "008000"; google_color_text = "000000"; //--></SCRIPT> <SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </SCRIPT> <IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-8682474657542641&amp;dt=1095612817373&amp;lmt=1095612817&amp;alternate_ad_url=http%3A%2F%2Fwww.eggheadcafe.com%2Falternateads.asp&amp;format=300x250_as&amp;output=html&amp;channel=0679176943&amp;url=http%3A%2F%2Fwww.eggheadcafe.com%2Farticles%2F20040721.asp&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=6D99CA&amp;color_url=008000&amp;color_border=FFFFFF&amp;ref=http%3A%2F%2Fwww.asp.net%2FDefault.aspx%3Ftabindex%3D0%26tabid%3D1&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=-420&amp;u_his=1&amp;u_java=true" frameBorder=0 width=300 scrolling=no height=250 allowTransparency><img height="1" width="1" border="0" src="http://pagead2.googlesyndication.com/pagead/imp.gif?client=ca-pub-8682474657542641&dt=1095612817373&lmt=1095612817&alternate_ad_url=http%3A%2F%2Fwww.eggheadcafe.com%2Falternateads.asp&format=300x250_as&output=html&channel=0679176943&url=http%3A%2F%2Fwww.eggheadcafe.com%2Farticles%2F20040721.asp&color_bg=FFFFFF&color_text=000000&color_link=6D99CA&color_url=008000&color_border=FFFFFF&ref=http%3A%2F%2Fwww.asp.net%2FDefault.aspx%3Ftabindex%3D0%26tabid%3D1&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=-420&u_his=1&u_java=true&event=noiframe" /></IFRAME>

Rather than launch into a repetitious tirade of post-answering, I thought it might be a good time to examine this with some carefully considered sample code. What I've done here is to create a simple WebService that has two methods -

1) GetImage - Accepts a string for the image fileName (assumes Server.MapPath), reads the image off the file system into a Byte array, and then sticks it into the public byte[] "myImage" property of a simple serializable class, "ImageClass". This class is sent in its entirety as the return type from the method.

2) GetImageBytes - This method does exactly what the first method does, except that it use the BinaryFormatter to serialize the ImageClass instance , and sends the resultant byte array to the caller.

There are significant advantages to first storing our image in a property of a class instance and serializing the entire class, not the least of which is that we can have additional information besides "just the image" getting serialized and sent over the wire.

The method details of these two WebMethods follow:

The ImageClass needs to be put into a separate class library project that is referenced from the WebService class (as well as by the caller). This is how we prevent BinaryFormatter "Assembly" errors, because when we attempt to deserialize the output of the Formatter, the Assembly name, version and cultureInfo will all match. If you attempt to put your ImageClass class into the actual WebService project, this is what will cause you unnecessary headaches. The same goes for interfaces with remoting - need to get that serializable "thing" into a separate class library. So now here is the code for the ImageClass:

Now for our test harness, we will use a Windows Forms app that uses a WebReference to our service, and has two buttons - one to exercise each method. Here is the code for each button, respectively:

The result of our method call, with my handy "Pete.jpg" image sitting on the server, would look like so:

BFService.jpg

And, just in case you missed my first point, let's not forget to add a reference to our ImageClass assembly in our Winforms app, so that our client will know how to and "into what" to deserialize the type it has received and get out the image property for display.

download the Visual Studio 2003 Solution that accompanies this article

C# 二进制BinaryFormatter进行序列化与反序列化

序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制。其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方。 .NET框架提供了两种种串行化的方式:1...
  • e295166319
  • e295166319
  • 2016年10月11日 17:41
  • 5960

二值网络(Binarized Neural Networks)学习与理解

二值网络(Binarized Neural Networks)学习与理解
  • linmingan
  • linmingan
  • 2016年03月29日 19:56
  • 5027

【九度】题目1044:Pre-Post

题目1044:Pre-Post 时间限制:1 秒内存限制:32 兆特殊判题:否提交:502解决:291 题目描述:     We are all familiar with pre-order, in...
  • u013027996
  • u013027996
  • 2014年03月20日 18:11
  • 1316

Map Serialization and Deserialization with Jackson

1. OverviewIn this article, we'll look at serialization and deserialization of Java maps using Jac...
  • kangkanglou
  • kangkanglou
  • 2017年07月05日 22:25
  • 130

Symbian_OS_Descriptors_For_Text_And_Binary_Data_With_Example

  • 2008年12月10日 16:39
  • 318KB
  • 下载

MySQL backup and point in time recovery with binary logs

本文转载自:http://blog.bytetouch.com/system-administration/mysql-backup-and-point-in-time-recovery-with-b...
  • yumushui
  • yumushui
  • 2014年07月29日 10:07
  • 580

Binary search with addition and subtraction

最近在学习Algorithms 4th, 做个记录 题目 Binary search with only addition and subtraction. [Mihai Patrascu] Wr...
  • u010434271
  • u010434271
  • 2017年02月10日 09:46
  • 107

Binary Tree Serialization

Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a...
  • mogusuntou
  • mogusuntou
  • 2017年02月24日 22:27
  • 93

331. Verify Preorder Serialization of a Binary Tree

Task: One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-n...
  • chenwenwen1989
  • chenwenwen1989
  • 2016年03月07日 20:11
  • 208

LeetCode 331. Verify Preorder Serialization of a Binary Tree(校验二叉树的前序遍历序列化)

原题网址:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ One way to seri...
  • jmspan
  • jmspan
  • 2016年04月28日 07:36
  • 399
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Binary Serialization and BinaryFormatter with WebServices
举报原因:
原因补充:

(最多只允许输入30个字)