2

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.

Community
  • 1
  • 1
Tom
  • 53
  • 7
  • And how would the contract on the service look like? public XMLElement SendReply(XMLElement data)? How does the service know how to relay the message? – rene Mar 05 '11 at 16:00
  • This might be helpful: http://pmichaels.net/2015/01/14/returning-an-unknown-interface-from-a-wcf-service/ – Paul Michaels Jan 16 '15 at 07:50

2 Answers2

3

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

Read about it here:

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Now that it is clear what I really want to do, what do you suggest? I prefer, something more clean, thank you. – Tom Mar 05 '11 at 16:39
  • This is clean. If you are building router or intermediary who don't need to know type of transfered objects you should work with `Message` directly. Both client and real service will work with concrete types. – Ladislav Mrnka Mar 05 '11 at 16:54
  • To quote "When using a parameter of type Message there can be only 1 parameter and the return type of the operation has to be void or must be also of type Message" This alone creates more headache for me. – Tom Mar 05 '11 at 17:00
  • @Tom: well, either you can nail your types in such a way that you can express it as XML schema (since that's the prerequisite for sending the data as WCF messages) - so that excludes interfaces, generics, all-purpose "object" types - or you have to go the way of using the raw `Message` type - I don't really see any alternative... – marc_s Mar 05 '11 at 20:41
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.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • Would you elaborate a little more? it seems that you have the answer to my problem, thank you. – Tom Mar 05 '11 at 16:10
  • If you need to pass the type info as well, you may look at RIA services. This is really what you need which can also transmit the type. – Aliostad Mar 05 '11 at 16:16
  • Thank you, I will, although I do not know nothing about it. would you refer me to the part in which this data type transmission is mentioned? – Tom Mar 05 '11 at 16:21
  • See my question: http://stackoverflow.com/questions/3684421/what-is-wcf-ria-services – Aliostad Mar 05 '11 at 16:45
  • I believe the XML serialization is the way to go for me, at least for now, thank you. – Tom Mar 05 '11 at 20:25