3

I have a WCF service and I want to one of its service methods to access a pdf file and convert it to a stream or some other thing and return the stream to my web application, where I will be converting that stream back to a pdf file.

Which is the better way, converting the pdf into Stream or byte[] array?

This is my method

 public byte[] GetPdf(string Address)
 {
    byte[] bytes = System.IO.File.ReadAllBytes(Address);
    return bytes
 }

Now in my web application from which I am calling this WCF service, I want to use these bytes and convert them back to pdf. Is this the correct way as I'm not sure whether returning the file as stream is correct or returning as byte[] is correct

But when I'm running the service for testing purpose I'm getting an error

The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

How can I resolve this issue, I have not added any bindings or anything, everything is default as I'm new to WCF

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tarantino
  • 201
  • 1
  • 3
  • 7
  • possible duplicate of [WCF - How to Increase Message Size Quota](http://stackoverflow.com/questions/884235/wcf-how-to-increase-message-size-quota) – Fals Oct 23 '13 at 19:07
  • I don't see this is a duplicate of the message size quota question... the answer to this can and should be different. It is similar to another question though... – RQDQ Oct 23 '13 at 20:49
  • possible duplicate of [Read a PDF file and return as stream from a WCF service?](http://stackoverflow.com/questions/10448694/read-a-pdf-file-and-return-as-stream-from-a-wcf-service) – RQDQ Oct 23 '13 at 20:50

1 Answers1

2

Good Day!

Quote from the book "Programming WCF Services" author Juval Lowy:

By default, when the client and the service exchange messages, these messages are buffered on the receiving end and delivered only once the entire message has been received. This is true whether it is the client sending a message to the service or the service returning a message to the client. As a result, when the client calls the service, the service is invoked only after the client’s message has been received in its entirety; likewise, the client is unblocked only once the returned message with the results of the invocation has been received in its entirety. For sufficiently small messages, this exchange pattern provides for a simple program- ming model because the latency caused by receiving the message is usually negligible compared with the message processing itself. However, when it comes to much larger messages—such as ones involving multimedia content, large files, or batches of data— blocking until the entire message has been received may be impractical. To handle such cases, WCF enables the receiving side (be it the client or the service) to start processing the data in the message while the message is still being received by the channel. This type of processing is known as streaming transfer mode. With large payloads, streaming provides improved throughput and responsiveness because neither the receiving nor the sending side is blocked while the message is being sent or received

For transfer files, I'm recomended using Stream with async pattern.

 [ServiceContract(SessionMode = SessionMode.NotAllowed)]
    public interface ITerrasoftFiles
    {
        [OperationContract(AsyncPattern = true)]
        IAsyncResult BeginGetFiles(Guid ID, AsyncCallback asyncCallBack, object asyncState);

        Stream EndGetFiles(IAsyncResult res);

        [OperationContract]
        FileInfo GetFileInfo(Guid ID);
    }

And set transferMode="StreamedResponse" in Web.config

<bindings>
      <netTcpBinding>
        <binding name="tcpTerrasoftFiles" transferMode="StreamedResponse">
          <security mode="None" />
        </binding>
      </netTcpBinding>
</bindings>
 <services>
    <service name="TWebServices.Services.TerrasoftFiles">
      <endpoint address="" 
                  binding="netTcpBinding" bindingConfiguration="tcpTerrasoftFiles" 
                  contract="TWebServices.Services.ITerrasoftFiles" />
      <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
     </service>
 </services>
StuS
  • 817
  • 9
  • 14