Return pdf file from WCF service?

后端 未结 1 1193
花落未央
花落未央 2021-01-20 19:29

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

相关标签:
1条回答
  • 2021-01-20 20:04

    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>
    
    0 讨论(0)
提交回复
热议问题