http://www.cnblogs.com/jeriffe/articles/2084084.html
rotobuf是google提供的一个开源序列化框架,类似于XML,JSON这样的数据表示语言,其最大的特点是基于二进制,因此比传统的XML表示高效短小得多
protobuf-net是Google的protobuf的dotNet实现,目前支持Mono,SilverLight,WCF,WP7 etc。
官方地址:http://code.google.com/p/protobuf-net/
下来我们构建一个简单的示例程序,
1.在官方地址下载最新的类库(目前最新的是:protobuf-net v2 beta r404.zip)
2.建立一个Console Application
3.添加类库:protobuf-net.dll到我们的应用程序。
4.示例代码如下:
namespace ProtoBuf_NET
{
class Program
{
static void Main(string[] args)
{
string dataDir = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Data");
string fileName = Path.Combine(dataDir, "Person.txt");
if(!Directory.Exists(dataDir))
{
Directory.CreateDirectory(dataDir);
}
//Initialize
var person = new Person
{
Id = 12345,
Name = "Fred",
Address = new Address
{
Line1 = "Flat 1",
Line2 = "The Meadows"
}
};
//Serializing Data
using (var file = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
Serializer.Serialize<Person>(file, person);
}
//Deserializing Data
Person newPerson;
using (var file = File.OpenRead(fileName))
{
newPerson = Serializer.Deserialize<Person>(file);
}
Console.WriteLine("Name: "+newPerson.Name
+Environment.NewLine+"Line1:"+newPerson.Address.Line1
+Environment.NewLine+"Line2:"+newPerson.Address.Line2);
Console.Read();
}
}
}
{
class Program
{
static void Main(string[] args)
{
string dataDir = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Data");
string fileName = Path.Combine(dataDir, "Person.txt");
if(!Directory.Exists(dataDir))
{
Directory.CreateDirectory(dataDir);
}
//Initialize
var person = new Person
{
Id = 12345,
Name = "Fred",
Address = new Address
{
Line1 = "Flat 1",
Line2 = "The Meadows"
}
};
//Serializing Data
using (var file = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
Serializer.Serialize<Person>(file, person);
}
//Deserializing Data
Person newPerson;
using (var file = File.OpenRead(fileName))
{
newPerson = Serializer.Deserialize<Person>(file);
}
Console.WriteLine("Name: "+newPerson.Name
+Environment.NewLine+"Line1:"+newPerson.Address.Line1
+Environment.NewLine+"Line2:"+newPerson.Address.Line2);
Console.Read();
}
}
}
如果要支持WCF,那么我们就需要添加一个behaviorExtensions
具体如下:
<behaviors>
<endpointBehaviors>
<behavior name="protoEndpointBehavior">
<protobuf/>
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="protobuf" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=2.0.0.280, Culture=neutral, PublicKeyToken=257b51d87d2e4d67"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
<endpointBehaviors>
<behavior name="protoEndpointBehavior">
<protobuf/>
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="protobuf" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=2.0.0.280, Culture=neutral, PublicKeyToken=257b51d87d2e4d67"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
在客户端和服务端都加上这个ExtensionBehavior
Client:
<client>
<endpoint address="http://localhost:33545/Service1.svc" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
name="WSHttpBinding_IService1" behaviorConfiguration="protoEndpointBehavior">
<dns value="localhost" />
</identity>
</endpoint>
</client>
<endpoint address="http://localhost:33545/Service1.svc" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
name="WSHttpBinding_IService1" behaviorConfiguration="protoEndpointBehavior">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
Server:
<service>
<endpoint address="" binding="wsHttpBinding" contract="TestWcfDto.IService1" behaviorConfiguration="protoEndpointBehavior">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
<endpoint address="" binding="wsHttpBinding" contract="TestWcfDto.IService1" behaviorConfiguration="protoEndpointBehavior">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>