zoukankan      html  css  js  c++  java
  • WCF MaxStringContentLength & MaxReceivedMessageSize

    [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.tcp://localhost:8080/Service"));
      
    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));
    }

  • 相关阅读:
    C#使用Json.Net遍历Json
    晓晨高效IP提取工具 附源码
    JavaScript原生秒表、计时器
    网盘直链工具 winform版 V1.0
    【架构】RPC 使用 Haproxy、keepalive作为负载均衡
    【架构】Google的大规模集群管理工具Borg
    【Networking】gRPC golang 相关资料
    【Linux】find grep 联合使用 过滤所有子目录、文件
    【Storage】Ubuntu LVM 安装配置
    【Ansible】SSH Error: ssh_exchange_identification: Connection closed by remote host
  • 原文地址:https://www.cnblogs.com/goody9807/p/2387467.html
Copyright © 2011-2022 走看看