【问题标题】:WCF And Large Amounts Of DataWCF 和大量数据
【发布时间】:2014-02-18 09:08:18
【问题描述】:

我希望能够向我的WCF service 发送大量数据,我发送的是客户端收到的List<byte[]>,目前我的应用程序收到一个错误,即套接字中止。

所以我找到了这个帖子:http://geekswithblogs.net/tmurphy/archive/2011/08/15/sending-large-arrays-with-wcf.aspx

这就是我建立连接的方式:

string urlService = "net.tcp://localhost:8000/myApp";
ServiceHost host = new ServiceHost(typeof(pp.classes.service1));
host.Opening += new EventHandler(host_Opening);
host.Opened += new EventHandler(host_Opened);
host.Closing += new EventHandler(host_Closing);
host.Closed += new EventHandler(host_Closed);

NetTcpBinding tcpBinding = new NetTcpBinding();
tcpBinding.TransactionFlow = false;
tcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
tcpBinding.Security.Mode = SecurityMode.None; // <- Very crucial
pp.classes.service1 ser = new pp.classes.service1();
host.AddServiceEndpoint(typeof(pp.classes.IService1), tcpBinding, urlService);
ServiceMetadataBehavior metadataBehavior;
metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
    // Create the proxy object that is generated via the svcutil.exe tool
    metadataBehavior = new ServiceMetadataBehavior();
    metadataBehavior.HttpGetUrl = new Uri("http://localhost:8001/myApp");
    metadataBehavior.HttpGetEnabled = true;
    metadataBehavior.ToString();
    host.Description.Behaviors.Add(metadataBehavior);
    string urlMeta = metadataBehavior.HttpGetUrl.ToString();
}

host.Open();

我尝试查找maxReceivedMessageSizemaxStringContentLengthmaxArrayLength 属性,但我只找到maxReceivedMessageSize(我在没有app.config 文件的情况下工作)

【问题讨论】:

  • 我没有,我在我的 Winforms gui 中使用 WCF 服务
  • 首先我喜欢评论 没有任何进一步的解释为什么。那太棒了。此外,您应该将 WCF 配置添加到您的 app.config,这使得配置变得更加容易(是的,即使您自己托管服务也可以这样做)。第三,您可能想要使用流服务,它允许您通过流传输大量数据。这个问题似乎总结得很好:stackoverflow.com/questions/14479885/…
  • 如何将 WCF 配置添加到我的 app.config 中?
  • 你看到我评论中的链接了吗?有一个例子。基本上,您将ServiceConfiguration 添加到您配置绑定、绑定配置等的app.config,然后您需要做的就是使用typeof(&lt;ServiceClassName&gt;) 参数调用ServiceHost 构造函数。否则有很多教程,你可以很容易地用谷歌搜索。

标签: c# wcf


【解决方案1】:

在代码中使用此配置。它将读者配额设置为最大值:

XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
quotas.MaxDepth = 2147483647;
quotas.MaxStringContentLength = 2147483647;
quotas.MaxArrayLength = 2147483647;
quotas.MaxBytesPerRead = 2147483647;
quotas.MaxNameTableCharCount = 2147483647;
tcpBinding.ReaderQuotas = quotas;

使用此设置超时。它将超时设置为 5 分钟。

tcpBinding.ReceiveTimeout = new TimeSpan(0, 5, 0);
tcpBinding.SendTimeout = new TimeSpan(0, 5, 0);

【讨论】:

  • 复制这段代码,放在tcpBinding旁边?
  • 您应该将它与您当前的绑定集成。设置绑定的Quotas
  • 你能告诉我怎么做吗? (我是新开发者...)
  • 仍然附加信息:套接字连接已中止。这可能是由于处理您的消息时出错或远程主机超出接收超时,或者是潜在的网络资源问题引起的。本地套接字超时为“00:00:59.9710000”。
  • 你必须把它放在服务器和客户端配置上。
猜你喜欢
  • 1970-01-01
  • 2012-04-29
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 2012-01-03
  • 2010-10-10
  • 2011-02-15
相关资源
最近更新 更多