【问题标题】:Request big file stream failed with wcf restwcf rest 请求大文件流失败
【发布时间】:2015-10-01 21:58:42
【问题描述】:

我有一个使用 WCF 库编写的 REST GET API,它返回位于托管该 Web 服务应用程序的 API 服务器上的特定请求文件的流。如果请求的文件很小,则该服务运行良好;小于 100 MB。但如果文件大小大于 > 100 MB,则服务返回 0 字节而没有任何记录信息,我可以获取库方法(即“catch”块)。

库方法(类库项目)返回所需文件的Stream是

public Stream GetFile(string fileId, string seekStartPosition=null)
        {
            _lastActionResult = string.Empty;
            Stream fileStream = null;

            try
            {
            Guid fileGuid;
            if (Guid.TryParse(fileId, out fileGuid) == false)
            {
                _lastActionResult = string.Format(ErrorMessage.FileIdInvalidT, fileId);
            }
            else
            {
                ContentPackageItemService contentItemService = new ContentPackageItemService();
                string filePath = DALCacheHelper.GetFilePath(fileId);

                if (File.Exists(filePath))
                {
                    fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

                    long seekStart = 0;
                    // if seek position is specified, move the stream pointer to that location
                    if (string.IsNullOrEmpty(seekStartPosition) == false && long.TryParse(seekStartPosition, out seekStart))
                    {
                        // make sure seek position is smaller than file size
                        FileInfo fi = new FileInfo(filePath);
                        if (seekStart >= 0 && seekStart < fi.Length)
                        {
                            fileStream.Seek(seekStart, SeekOrigin.Begin);
                        }
                        else
                        {
                            _lastActionResult = string.Format(ErrorMessage.FileSeekInvalidT, seekStart, fi.Length);
                        }
                    }
                }
                else
                {
                    _lastActionResult = string.Format(ErrorMessage.FileNotFoundT, fileId);
                    Logger.Write(_lastActionResult,
                        "General", 1, Constants.LogId.RESTSync, System.Diagnostics.TraceEventType.Error, System.Reflection.MethodBase.GetCurrentMethod().Name);
                }

            }
      }
      catch(Exception ex)
      {
          Logger.Write(ex,"General", 1, Constants.LogId.RESTSync, System.Diagnostics.TraceEventType.Error, System.Reflection.MethodBase.GetCurrentMethod().Name);
      }

     return fileStream;

    }

客户端项目的API方法(.svc文件所在的位置):

[WebGet(UriTemplate = "files/{fileid}")]
        public Stream GetFile(string fileid)
        {
            ContentHandler handler = new ContentHandler();
            Stream fileStream = null;
            try
            {
                fileStream = handler.GetFile(fileid);
            }
            catch (Exception ex)
            {
                Logger.Write(string.Format("{0} {1}", ex.Message, ex.StackTrace), "General", 1, Constants.LogId.RESTSync, System.Diagnostics.TraceEventType.Error, System.Reflection.MethodBase.GetCurrentMethod().Name);

                throw new WebFaultException<ErrorResponse>(new ErrorResponse(HttpStatusCode.InternalServerError, ex.Message), HttpStatusCode.InternalServerError);
            }

            if (fileStream == null)
            {
                throw new WebFaultException<ErrorResponse>(new ErrorResponse(handler.LastActionResult), HttpStatusCode.InternalServerError);
            }

            return fileStream;

        }

【问题讨论】:

    标签: c# asp.net wcf streaming httpwebrequest


    【解决方案1】:

    当您使用 REST 时,我假设您使用的是 WebHttpBinding。您需要将客户端绑定上的 MaxReceivedMessageSize 设置为足以满足最大预期响应大小。默认值为 64K。 Here's the msdn documentation 用于在代码中创建绑定时的属性。如果您在 app.config 中创建绑定,则需要 this is the documentation

    【讨论】:

      猜你喜欢
      • 2013-08-04
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 2010-10-21
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 2015-06-11
      相关资源
      最近更新 更多