Clicky

Hi Experts,

  I've WCF method where it returns byte[] (for PDF document) and in ASPX page the PDF is being rendered correctly. But my boss is saying that being returned byte[] from the WCF service method is costly and will degrade the performance. Is it true? He suggested to return as STREAM object. But returning stream object from WCF is not getting serialized properly.

  Could you please clarify that what are the impact by returning byte[]?

  How to return stream object from WCF method to client without any issue?

  Any help is much appreciated. Thanks in advance.

asked 11/30/2011 01:54

EaswaranP's gravatar image

EaswaranP ♦♦


13 Answers:
Streaming is a great part of WCF, and this benefit is provided by the OData protocol, described in this referenced link (from above)

Streaming Provider (WCF Data Services)
http://msdn.microsoft.com/en-us/library/ee960144%28v=VS.100%29.aspx

When an entity in the data model includes one or more binary properties, the data service returns this binary data encoded as base-64 inside the entry in the response feed. Because loading and serializing large binary data in this manner can affect performance, the Open Data Protocol (OData) defines a mechanism for retrieving binary data independent of the entity to which it belongs. This is accomplished by separating the binary data from the entity into one or more data streams.
link

answered 2011-12-01 at 10:19:22

TheLearnedOne's gravatar image

TheLearnedOne

Thanks for your valuable comments.

TheLearnedOne: In your case, the data should be in database as BLOB. But let me describe my case. It is Stream object is returned by Sharepoint method to .net. The stream array is converted to byte[] and set value for ONE OF my Response object. It works fine. The stream itself could not be sent. I tried many options but no luck. I get various error such as "((System.IO.Stream)(memoryStream)).ReadTimeout' threw an exception of type 'System.InvalidOperationException". I tried to rectify that as well. But no luck.


lucky85: As you specified in the 3rd link, I tried MessageContract and MessageMember attribute instead of my existing DataContract and DataMember attributes. I tried with even with both attributes. But no luck.

Could you please guide me to resolve this issue as I'm pulling my hair for past few days.?

Thanks in advance.

link

answered 2011-12-01 at 10:29:22

EaswaranP's gravatar image

EaswaranP

How much of that referenced article did you read?  Did you see the part about implementing IDataServiceStreamProvider?

With WCF Data Services, you define a binary resource stream by implementing a streaming data provider. The streaming provider implementation supplies the data service with the media resource stream associated with a specific entity as an Stream object. This implementation enables the data service to accept and return media resources over HTTP as binary data streams of a specified MIME type.

Configuring a data service to support the streaming of binary data requires the following steps:

Attribute one or more entities in the data model as a media link entry. These entities should not include the binary data to be streamed. Any binary properties of an entity are always returned in the entry as base-64 encoded binary.

Implement the T:System.Data.Services.Providers.IDataServiceStreamProvider interface.

Define a data service that implements the IServiceProvider interface. The data service uses the GetService implementation to access the streaming data provider implementation. This method returns the appropriate streaming provider implementation.

Enable large message streams in the Web application configuration.

Enable access to binary resources on the server or in a data source.
link

answered 2011-12-02 at 03:16:10

TheLearnedOne's gravatar image

TheLearnedOne

What are you trying to do with PDF at the client side ? Is it something like a downloading or opening a file at client side?
link

answered 2011-12-02 at 05:13:10

apeter's gravatar image

apeter

Yes. You are right. I'm trying to download/show the PDF file. This could be some other file types as well such as word. But I need to return the stream through WCF Service.
link

answered 2011-12-04 at 23:08:27

EaswaranP's gravatar image

EaswaranP

Using byte[] is not a good option if the file size is  more.Check below sample where it returns a stream from wcf service. Then you can read the straem and wrap inside response object send back to client.

http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/
link

answered 2011-12-05 at 20:55:17

apeter's gravatar image

apeter

Hi apeter: Thanks for your comments. Let me apply the same in my project and see whether it works or not.
link

answered 2011-12-06 at 07:57:05

EaswaranP's gravatar image

EaswaranP

In order to add [MessageContract] which DLL should be included? Please guide me.
link

answered 2011-12-06 at 21:23:48

EaswaranP's gravatar image

EaswaranP

add this at the top and it should work.

using System.ServiceModel;
link

answered 2011-12-13 at 06:16:09

lucky85's gravatar image

lucky85

I tried with MessageContract. But while adding the servicereferece to client application I get error as specfied in the attached screenshot. Please guide me to resolve.
link

answered 2011-12-13 at 08:52:57

EaswaranP's gravatar image

EaswaranP

Can you access yourservice.svc in browser? If yes, can you access its wsdl by adding '?wsdl' at the address end?

For debug convince, please enable exception detail, to do this, open web.config file, update this section

<serviceDebug includeExceptionDetailInFaults="true"/>
link

answered 2011-12-13 at 23:34:50

lucky85's gravatar image

lucky85

Thanks for your help!!
link

answered 2011-12-14 at 07:12:18

EaswaranP's gravatar image

EaswaranP

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×102
×14

Asked: 11/30/2011 01:54

Seen: 429 times

Last updated: 12/14/2011 08:23