博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WCF - MaxStringContentLength & MaxReceivedMessageSize
阅读量:5856 次
发布时间:2019-06-19

本文共 3127 字,大约阅读时间需要 10 分钟。

[ServiceContract]
public 
interface IService
{
[OperationContract]
void Test(
string s);
}
public 
class Service : IService 
{
public 
void Test(
string s)
{
Console.WriteLine(s.Length);
}
}
public 
class WcfTest
{
public 
static 
void Test()
{
AppDomain.CreateDomain(
"
Server
").DoCallBack(
delegate
{
ServiceHost host = 
new ServiceHost(
typeof(Service), 
new Uri(
"
net.tcp://localhost:8080/service
"));
host.AddServiceEndpoint(
typeof(IService), 
new NetTcpBinding(), 
"");
host.Open();
});
IService channel = ChannelFactory<IService>.CreateChannel(
new NetTcpBinding(), 
new EndpointAddress(
"
net.tcp://localhost:8080/Service
"));
using (channel 
as IDisposable)
{
channel.Test(
new String(
'
a
', 
1024 * 
10));
}
}
}

 

 

 

 运行一下,目标出现~~~ 

  未处理 System.ServiceModel.FaultException

 Message="The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Test'. The maximum string content length quota (8192) has been exceeded while reading <href="http://tech.ddvip.com/web/xml/index.html" target="_blank">XML</a> data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader."

既然已经有了详细提示,那么我们就按照提示做。 

 

 AppDomain.CreateDomain("Server").DoCallBack(delegate

{
  XmlDictionaryReaderQuotas quotas = 
new XmlDictionaryReaderQuotas();
  quotas.MaxStringContentLength = 
6553500;
  NetTcpBinding binding = 
new NetTcpBinding();
  binding.ReaderQuotas = quotas;
  ServiceHost host = 
new ServiceHost(
typeof(Service), 
new Uri(
"
net.tcp://localhost:8080/service
"));
  host.AddServiceEndpoint(
typeof(IService), binding, 
"");
  host.Open();
});
IService channel = ChannelFactory<IService>.CreateChannel(
new NetTcpBinding(), 
  
new EndpointAddress(
"
net.tcp://localhost:8080/Service
"));
  
using (channel 
as IDisposable)
{
  channel.Test(
new String(
'
a
', 
1024 * 
10));
}

 

 OK!可以运行了。我们继续加大传递的字符串。 

 IService channel = ChannelFactory<IService>.CreateChannel(new NetTcpBinding(), 

  
new
 EndpointAddress(
"net."
));
  
using
 (channel 
as
 IDisposable)
{
  channel.Test(
new
 String(
'a'
, 1024 * 100));
}

 

 哎~~ 新的异常出现了。 

  未处理 System.ServiceModel.CommunicationException

  Message="The maximum message size quota for incoming messages has been exceeded for the remote channel. See the server logs for more details."

  看异常提示,这回要改的是 channel 的信息。

  

AppDomain.CreateDomain(
"
Server
"
).DoCallBack(
delegate 

{

  XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
  quotas.MaxStringContentLength = 6553500;
  NetTcpBinding binding = new NetTcpBinding();
  binding.ReaderQuotas = quotas;
  binding.MaxReceivedMessageSize = 6553500; // <---- Here!--------------
  ServiceHost host = new ServiceHost(typeof(Service), new Uri("net.tcp://localhost:8080/service"));
  host.AddServiceEndpoint(typeof(IService), binding, "");
  host.Open();
});
IService channel = ChannelFactory<IService>.CreateChannel(new NetTcpBinding(), 
  new EndpointAddress("net.tcp://localhost:8080/Service"));
  
using (channel as IDisposable)
{
  channel.Test(new String('a', 1024 * 100));
}

转载地址:http://xaojx.baihongyu.com/

你可能感兴趣的文章
RvmTranslator 3.1 is released
查看>>
WCF的三个名称/命名空间,你是否傻傻分不清楚?
查看>>
百分点:守住电商后门 推荐引擎让大数据快变现
查看>>
外媒:中国TA459 APT组织利用CVE-2017-0199攻击顶级金融公司
查看>>
解析深度学习的未来十大趋势
查看>>
影响百万人就业、价值6万亿的“大生意”是这样落地的
查看>>
警惕“办公室之狼”:企业文印安全亟待保护
查看>>
Uber要共享匿名用户数据,滴滴呢?
查看>>
阿里正式发布《Java开发手册》终极版!
查看>>
赛门铁克:疑有国家背景的黑客小组攻击中俄等国
查看>>
中国人工智能学会通讯——深度学习的迁移模型 四、迁移学习应用案例
查看>>
中桥国际:如何应对客户端计算趋势
查看>>
《PIC微控制器项目设计:C语言》一2.6.3 while语句
查看>>
走在专家的路上,每天一条SQL优化(2)
查看>>
云栖长卷:一张图看懂云栖七年
查看>>
关于iOS系统“Trident”漏洞情况的通报
查看>>
飞利浦的选择:传统IT系统迁移到云平台
查看>>
Ubuntu支持LinuxONE大型机:为云而生的强强新组合
查看>>
英特尔至强E7 v4上市,剑指Power
查看>>
IT部门不应该推迟的10个项目
查看>>