【发布时间】: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();
我尝试查找maxReceivedMessageSize、maxStringContentLength 和maxArrayLength 属性,但我只找到maxReceivedMessageSize(我在没有app.config 文件的情况下工作)
【问题讨论】:
-
我没有,我在我的 Winforms gui 中使用 WCF 服务
-
首先我喜欢评论 没有任何进一步的解释为什么。那太棒了。此外,您应该将 WCF 配置添加到您的
app.config,这使得配置变得更加容易(是的,即使您自己托管服务也可以这样做)。第三,您可能想要使用流服务,它允许您通过流传输大量数据。这个问题似乎总结得很好:stackoverflow.com/questions/14479885/… -
如何将 WCF 配置添加到我的 app.config 中?
-
你看到我评论中的链接了吗?有一个例子。基本上,您将
ServiceConfiguration添加到您配置绑定、绑定配置等的app.config,然后您需要做的就是使用typeof(<ServiceClassName>)参数调用ServiceHost构造函数。否则有很多教程,你可以很容易地用谷歌搜索。