WCF, Sending an unknown type to a WCF service

不羁岁月 提交于 2020-01-04 14:16:20

问题


Consider this scenario that two WCF clients connect to one WCF service(server), this service will receive an object from one client and send it to the other one through some operation contract and client callbacks, both clients have the type for this object but we do not want the WCF service(server) to be dependent on this type.

The project is much bigger than this, but I wonder if you can send an object with an unknown type to a service and somehow receive it back on the other client. I saw this but it does not help me at all: Can WCF service transmit type (client doesn't know this type) information?

Thanks in advance.


回答1:


Sending an "object" with unknown type is not possible in WCF because WCF requires a full compatibility with WSDL - and WSDL requires transparent type definition.

Having said that, if you use a type of object I believe there is a way for this to be loaded as a string and in WSDL it is defined as xs:anyType.

I personally would prefer defining the type as string and passing an XML which can be serialised using plain XML Serialization. I have used this in our company and it works really well, especially since we will be storing the XML as document in database.




回答2:


You can do certain things with the "raw" Message data type - but it's really not pretty programming...

Read about it here:

  • How to pass arbitrary data in a Message object using WCF

  • WCF : Untyped messages on WCF operations.



来源:https://stackoverflow.com/questions/5204893/wcf-sending-an-unknown-type-to-a-wcf-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!