简体   繁体   中英

WCF returning different types of data

I'm just starting out really with WCF and Web Services in general. I have a pretty firm understanding on the purposes behind them and how they work, but I was wondering what the capabilities with them are if you wished to return something other than Text; such as a straight HTML form, or an image.

I've done some simple googling but alas all I can find is how to handle data passed from a form, rather than how to return a form.

I hope someone could give me a good starting point on what I should be looking at. I looked at a thread stating to look at Streaming with WCF but that may seem a bit excessive and was wondering if someone could give me some general advice and input.

Many thanks, Ronald.

WCF services can return any object that the runtime can seralize. We return custom objects in our services with no issues, provided it's a .NET Client consuming them. Other languages may have to work harder to de-serialize complex objects. (Meaning you have to write more code because non-Visual Studio IDE's probably won't know how to auto-generate the required client code.)

It probably depends on the actual binding but, for the sake of simplicity, assume that you bind your WCF service over http. Then, everything you pass to and from the service should somehow be translated to string. Simple types, ints, doules, strings, are easily convertible. Compound types - also, as they consist of simple types. When it comes to specific types like images or html forms, you always try to find a way to convert them at one side and convert back on the other side. In many cases the serializer can do it for you, for example if you return byte[] , the data will be encoded as base64 string. If the serializer fails for some reason, you have to find your own way to pass your specific types. Please also remember that for WCF, it is you to select a particular serializer:

http://nirajrules.wordpress.com/2009/08/26/wcf-serializers-xmlserializer-vs-datacontratserializer-vs-netdatacontractserializer/

WCF is designed to build web API using standard or custom protocols. If you use the default configuration, WCF will output objects serialized using SOAP, but JSON is available too, for instance.

WCF is certainly able to output plain HTML, but it isn't designed with this goal in mind. It is meant to be used for communication between processes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM