我正在尝试设置一个WCF-Web,它将数据从数据库作为流响应返回给客户端。该方法当前返回45.000项。
我的问题是,当我尝试多次调用此方法时,一些请求将成功,而下一个调用因TimeoutException而死 - 似乎客户端无法建立到主机的新连接。如果我将TransferMode更改为“Buffered”,它将按预期工作。
通过将System.Net.ServicePointManager.DefaultConnectionLimit增加到更大的Number,可以增加成功处理的调用量。
在我看来,客户端没有关闭连接?但为什么只能在StreamedResponse模式下?哪里出错了?任何的想法?
来源:
public static void DoCall() {
var factory = new ChannelFactory<ICacheService>("ICacheService", new EndpointAddress(...));
ICacheService service = factory.CreateChannel();
try
{
var itemsMessage = service.DoStreaming(
Message.CreateMessage(MessageVersion.Soap11, "DoStreaming", "SomeArgumnt")
);
foreach (Item item in GetAllItems(itemsMessage))
{
...
}
var serviceChannel = ((IServiceChannel) service);
if (serviceChannel.State != System.ServiceModel.CommunicationState.Faulted)
serviceChannel.Close();
itemsMessage.Close();
((IServiceChannel)service).Close();
}
catch (Exception ex)
{
((IServiceChannel) service).Abort();
}
factory.Close();
}
private static IEnumerable<Item> GetAllItems(System.ServiceModel.Channels.Message message)
{
XmlReader reader = message.GetReaderAtBodyContents();
if (reader.LocalName != "results")
{
throw new Exception("The service returned an invalid message");
}
XmlSerializer serializer = new XmlSerializer(typeof(Item));
reader.ReadStartElement("results");
while (!reader.EOF && reader.LocalName == "Item")
{
yield return (Item)serializer.Deserialize(reader);
}
reader.ReadEndElement();
}
客户装订:
<binding
name="BasicHttpBinding_ICacheService"
openTimeout="00:01:00"
receiveTimeout="00:01:00"
sendTimeout="00:10:00"
maxReceivedMessageSize="1000000000"
transferMode="Buffered"
>
<readerQuotas
maxDepth="2147483647"
maxStringContentLength="2147483646"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647"
/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>