WCF 通讯 数据多了出错

cari_ayong 2008-07-08 08:11:31
service :

[DataContract]
class Contact
{
string m_FirstName;
IPAddress m_LastName;

[DataMember]
public string FirstName
{
get
{
return m_FirstName;
}
set
{
m_FirstName = value;
}
}
[DataMember]
public IPAddress LastName
{
get
{
return m_LastName;
}
set
{
m_LastName = value;
}
}
}


[ServiceContract]
interface IContactManager
{
[OperationContract]
void AddContact(List <Contact> contact);

[OperationContract]
Dictionary <int,List <Contact>> GetContacts();
}

class ContactManager : IContactManager
{
Dictionary <int,List <Contact>> m_Contacts = new Dictionary <int,List <Contact>> ();

public void AddContact(List <Contact> contact)
{
for (int i = 0; i < 2000; i++)
{
m_Contacts.Add(i,contact);

}
}

public Dictionary <int, List <Contact>> GetContacts()
{

return m_Contacts;
}
}





client:

Contact contact = new Contact();
contact.FirstName = "Juval";
contact.LastName = IPAddress.Parse( "192.168.12.1");

List <Contact> contactList = new List <Contact>();
contactList.Add(contact);
ContactManagerClient proxy = new ContactManagerClient();
proxy.AddContact(contactList);
Dictionary <int, List <Contact>> colelction = proxy.GetContacts();

proxy.Close();





config:

<system.serviceModel>
<services>
<service name = "ContactManager">
<endpoint
address = "http://localhost:8000/ContactManager"
binding = "wsHttpBinding" bindingConfiguration="NewBindingStationCnfg"
contract = "IContactManager">
</endpoint>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="NewBindingStationCnfg" maxReceivedMessageSize="2147483647" />
</wsHttpBinding>

</bindings>
</system.serviceModel>





for (int i = 0; i < 2000; i++)
{
m_Contacts.Add(i,contact);
}
i=2000 的时候没问题,当把它改为5000的时候出错,出错信息 "基础连接已经关闭: 接收时发生错误

接收对 http://localhost:8000/ContactManager/ 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致

整个解决方案可以告诉我邮箱,我发给您!
...全文
1323 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
leishiyuning 2012-07-09
  • 打赏
  • 举报
回复
十分感谢 十二分感谢
zqs1002 2011-09-21
  • 打赏
  • 举报
回复
mark
dulei123321 2011-03-25
  • 打赏
  • 举报
回复
恩 改下接受数据量大小就OK了
编程爱好者L 2011-03-25
  • 打赏
  • 举报
回复
解決了 都不結貼的...
hbhdyn 2010-03-17
  • 打赏
  • 举报
回复
学习了MaxItemsInObjectGraph
xzw561219 2009-06-11
  • 打赏
  • 举报
回复
问题解决,谢谢你
honglian2008 2009-02-05
  • 打赏
  • 举报
回复
太谢谢啦,困扰我N久的问题终于解决了,呵呵呵.....
zero8500 2008-10-10
  • 打赏
  • 举报
回复
学习学习
sxmonsy 2008-10-10
  • 打赏
  • 举报
回复
学习学习
felexcheng 2008-10-10
  • 打赏
  • 举报
回复
感谢zzqqqzzz ,高手阿,敬佩
ChinaLyp 2008-09-27
  • 打赏
  • 举报
回复
to zzqqqzzz:
谢谢。也帮我搞定了。。。
zzqqqzzz 2008-09-01
  • 打赏
  • 举报
回复
你可以用SvcConfigEditor打开服务端和客户端的log,然后用svcTracViewer打开log,你会看到服务端有这样一个异常:
There was an error while trying to serialize parameter http://tempuri.org/:GetContactsResult. The InnerException message was 'Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Change the object graph or increase the MaxItemsInObjectGraph quota. '. Please see InnerException for more details.

解决方法:
用SvcConfigEditor为客户端和服务端都加上一个behavior (服务端添加servicehehavior, 客户端添加endpointbehavior),然后为这两个服务各添加一个dataContractSerializer,把他的maxItemsInObjectGraph字段变成更大的数。我已经调试通过你的代码了。

这里是我这里服务端添加的log:
<system.serviceModel>
<diagnostics>
<messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true" />
</diagnostics>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8000/ContactManager/md" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="65536000" /> </behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior" name="WcfServiceLibrary1.ContactManager">
<endpoint address="http://localhost:8000/ContactManager" binding="wsHttpBinding"
bindingConfiguration="NewBindingStationCnfg" contract="WcfServiceLibrary1.IContactManager" />
<endpoint address="http://localhost:8000/ContactManager/mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="NewBindingStationCnfg" maxReceivedMessageSize="483647" />
</wsHttpBinding>

</bindings>
</system.serviceModel>


这里是我客户端的config:

<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="NewBehavior">
<dataContractSerializer maxItemsInObjectGraph="6553600" />
</behavior>
</endpointBehaviors>
</behaviors>
<diagnostics>
<messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true" />
</diagnostics>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IContactManager1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/ContactManager" behaviorConfiguration="NewBehavior"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IContactManager1"
contract="ServiceReference2.IContactManager" name="WSHttpBinding_IContactManager1">
<identity>
<userPrincipalName value="vblab1@redmond.corp.microsoft.com" />
</identity>
</endpoint>
</client>
</system.serviceModel>
LQknife 2008-08-21
  • 打赏
  • 举报
回复
晓习,帮顶

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧