我需要为从WCF Web服务返回的大量数据提供导出到Excel的功能。
加载数据列表的代码如下:
List<resultSet> r = myObject.ReturnResultSet(myWebRequestUrl); //call to WCF service
myDataList.DataSource = r;
myDataList.DataBind();
我正在使用Response对象来完成任务:
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=MyExcel.xls");
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter tw = new HtmlTextWriter(sw);
myDataList.RenderControl(tw);
Response.Write(sb.ToString());
Response.End();
问题在于WCF服务在处理大量数据(约5000行)时会超时,并且结果集为空。当我调试服务时,我可以看到保存/打开Excel表格的窗口出现在服务返回结果之前,因此Excel表格始终为空。请帮助我解决这个问题。
编辑后添加-WCF站点的IHttpModule用于重写URL被调用了两次或三次。这可能是由于aspnet_wp回收引起的吗?如果是这样,我应该在我的应用程序事件日志中看到错误,对吧?但我没有。请帮助我解决这个问题。
这是我的自定义HttpModule: public class CustomHttpModule:IHttpModule { public void Dispose(){ }
public void Init(HttpApplication appln)
{
appln.AuthorizeRequest+= delegate
{
HttpContext tcontext= HttpContext.Current;
string path = tcontext.Request.AppRelativeCurrentExecutionFilePath;
int i = path.IndexOf('/', 2);
if (i > 0)
{
string svc = path.Substring(0, i) + ".svc";
string fu = path.Substring(i, path.Length - i);
tcontext.RewritePath(svc, fu, tcontext.Request.QueryString.ToString(), false);
}
};
}
我看到appln.AuthorizeRequest被调用了两次。我认为这就是为什么我会看到操作超时或连接关闭异常的原因。我该如何阻止它重复调用?我只创建了一个请求。