How to send an HTTP response directly to the network

I am writing a high-performance web service using WCF 4.0 + REST. The web service returns XML responses. The return type of my working methods are XDocument, and WCF takes care of XML return. However, building an XML response in memory to return it to the caller is not very efficient.

I am trying to move from XmlDocument / XDocument to XmlWriter. In the console application, I can easily transfer the response to the file, but what about WCF? Can I pass a response using WebOperationContext, HttpContext, returning a stream?

FileStream fs = new FileStream("New.xml", FileMode.Create, FileAccess.Write);
using (XmlWriter writer = XmlWriter.Create(fs))
{
    FormatResponse(writer, myDate);
}

Thanks for the help!

+3
source share
2 answers

Only TCP, IPC, WebHttpBinding, and basicHttp bindings are supported.

Defining Your Contract

[OperationContract]
Stream GetXml();

Implementing your service method

public Stream GetXml()
{
    string filePath = "document.xml";

    try
    {
        FileStream xmlFileStream = File.OpenRead(filePath);
        return xmlFileStream;
    }
    catch (IOException ex)
    {
        // Exception handling logic
    }
}

( StreamedResponse)

<bindings>
   <basicHttpBinding>
      <binding name = "StreamedHTTP"
               transferMode = "StreamedResponse"
      />
   </basicHttpBinding>
</bindings>

TransferMode:

public enum TransferMode
{
   Buffered, //default
   Streamed,
   StreamedRequest,
   StreamedResponse
}

:

  • maxReceivedMessageSize = " "
  • .
  • , SessionMode.Required.
  • TCP .
+1

REST WCF. , .

0

All Articles