Hi all!
I'm trying to create RESTful service which upload and download big files through stream. I have VS2008/Framework 3.5
Download:
Code snippet(Server side):
Contract:
[OperationContract]
[WebGet(RequestFormat=WebMessageFormat.Xml, ResponseFormat=WebMessageFormat.Xml,
UriTemplate = "/DownloadFile/?filename={filename}")]
Stream DownloadFile(string filename);
Implementation:
public Stream DownloadFile(string filename)
{
string filenamefull = @"C:\downloads\" + filename;
using (FileStream fs = new FileStream(filenamefull, FileMode.Open, FileAccess.Read))
{
WebOperationContext ctx = WebOperationContext.Current;
ctx.OutgoingResponse.ContentType = "application/octet-stream";
ctx.OutgoingResponse.ContentLength = fs.Length;
return fs;
}
}
app.config:
<webHttpBinding>
<binding name="streamWebHttpBinding" receiveTimeout="01:00:00" maxReceivedMessageSize="2147483648"
sendTimeout="01:00:00" transferMode="Streamed" />
</webHttpBinding
><endpoint address="rest" binding="webHttpBinding" contract="EssentialWCF.IRest"
behaviorConfiguration="behaviorRest" bindingConfiguration="streamWebHttpBinding"/>
<endpointBehaviors>
<behavior name="behaviorRest">
<webHttp />
</behavior>
</endpointBehaviors>
When I execute it I get empty stream! If I change stream to byte[] it works great!
---------------------------------------------------------------------------------
Upload works great, but when I run WCF Test Client I get error like below:
Cannot obtain Metadata from http:
<br />
For request in operation UploadFile to be a stream the operation must have a single parameter whose type is Stream.
Code snippet:
Contract:
[OperationContract, WebInvoke(UriTemplate = "/UploadFile/?filename={fileName}")]
void UploadFile(string fileName, Stream fileContents);
Implementation:
public void UploadFile(string fileName, Stream fileContents)
{
using (FileStream fs = new FileStream(@"C:\Downloads\" + fileName, FileMode.Create, FileAccess.Write ))
{
CopyStream(fileContents, fs);
}
}
app.config above.
Could anyone explain me what's wrong?