我有一个WCF Web服务,它将DataTable发送(返回)给客户端。现在我在数据表中有47,000条记录。在第一次打电话时,它花了太多时间来获取数据。
我已将生成序列化程序集设置为“ on ”并在配置文件中尝试 useDefaultWebProxy =“false”,但问题无法解决
这是我的app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_I123Services" closeTimeout="00:30:00"
openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="500000000"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="false"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2000000"
maxBytesPerRead="2000000" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:30:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://123.com/123services.svc" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_I123Services" contract="123Services.I123Services"
name="WSHttpBinding_I123Services" />
</client>
</system.serviceModel>
</configuration>
任何人都可以帮我找到解决方案。 谢谢。
答案 0 :(得分:1)
我已通过以下方式解决了这个问题,
数据以字节数组形式返回,而不是带压缩的DataTable。刚刚使用了以下部分。
public byte[] ToByteArray(object o)
{
if (o == null)
return new byte[0];
using (MemoryStream outStream = new MemoryStream())
{
using (GZipStream zipStream = new GZipStream(outStream, CompressionMode.Compress))
{
using (MemoryStream stream = new MemoryStream())
{
new BinaryFormatter().Serialize(stream, o);
stream.Position = 0;
stream.CopyTo(zipStream);
zipStream.Close();
return outStream.ToArray();
}
}
}
}
public object ToObject(byte[] byteArray)
{
if (byteArray.Length == 0)
return null;
using (MemoryStream decomStream = new MemoryStream(byteArray), ms = new MemoryStream())
{
using (GZipStream hgs = new GZipStream(decomStream, CompressionMode.Decompress))
{
hgs.CopyTo(ms);
decomStream.Close();
hgs.Close();
ms.Position = 0;
return new BinaryFormatter().Deserialize(ms);
}
}
}
希望它可以帮助任何人。
答案 1 :(得分:0)
首先,通过从服务操作返回数据库,您肯定会扩大响应大小。请参阅http://www.hanselman.com/blog/ReturningDataSetsFromWebServicesIsTheSpawnOfSatanAndRepresentsAllThatIsTrulyEvilInTheWorld.aspx。
其次,您期望通过网络返回47K行数据?这有很多序列化开销。我很惊讶你的电话只需要26秒才能回来。为什么需要将47K行数据加载到客户端?
听起来你已经在两件事之间设置了服务边界,而这两件事应该真正在彼此之间进行。在内存中,这么多数据需要几毫秒才能返回。