最近在用WCF做后台发布信息的功能,但是在发布图片的时候出现了这样的异常。但是图片大小在30K左右的时候这样的异常就不会发生,测试过70K的图片文件都会发生这样的异常,由于使用的是Winform做后台,使用的是RichTextBox编辑内容,但是将图片保存入数据却没有问题,一旦读取就会出问题.以下是我的WCF配置文件和发送图片获取图片代码。
麻烦高人指点下。#region 获取数据以保存入数据库
            byte[] bWrite = System.Text.Encoding.Default.GetBytes(this.rtbContent.Rtf); //rtbContent是RichTextBox
            InfoCenterModel model = new InfoCenterModel();
            model.Author = AccountNo;
            model.Title = txtTitle.Text;
            model.BigType = cboMEA.Text.Trim();
            model.SmallType = cboTypeDetail.Text.Trim();
            model.Content = bWrite;
            model.Sourse = txtSourse.Text.Trim();
            
            m_proxy.AddInfo(model);            #endregion #region 读取数据以显示
            DataTable dt = null ;
            dt = m_proxy.QueryInfo();
            byte[] b = (byte[])dt.Rows[0]["Content"];
            
            string s = System.Text.Encoding.Default.GetString(b, 0, b.Length);
            this.rtbContent.Rtf = @s;
            #endregion

解决方案 »

  1.   

    配置文件服务端:<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding name="BindingBehaviorConfiguration" receiveTimeout="10:10:10"  maxBufferSize="2147483647"
              maxReceivedMessageSize="2147483647" transferMode="Streamed" maxBufferPoolSize="2147483647">
              <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" maxBytesPerRead="2147483647"/>
              <security mode="None">
                <transport clientCredentialType="Windows"/>
                <message clientCredentialType="Windows" />
              </security>
            </binding>
          </netTcpBinding>
        </bindings>
        <services>
          <service behaviorConfiguration="serviceBehavior" name="SunkeBest.Com.InfoSys.DataExchange.DataExchangeService">
            <endpoint address="DataExchangeService" binding="netTcpBinding"
              bindingConfiguration="BindingBehaviorConfiguration" contract="SunkeBest.Com.InfoSys.DataExchange.IDataExchangeService">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
              contract="IMetadataExchange">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="net.tcp://192.168.1.2:8999" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="serviceBehavior">
              <serviceMetadata/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
              <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>
    配置文件客户端:<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding name="NetTcpBinding_DataExchangeContract" closeTimeout="10:10:00"
                openTimeout="10:10:00" receiveTimeout="10:10:00" sendTimeout="10:10:00"
                transactionFlow="false" transferMode="Streamed" transactionProtocol="OleTransactions"
                hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10"
                maxReceivedMessageSize="2147483647">
              <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                  maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
              <reliableSession ordered="true" inactivityTimeout="10:30:00"
                  enabled="false" />
              <security mode="None">
                <transport clientCredentialType="Windows"/>
                <message clientCredentialType="Windows" />
              </security>
            </binding>
          </netTcpBinding>
        </bindings>
        <client>
          <endpoint address="net.tcp://192.168.1.2:8999/DataExchangeService"
              binding="netTcpBinding" bindingConfiguration="NetTcpBinding_DataExchangeContract"
              contract="DataExchangeContract" name="NetTcpBinding_DataExchangeContract">
            <identity>
              <dns value="localhost" />
            </identity>
          </endpoint>
        </client>
      </system.serviceModel>
    </configuration>
      

  2.   

    我都是内网测试的,将大文件保存入数据库速度都比较快,就是从数据库获取显示在richtextbox时,文件过大会报标题的异常
      

  3.   

    m_proxy.QueryInfo();这是什么呢?你贴的代码跟问题一点关系都没有,贴那么多没用。
      

  4.   

    通常,这是因为在服务器端存在非常严重的、造成进程崩溃的异常,服务器直接关闭了连接(而无法传递异常信息给客户端)。如果你仅用vs来开发,那是很不够的。调试时你在客户端看不到服务器端都出现了什么重大问题。.net的WCF配有专门的服务器调试工具,可以记录服务器端的异常信息,例如SvcTraceViewer等等。
      

  5.   

    那主要是查询数据的
     string Sqlstring = "select top 1 Id,Title,Author,Content,ReleaseTime from infocenter order by ReleaseTime Desc ";
                return SQLHelper.QueryReturnTable(connectionString, Sqlstring).Tables[0];
      

  6.   

    数据库相应字段用的是Image,网上查了可以存储2G的数据,应该不是这个的问题吧!
      

  7.   

    刚又测试了,存储大于4MB的图片都没问题,但是读取几十KB的文件都会报标题的错误!
      

  8.   

    又有新问题了。我将配置文件的transferMode="Streamed"  改为transferMode="Buffered" 读取文件大小 最大可以读取到约3MB的文件,一旦文件超过这个大小,就会出校OutOfMemory错误。 
      

  9.   

    m_proxy是什么类型的对象,它的那些方法都做了什么?
      

  10.   

    m_proxy是客户端代理,主要是调用远程服务,这里主要是对远程机器数据库进行插入与读取
      

  11.   

    你的配置文件没什么问题,就算有问题,错误信息也不是那样的,所以要检查m_proxy的实现过程。
    你不公开那部分代码的话,我只能给点建议性的方案,检查下数据读取是否是按照对象读取的,有可能是手动组装的对象,以Byte[]方式序列化后传输,那么就有可能在数据尚未完整接收就反序列化。按理说使用微软自己的方法序列化传输是不会有这种奇怪的错误提示的。
      

  12.   

    m_proxy.QueryInfo()QueryInfo()
    {
      string Sqlstring = "select top 1 Id,Title,Author,Content,ReleaseTime from infocenter order by ReleaseTime Desc ";
    return SQLHelper.QueryReturnTable(connectionString, Sqlstring).Tables[0];
    }
    客户端代理[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(Namespace = "http://SunkeSoft.Com.SunkeTrader/2010/01", ConfigurationName = "DataExchangeContract")]
    public interface DataExchangeContract
    {
    [System.ServiceModel.OperationContractAttribute(Action = "http://SunkeSoft.Com.SunkeTrader/2010/01/DataExchangeContract/QueryInfo", ReplyAction = "http://SunkeSoft.Com.SunkeTrader/2010/01/DataExchangeContract/QueryInfoResponse")]
        DataTable QueryInfo();
    }实现接口:
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public partial class DataExchangeContractClient : System.ServiceModel.ClientBase<DataExchangeContract>, DataExchangeContract
    {    public DataExchangeContractClient()
        {
        }    public DataExchangeContractClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
        {
        }    public DataExchangeContractClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }    public DataExchangeContractClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }    public DataExchangeContractClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
        {
        }
        public DataTable QueryInfo()
        {
            return base.Channel.QueryInfo();
        }

      

  13.   

    改一下你的客户端配置文件中的maxBytesPerRead="4096"呢?这个和服务端不一致,虽然不敢保证是否有效,不过可以尝试下。
      

  14.   

    改了,又出现以下异常:
    CLR 无法从 COM 上下文 0x3ca9f8 转换为 COM 上下文 0x3cab68,这种状态已持续 60 秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作。这种情况通常会影响到性能,甚至可能导致应用程序不响应或者使用的内存随时间不断累积。要避免此问题,所有单线程单元(STA)线程都应使用泵式等待基元(如 CoWaitForMultipleHandles),并在运行时间很长的操作过程中定期发送消息。
      

  15.   

    这个问题是由于你服务端的代码问题,
    服务端获取到的数据量太大,造成WCF在反序列化的过程中几乎处于死循环。你可以看下,服务端的数据量是不是很大。减少一点数据量就可以了。