i'm trying to set up an WCF-Web that returns data from the database as Streamed Response to the Client. The method returns 45.000 Items currently.
My problem is, that when i try to call this method a couple of times, some requests will succeed, while the next call dies with an TimeoutException - seems like the client is not able to establish a new connecton to the host. If i change the TransferMode to "Buffered" it will work like intended.
The amount of successfully processed calls can be increased by increasing the System.Net.ServicePointManager.DefaultConnectionLimit to a larger Number.
It seems to me, that the client does not close the connection? But why only in StreamedResponse mode? Where's the mistake? Any idea?
Source:
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();
}
Client-Binding:
<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="2147483开发者_运维知识库647"
/>
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
精彩评论