发送到 WCF REST 服务的数据不会转到 Stream 输入参数 [英] Data sent to a WCF REST service wouldn't go to the Stream input parameter

查看:44
本文介绍了发送到 WCF REST 服务的数据不会转到 Stream 输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力将文件上传到 WCF REST 服务.

I'm currently struggling with implementing file upload to a WCF REST service.

客户端执行以下操作:

void uploadFile( string serverUrl, string filePath )
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.
        Create( serverUrl );
    request.Method = "POST";
    request.ContentType = "multipart/form-data";
    request.SendChunked = true;
    request.Timeout = 60000;
    request.KeepAlive = true;

    using( BinaryReader reader = new BinaryReader( 
        File.OpenRead( filePath ) ) ) {

        request.ContentLength = reader.BaseStream.Length;
        using( Stream stream = request.GetRequestStream() ) {
            byte[] buffer = new byte[1024];
            while( true ) {
                int bytesRead = reader.Read( buffer, 0, buffer.Length );
                if( bytesRead == 0 ) {
                    break;
                }
                stream.Write( buffer, 0, bytesRead );
            }
        }
    }

     HttpWebResponse result = (HttpWebResponse)request.GetResponse();
     //handle result - not relevant
 }

其中 serverUrl 是http://localhost:/Service1.svc/UploadFile".

where serverUrl is "http://localhost:/Service1.svc/UploadFile".

WCF REST 服务有一个具有以下签名的方法:

The WCF REST service has a method with the following signature:

[OperationContract]
[WebInvoke(Method="POST")]
string UploadFile(System.IO.Stream fileData);

实现如下:

public string UploadFile(System.IO.Stream fileData)
{
    byte[] buffer = new byte[1024];
    while( true ) {
        int readAmount = fileData.Read( buffer, 0, buffer.Length );
        if( readAmount == 0 ) {
            break;
        }
    }
    return "";
}

当我启动服务并运行客户端代码时,会发生以下情况.

When I start the service and run the client code the following happens.

客户端启动while循环,控制进入服务方法,服务端启动while循环.服务器上的 Read 返回零,控制失败,服务器方法返回.同时,客户端在尝试调用 Write() 时遇到异常 - 无法将数据写入传输连接:远程主机强行关闭了现有连接."

The client starts the while-loop, control enters the service method and the server starts the while-loop. Read on the server returns zero, control falls through and the server method returns. Meanwhile the client gets an exception trying to call Write() - "Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host."

所以在我看来,我尝试流式传输到请求中的文件数据并没有映射到服务器方法参数上.我做错了什么?

So to me it looks like the file data that I try to stream into the request just doesn't map onto the server method parameter. What am I doing wrong?

推荐答案

可能尝试不同的内容类型?应用程序/八位字节流"

possibly try a different content-type? "application/octet-stream"

代码在我看来是正确的.客户端是读入整个文件还是有问题?先测试一下.使用 fiddler 查看通过线路连接到服务的实际情况.

The code looks right to me. Does the client part read the entire file in or does it have problems? Test that first. Use fiddler to see what is actually going over the wire to the service.

如果客户端可以将整个文件读入缓冲区,请尝试一次性发送所有文件

If the client can read in the entire file to the buffer, try sending it all at once

request.Write(entireFileBuffer, 0, entireFileLength);request.close();

request.Write(entireFileBuffer, 0, entireFileLength); request.close();

而不是分块.

这篇关于发送到 WCF REST 服务的数据不会转到 Stream 输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆