将数据从 WCF 服务导出到 Excel
我需要为从 WCF Web 服务返回的大量数据提供导出到 Excel 的功能。
加载数据列表的代码如下:
List<resultSet> r = myObject.ReturnResultSet(myWebRequestUrl); //call to WCF service
myDataList.DataSource = r;
myDataList.DataBind();
我使用 Reponse 对象来完成这项工作:
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 工作表始终为空。请帮我解决这个问题。
编辑添加 - 用于重写 URL 的 WCF 站点的 IHttpModule 被调用两次或三次。这可能是因为 aspnet_wp 回收吗?在这种情况下,我应该在应用程序事件日志中看到错误,对吧?但我不这么认为。请帮我解决这个问题。
这是我的自定义 HttpModule: 公共类 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 被调用了两次。我认为这就是为什么我看到操作超时或连接关闭异常的原因。我怎样才能阻止它重复两次。我只创建一个请求。
I need to provide an export to excel feature for a large amount of data returned from a WCF web service.
The code to load the datalist is as below:
List<resultSet> r = myObject.ReturnResultSet(myWebRequestUrl); //call to WCF service
myDataList.DataSource = r;
myDataList.DataBind();
I am using the Reponse object to do the job:
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();
The problem is that WCF Service times out for large amount of data (about 5000 rows) and the result set is null. When I debug the service, I can see the window for saving/opening the excel sheet appear before the service returns the result and hence the excel sheet is always empty. Please help me figure this out.
EDITED TO ADD - WCF site's IHttpModule used to rewriting the URL is being called twice or thrice. Could this be because of a aspnet_wp recycle? In that case I should be seeing the error on my Application Event Log, right? But I don't. Please help me with this issue.
Here is my custom 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);
}
};
}
}
I see that appln.AuthorizeRequest is called twice. I think this is why I am seeing the operation time out or the connection closed exception. How do I stop it from doing it twice. I only create one request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到了多种 WCF/IIS 超时和限制之一。这一特定的可能是 MaxReceivedMessageSize。默认值为 64 KB。在服务的绑定上配置此项。
其他限制包括(这些并非全部绑定参数):
You've run into one of many kinds of WCF/IIS timeouts and limits. This particular one may be MaxReceivedMessageSize. Default is 64 KB. Configure this on the service's binding.
Other limits are (these aren't all binding parameters):
Tor的回答对我有一点帮助。我必须将 MaxItemsInObjectGraph 设置为更高的值才能消除此异常。但是,我在设置这个值时遇到了麻烦,因为我不知道如何设置它以及在哪里设置它。
这文章帮助我了解了有关 WCF Rest 服务和限制的更多信息。真正对我有用的是设置 ServiceBehavior 属性对于我的服务级别。
{
巴拉...
如果
您不担心必须一遍又一遍地更改最大限制,您可能会很高兴在代码中指定它,并乐于看到它全部工作。
Tor's response helped me a little bit. I had to set the MaxItemsInObjectGraph to a higher value for this exception to go away. However, I had trouble setting this value because I didn't how to set it and where to set it.
This article helped me understand more about WCF Rest service and throttling. What actually worked for me was setting the ServiceBehavior attribute for my service class.
{
blah...
}
If you aren't concerned about having to change the max limit over and over, you could be happy with specifying it in code and have the fun in seeing it all working.