最近在用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
麻烦高人指点下。#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
<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>
string Sqlstring = "select top 1 Id,Title,Author,Content,ReleaseTime from infocenter order by ReleaseTime Desc ";
return SQLHelper.QueryReturnTable(connectionString, Sqlstring).Tables[0];
你不公开那部分代码的话,我只能给点建议性的方案,检查下数据读取是否是按照对象读取的,有可能是手动组装的对象,以Byte[]方式序列化后传输,那么就有可能在数据尚未完整接收就反序列化。按理说使用微软自己的方法序列化传输是不会有这种奇怪的错误提示的。
{
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();
}
}
CLR 无法从 COM 上下文 0x3ca9f8 转换为 COM 上下文 0x3cab68,这种状态已持续 60 秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作。这种情况通常会影响到性能,甚至可能导致应用程序不响应或者使用的内存随时间不断累积。要避免此问题,所有单线程单元(STA)线程都应使用泵式等待基元(如 CoWaitForMultipleHandles),并在运行时间很长的操作过程中定期发送消息。
服务端获取到的数据量太大,造成WCF在反序列化的过程中几乎处于死循环。你可以看下,服务端的数据量是不是很大。减少一点数据量就可以了。