How to cancel an HTTP message without reading its full contents (.NET REST service)

I created a self-service .NET service that accepts binary downloads.

[ServiceContract]
public interface IBinaryService
{

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "up/file/{fileName}/{hash}")]

    void FileUpload(string fileName, string hash, Stream fileStream);

When he receives a file, he first checks whether this file is already in the system, otherwise he transfers the file from the client and saves it:

    public void FileUpload(string fileName, string hash, Stream fileStream)
    {
        string filebasedir = basedir + @"file\"; //" 
        if (File.Exists(filebasedir + hash))
        {

            WebOperationContext.Current.OutgoingResponse.StatusCode = 
                                   System.Net.HttpStatusCode.Conflict;
            return;
        }

        using(var fileToupload = new FileStream(
                string.Concat(filebasedir, hash),
                FileMode.Create))
                {
                fileStream.CopyTo(fileToupload);
                }

If I go through the code, I see that the contents of the file are not broadcast until AFTER the server reads the parameters and decides if there is a conflict. I just need to somehow make the server not read the full content (which can be many megabytes). Exiting a method earlier using 'return' does not, unfortunately.

Is it possible?

+5
source share
4 answers

, .

void FileUpload(string fileName, string hash, Stream fileStream);

, . , .

, , ( , ) true false, , filestream.

, .

+4

( ) , . , .

, :

1) , (httpHandler). , , . .

2) , , , . , ABCUpload.NET . - PowUpload.

0

Maybe just return the answer with the error code and don’t miss the file

-1
source

You can use the method OperationContext.Current.RequestContext.Abort().

-2
source

All Articles