.NET远程处理(Remoting)与WCF的性能对比
来源:未知 责任编辑:责任编辑 发表时间:2014-01-06 18:19 点击:次
用TCP信道时,.NET远程处理默认的序列化器是System.Runtime.Serialization.Formatters.Binary.BinaryFormatter。WCF是用SOAP传送消息的,即格式是XML。可以设置XML的编码方式,选择编码为文本或二进制数据。下面测试用BinaryFormatter和WCF的序列化器序列化同一个对象时,哪一个产生的二进制流最短。
class Program
{
static void Main(string[] args)
{
Customer customer = new Customer { Age = 30, FirstName = "Gqq", LastName = "Nb" };
using (FileStream stream1 = new FileStream(@"B:\binaryFormatter.bin", FileMode.Create))
{
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(stream1, customer);
}
using (FileStream stream2 = new FileStream(@"B:\xmlFormatter.bin", FileMode.Create))
{
var serializer = new DataContractSerializer(typeof(Customer));
XmlDictionaryWriter binaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream2);
serializer.WriteObject(binaryWriter, customer);
binaryWriter.Close();
}
}
}
[DataContract]
[Serializable]
class Customer
{
private string fn;
[DataMember]
public string FirstName
{
get { return fn; }
set { fn = value; }
}
private string ln;
[DataMember]
class Program
{
static void Main(string[] args)
{
Customer customer = new Customer { Age = 30, FirstName = "Gqq", LastName = "Nb" };
using (FileStream stream1 = new FileStream(@"B:\binaryFormatter.bin", FileMode.Create))
{
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(stream1, customer);
}
using (FileStream stream2 = new FileStream(@"B:\xmlFormatter.bin", FileMode.Create))
{
var serializer = new DataContractSerializer(typeof(Customer));
XmlDictionaryWriter binaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream2);
serializer.WriteObject(binaryWriter, customer);
binaryWriter.Close();
}
}
}
[DataContract]
[Serializable]
class Customer
{
private string fn;
[DataMember]
public string FirstName
{
get { return fn; }
set { fn = value; }
}
private string ln;
[DataMember]
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>