是否可以在WCF服务中同时使用DataContractSerializer和XmlSerializer? [英] Is it possible to use both DataContractSerializer and XmlSerializer in a WCF-service?

查看:71
本文介绍了是否可以在WCF服务中同时使用DataContractSerializer和XmlSerializer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

是否可以在WCF服务中同时使用DataContractSerializer和XmlSerializer(将它们组合),并且也可以使用一种方法?

我有一个带有大约50个带有DataContract-attributes标记的类的项目.我也有一些必须使用的外部XSD方案,它与DataContractSerializer不兼容(集合未在标签中分组).我从这些模式中生成了带有XmlSerializer-attributes标记的类(另50个).我的解决方案与此类似:

标记为DataContracts的类:
-驱动程序
-车轮
-ConnectionInfo

标记为XmlSerializable的类:
-StandarizedCar
-MyCustomCar:StandarizedCar

因此,Standarized汽车是从XSD生成的,并带有XmlSerializable属性标记.我制作了一个MyCustomCar类,它扩展了StandarizedCar并添加了一些属性(例如wheel和driver,请注意Wheel是DataContract).我想通过以下方法进行服务:

Hi!

Is it possible to use both DataContractSerializer and XmlSerializer (combine them) in a WCF-service, and maybe also in one method?

I have a project with approximately 50 classes marked with DataContract-attributes. I altso have some external XSD-schemas I have to use wich is not compatible with DataContractSerializer (collections are not grouped in a tag). I have generated classes (another 50) marked with XmlSerializer-attributes from these schemas. My solution looks similar to this:

Classes marked as DataContracts:
- Driver
- Wheel
- ConnectionInfo

Classes marked as XmlSerializable:
- StandarizedCar
- MyCustomCar : StandarizedCar

So, Standarized car is generated from XSD and marked with XmlSerializable-attributes. I made a class MyCustomCar wich extends StandarizedCar and adds some properties (like wheels and a driver, notice that Wheel is a DataContract). I want to make a service with the following methods:

void:Connect(ConnectionInfo info)
MyCustomCar:GetCustomCar(int carID)
void:AddWheel(Wheel wheel)



我的问题:

1.我的服务可以同时使用DataContractSerializer和XmlSerializer吗?
2.我的方法GetCustomCar()可以同时使用DataContractSerializer和XmlSerializer,还是必须为每种方法选择一个?
3.是否可以同时使用DataContractSerializer属性和XmlSerializer属性标记Wheel?
4.即使Wheels没有任何XmlSerializer属性,即使我用[XmlSerializerFormat]标记它,GetCustomCar也会起作用吗?



My question(s):

1. Can my Service use both DataContractSerializer and XmlSerializer?
2. Can my method GetCustomCar() use both DataContractSerializer and XmlSerializer, or do I have to choose one for each method?
3. Can I mark Wheel with both DataContractSerializer-attributes AND XmlSerializer attributes?
4. Will GetCustomCar work if I mark it with [XmlSerializerFormat] even though Wheels don''t have any XmlSerializer-attributes)?

推荐答案

1.我的服务可以同时使用DataContractSerializer和XmlSerializer吗?
是的,但是WCF客户端希望使用DataContractSerializer.

2.我的方法GetCustomCar()可以同时使用DataContractSerializer和XmlSerializer,还是必须为每种方法选择一个?
我不确定为什么要这么做,但是您必须弄清楚哪一种才是在运行时运行的正确方法,如果您不能做到这一点,那么您将无法使用相同的方法.方法


3.是否可以同时使用DataContractSerializer属性和XmlSerializer属性标记Wheel?
是的,AFAIK

4.即使Wheels没有任何XmlSerializer属性,即使我用[XmlSerializerFormat]标记了GetCustomCar,它也可以工作吗?
是:只会序列化您修饰过的超类中的属性.

我不是所有这一切的100%,所以最好的办法是削减代码.如果要使用XmlSerializer进行服务器端持久性,而使用DataContract Serialzer进行WCF服务通信,则需要添加一个基于DataContract的对象.基于XmlSerialer的对象转换器(两个),然后将这两层完全分开.由于您的类相似,因此您可能可以编写这样的代码为您自动生成类(和转换器).
1. Can my Service use both DataContractSerializer and XmlSerializer?
Yes- but the WCF client expects the DataContractSerializer to be used.

2. Can my method GetCustomCar() use both DataContractSerializer and XmlSerializer, or do I have to choose one for each method?
I''m not sure why you''d want to,but you''d have to work out which is the correct one to run at runtime, if you can''t do this then you can''t used the same method


3. Can I mark Wheel with both DataContractSerializer-attributes AND XmlSerializer attributes?
Yes, AFAIK

4. Will GetCustomCar work if I mark it with [XmlSerializerFormat] even though Wheels don''t have any XmlSerializer-attributes)?
Yes: It will just serialize the properties in the super class you decoracted.

I''m not 100% about all this, so the best thing to do is cut code. I''d add this, if you are using the XmlSerializer for server-side persistence and the DataContract Serialzer for WCF service communication, a MUCH better architecture wild be to have a DataContract-based obect --> XmlSerialer-based object converter (two), then you separate the two layers out completely. As your classes are similar, you can probably write so code to auto-generate the classes (and converters) for you.


这篇关于是否可以在WCF服务中同时使用DataContractSerializer和XmlSerializer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆