使用wcf rest

时间:2015-10-01 21:58:42

标签: c# asp.net wcf streaming httpwebrequest

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

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

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;

        }

1 个答案:

答案 0 :(得分:0)

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