【发布时间】:2012-09-24 00:37:07
【问题描述】:
该问题最初是在 WCF 数据服务 (OData) 上遇到的,其中返回大量数据 (~3MB) 并且使用数据的应用程序(MVC 网站)偶尔会挂在一些请求上,并且会最终超时。
可以在较小的数据负载上复制该问题,但需要很长时间才能发生。该问题已在 Cassini 和 IIS 7 上重现。通过 IIS 运行时,我可以看到挂起的 W3 工作进程卡在“SendResponse”状态,直到超时。
为了缩小问题的原因,我创建了一个控制台应用程序,该应用程序将按顺序从 ASP.NET 站点下载内容。该问题已在 WCF 数据服务 (OData)、标准 WCF 服务和将类似数据直接输出到 HttpResponse 对象的 Web 窗体页面上复制。使用的示例代码使用标准 WCF 服务作为示例(以便于那些试图复制它的人进行设置)。
控制台应用客户端:
static void Main(string[] args)
{
Console.ReadKey(); // press any key to start test
do
{
var req = WebRequest.Create(new Uri("http://localhost:26332/WCFTest.svc/GetData"));
var response = req.GetResponse();
using (var stream = response.GetResponseStream())
{
var content = new StreamReader(stream).ReadToEnd();
}
} while (true);
}
WCF 服务:
public class Service1 : IService1
{
public TestClass[] GetData()
{
return Enumerable
.Range(0, 15000)
.Select(i => new TestClass{ ID = "1" })
.ToArray();
}
}
[DataContract]
public class TestClass
{
[DataMember]
public string ID { get; set; }
}
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "GetData")]
TestClass[] GetData();
}
在对 Web 服务进行多次调用(每次运行时不同,但通常少于 30 次)后,GetResponseStream 调用将挂起。
对于可能出现的问题或如何缩小问题原因的任何想法将不胜感激!
服务 web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfService2.Service1" >
<endpoint
address=""
binding="webHttpBinding"
contract="WcfService2.IService1"
behaviorConfiguration="webHttp"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
WCF 数据服务代码:
public class Service1 : DataService<ODataContext>
{
public static void InitializeService(DataServiceConfiguration configuration)
{
configuration.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
}
}
public class ODataContext
{
public IQueryable<TestClass> Test
{
get
{
return Enumerable
.Range(0, 15000)
.Select(i => new TestClass {ID = "1"})
.AsQueryable();
}
}
}
public class TestClass
{
public string ID { get; set; }
}
【问题讨论】:
-
按要求添加了 WCF 服务 web.config。标准 web.config 的唯一补充是 webHttp 行为。
-
如果您有可用的代码,我将尝试在 WCF 数据服务上进行复制;可能看起来与上面的非常相似。顺便说一句,您尝试了哪个版本的 WCF 数据服务? (根据您对问题的描述,我并不希望这会有所作为。)
-
@MarkStafford-MSFT 我在 WCF 数据服务服务器的 5.0.1 版上进行了尝试。我已更新问题以包含代码。
-
嗯...我做了一些微不足道的修改(命名空间等)并从控制台应用程序循环运行它 - 超过一千次迭代没有挂起。我根本没有修改我的 Web.config。会不会是环保的?网络?主持人?我只是在我的本地机器上玩 Win8/VS2012/IISExpress。
-
@MarkStafford-MSFT 看起来应该归咎于公司的防病毒软件。感谢您花时间尝试复制问题,并很抱歉浪费您的时间!