This example code streams large data files using Windows Communication Foundation (WCF) and the WebHttpBinding.
[The original code comes from this forum post: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/fb9efac5-8b57-417e-9f71-35d48d421eb4 ] For more information about streaming, see http://msdn.microsoft.com/en-us/library/ms733742.aspx.
public
class
Post_fb9efac5_8b57_417e_9f71_35d48d421eb4
{
[ServiceContract]
interface
ITest
[OperationContract]
[WebGet]
Stream DownloadFile(
string
fileName);
[WebInvoke(UriTemplate =
"/UploadFile/{fileName}"
)]
void
UploadFile(
fileName, Stream fileContents);
}
static
long
CountBytes(Stream stream)
byte
[] buffer =
new
[100000];
int
bytesRead;
totalBytesRead = 0;
do
bytesRead = stream.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
while
(bytesRead > 0);
return
totalBytesRead;
MyReadonlyStream : Stream
length;
leftToRead;
MyReadonlyStream(
length)
this
.length = length;
.leftToRead = length;
override
bool
CanRead
get
true
; }
CanSeek
false
CanWrite
Flush()
Length
.length; }
Position
throw
NotSupportedException(); }
set
Read(
[] buffer,
offset,
count)
toReturn = (
)Math.Min(
.leftToRead, (
)count);
.leftToRead -= toReturn;
toReturn;
Seek(
offset, SeekOrigin origin)
NotSupportedException();
SetLength(
value)
Write(
Service : ITest
fileName)
WebOperationContext.Current.OutgoingResponse.Headers[
"Content-Disposition"
] =
"attachment; filename="
+ fileName;
MyReadonlyStream(200000000);
//200MB
fileName, Stream fileContents)
totalBytesRead = CountBytes(fileContents);
Console.WriteLine(
"Total bytes read for file {0}: {1}"
, fileName, totalBytesRead);
Test()
baseAddress =
"http://"
+ Environment.MachineName +
":8000/Service"
;
ServiceHost host =
ServiceHost(
typeof
(Service),
Uri(baseAddress));
WebHttpBinding binding =
WebHttpBinding
TransferMode = TransferMode.Streamed,
MaxReceivedMessageSize =
.MaxValue,
};
binding.ReaderQuotas.MaxArrayLength =
.MaxValue;
host.AddServiceEndpoint(
(ITest), binding,
""
).Behaviors.Add(
WebHttpBehavior());
host.Open();
"Host opened"
);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(baseAddress +
"/DownloadFile?fileName=test.txt"
req.Method =
"GET"
HttpWebResponse resp;
try
resp = (HttpWebResponse)req.GetResponse();
catch
(WebException e)
resp = (HttpWebResponse)e.Response;
"HTTP/{0} {1} {2}"
, resp.ProtocolVersion, (
)resp.StatusCode, resp.StatusDescription);
foreach
(
header
in
resp.Headers.AllKeys)
"{0}: {1}"
, header, resp.Headers[header]);
Stream respStream = resp.GetResponseStream();
size = CountBytes(respStream);
"Response size: {0}"
, size);
req = (HttpWebRequest)HttpWebRequest.Create(baseAddress +
"/UploadFile/test.txt"
"POST"
req.SendChunked =
req.AllowWriteStreamBuffering =
req.ContentType =
"application/octet-stream"
Stream reqStream = req.GetRequestStream();
[10000000];
bytesWritten = 0;
for
i = 0; i < 50; i++)
reqStream.Write(buffer, 0, buffer.Length);
bytesWritten += buffer.Length;
if
((i % 10) == 0)
"Wrote {0} bytes"
, bytesWritten);
reqStream.Close();
Console.WriteLine(resp.StatusCode);
This article is also available in the following languages: