现在的位置: 首页 > 综合 > 正文

protobuf-net简单示例

2018年02月09日 ⁄ 综合 ⁄ 共 2856字 ⁄ 字号 评论关闭

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();
        }
    }
}
复制代码

 

如果要支持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>
复制代码

在客户端和服务端都加上这个ExtensionBehavior

Client:

复制代码

<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>
复制代码

抱歉!评论已关闭.