C# 使用wcf rest请求大文件流失败
我有一个REST GET API,它是使用WCF库编写的,用于返回特定请求文件的流,该文件位于承载该web服务应用程序的API服务器上。如果请求文件的大小很小,则服务工作正常;这不到100 MB。但是如果文件大小大于100 MB,那么服务返回0字节,没有任何记录的信息,我可以从库方法中获取这些信息(比如“catch”块) 库方法(类库项目)返回所需文件的流C# 使用wcf rest请求大文件流失败,c#,asp.net,wcf,streaming,httpwebrequest,C#,Asp.net,Wcf,Streaming,Httpwebrequest,我有一个REST GET API,它是使用WCF库编写的,用于返回特定请求文件的流,该文件位于承载该web服务应用程序的API服务器上。如果请求文件的大小很小,则服务工作正常;这不到100 MB。但是如果文件大小大于100 MB,那么服务返回0字节,没有任何记录的信息,我可以从库方法中获取这些信息(比如“catch”块) 库方法(类库项目)返回所需文件的流 public Stream GetFile(string fileId, string seekStartPosition=null)
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;
}
公共流GetFile(字符串fileId,字符串seekStartPosition=null)
{
_lastActionResult=string.Empty;
Stream fileStream=null;
尝试
{
Guid文件Guid;
if(Guid.TryParse(fileId,out fileGuid)==false)
{
_lastActionResult=string.Format(ErrorMessage.FileIdValidt,fileId);
}
其他的
{
ContentPackageItemService contentItemService=新的ContentPackageItemService();
字符串filePath=dalcacheheloper.GetFilePath(fileId);
if(File.Exists(filePath))
{
fileStream=newfilestream(filePath,FileMode.Open,FileAccess.Read,FileShare.Read);
long-seekStart=0;
//如果指定了查找位置,请将流指针移动到该位置
if(string.IsNullOrEmpty(seekStartPosition)=false&&long.TryParse(seekStartPosition,out seekStart))
{
//确保搜索位置小于文件大小
FileInfo fi=新的FileInfo(filePath);
如果(seekStart>=0&&seekStart
客户端项目上的API方法(其中.svc文件为):
[WebGet(UriTemplate=“files/{fileid}”)]
公共流GetFile(字符串文件ID)
{
ContentHandler=新的ContentHandler();
Stream fileStream=null;
尝试
{
fileStream=handler.GetFile(fileid);
}
捕获(例外情况除外)
{
Write(string.Format(“{0}{1}”,例如Message,例如StackTrace),“General”,1,Constants.LogId.RESTSync,System.Diagnostics.TraceEventType.Error,System.Reflection.MethodBase.GetCurrentMethod().Name);
抛出新的WebFaultException(新的ErrorResponse(HttpStatusCode.InternalServerError,ex.Message),HttpStatusCode.InternalServerError);
}
if(fileStream==null)
{
抛出新的WebFaultException(新的ErrorResponse(handler.LastActionResult)、HttpStatusCode.InternalServerError);
}
返回文件流;
}
当您使用REST时,我假定您使用的是WebHttpBinding。您需要在客户端绑定上设置MaxReceivedMessageSize,使其足以满足预期的最大响应大小。默认值为64K。如果要在代码中创建绑定,则为属性。如果要在app.config中创建绑定,则需要
[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;
}