Uploading files WCF Web API Endpoint
Is there any way to upload a file to WC开发者_如何学GoF web API endpoint? If so, how can I access Network Stream to read data?
Any help is very much appreciated.
Thanks.
Assuming you are talking about on the client side:
One way to do it is to create a FileContent class that derives from HttpContent. In the overrides you will be given the NetworkStream when the HTTPClient is ready to receive the stream.
If you are asking about the server side, then you can simply get hold of the HTTPRequestMessage and access the httpRequestMessage.Content.ContentReadStream property.
Update
By default the WCF Transport is limited to sending messages at 65K. If you want to send larger you need to enable "Streaming Transfer Mode" and you need to increase the size of MaxReceivedMessageSize, which is there just as a guard to prevent someone killing your server by uploading a massive file.
So, you can do this using binding configuration or you can do it in code. Here is one way to do it in code:
var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]); //Assuming one endpoint
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10; // Allow files up to 10MB
This answer is only designed to add some details with Web API preview 6.
TransferMode
and MaxRecievedMessageSize
are new exposed through the WebApiConfigurationClass
.
var builder = new WebApiConfiguration();
builder.TransferMode = TransferMode.Streamed;
builder.MaxReceivedMessageSize = 1024 * 1024 * 10;
var serviceRoute = new WebApiRoute(route, new WebAPIServiceHostFactory() { Configuration = builder }, typeof(MyService));
I like it!
精彩评论