如何通过WCF通用对象 [英] How to pass a generic object through WCF

查看:132
本文介绍了如何通过WCF通用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过通用对象(由通用I指的是任何与 DataContract ,而不是C#泛型集合)从没有一个根继承?目前,我有这样的事情:

Is it possible to pass generic objects (by generic I mean any with DataContract, not C# generic collection) without inheriting from one root? At the moment I have something like this:

[OperationContract]
void Save(GenericObject o);

[DataContract]
[KnownType(typeof(ClassA))]
[KnownType(typeof(ClassB))]
class GenericObject {....

[DataContract]
class ClassA: GenericObject {....


[DataContract]
class ClassB: GenericObject {....

一切正常,但该项目得到了更大,我真的不能承受导出所有这些合同一个类中。

Everything was OK, but the project has got bigger and I cannot really afford to derive all those contracts from one class.

我不想专门的方法添加到服务。
作为一种变通方法,我可以[]通过DataContractSerializer的和反序列化它们在服务器端手动序列化类的字符串或字节:无效保存(字节[] serializedObject),但是这是一个黑客位的

I don't want to add specialized methods to the service. As a workaround I can manually serialized classes to string or byte[] via DataContractSerializer and deserialized them on the server side: void Save(byte[] serializedObject) but that's a bit of a hack.

我注意到,我可以轻松地返回一般对象如:
对象负载(字符串ID)
工作得很好,但无效保存(对象o)没有。

I've noticed that I can easily return generic objects e.g. object Load(string id) works just fine, but void Save(object o) does not.

我使用Silverlight作为客户端。

I'm using Silverlight as a client.

推荐答案

是的,你可以使用一个通用的消息键入WCF项目,它可以是任何东西,真的。但是,这会带来大量的工作你,无论是在客户端创建消息对象(你基本上手动创建拼成的通用消息的XML),以及服务器端,因为你可以不依赖在漂亮的XML序列化/反序列化了。你正在处理的基础,生尖括号汤(直XML)

Yes, you can use a generic Message type for WCF projects, which can be anything, really. However, this imposes a lot of work on you, both on the client side creating the message object (you basically have to manually create the XML that makes up the generic message), as well as the server side, since you can't rely on the nice XML serialization/deserialization anymore. You're dealing with basic, raw angle-bracket soup (straight XML).

它的工作原理,它可能是一个解决方案 - 但也许还有其他的,更优雅,有用的解决方案?是继承真正需要的呢?想想你的系统设计。

It works and it could be a solution - but maybe there are others, more elegant and useful solutions? Is that inheritance really needed at all? Think about your systems design.

马克

这篇关于如何通过WCF通用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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