Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Author: Steve Gray Created: 4/12/2011 8:51 AM RssIcon
Tips, Tricks, Tutorials, and Code Examples for WCF
By Steve Gray on 4/12/2011 8:56 AM

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://v100k.Web.Services:GetListOfOrdersResult. 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.

I’m not sure that I’ve quite got the handle on the above issue, but I did get it fixed, and I’m going to put as much here as I can in case I run into it again.

The key was to add an EndpointBehavior to the app.config file and then to reference it in the Client Endpoint. Find both occurrences of ‘LargeObjectGraphBehavior in the code below:

 

Code Snippet
  1.   <system.serviceModel>
  2.     <behaviors>
  3.       <endpointBehaviors>
  4.         <behavior name="LargeObjectGraphBehavior">
  5.           <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
  6.         behavior>
  7.       endpointBehaviors>
  8.     behaviors>
  9.  
  10.     <bindings>
  11.           <wsHttpBinding>
  12.               <binding name="WSHttpBinding_IAccountingService" closeTimeout="00:01:00"
  13.                   openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
  14.                   bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
  15.                   maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
  16.                   messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
  17.                   allowCookies="false">
  18.                   <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647"
  19.                       maxBytesPerRead="4096" maxNameTableCharCount="16384" />
  20.                   <reliableSession ordered="true" inactivityTimeout="00:10:00"
  21.                       enabled="false" />
  22.                   <security mode="Transport">
  23.                       <transport clientCredentialType="Basic" proxyCredentialType="None"
  24.                           realm="" />
  25.                       <message clientCredentialType="Windows" negotiateServiceCredential="true" />
  26.                   security>
  27.               binding>
  28.           wsHttpBinding>
  29.       bindings>
  30.       <client>
  31.           <endpoint address=https://api.mobi/AccountingService.svc
  32.                 binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAccountingService"behaviorConfiguration="LargeObjectGraphBehavior"
  33.               contract="AccountingService.IAccountingService" name="WSHttpBinding_IAccountingService" />
  34.       client>
  35.   system.serviceModel>

 

Next, in code, I had to change this:

Code Snippet
  1. Dim oAccountingServiceClient As New AccountingService.AccountingServiceClient(myBinding, ea)

To this:

Code Snippet
  1. Dim oAccountingServiceClient As New AccountingService.AccountingServiceClient
  2. oAccountingServiceClient.Endpoint.Binding = myBinding
  3. oAccountingServiceClient.Endpoint.Address = ea

Apparently using the constructor wiped out the additional behavior.

I’m going include the entire method below in case I need it later… but the essentials are above

Code Snippet
  1. Private Sub btnGetOrders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetOrders.Click
  2.     Me.txtOutput.Text = ""
  3.     Me.Enabled = False
  4.     Me.Cursor = Cursors.WaitCursor
  5.  
  6.     ' Create the binding.
  7.     Dim myBinding As New ServiceModel.WSHttpBinding
  8.     myBinding.Security.Mode = ServiceModel.SecurityMode.Transport
  9.     myBinding.Security.Transport.ClientCredentialType = ServiceModel.HttpClientCredentialType.Basic
  10.     myBinding.MaxReceivedMessageSize = Int32.MaxValue
  11.     myBinding.MaxBufferPoolSize = Int32.MaxValue
  12.  
  13.     ' Create the endpoint address.
  14.     Dim ea As New ServiceModel.EndpointAddress("https://myService.svc")
  15.  
  16.     Dim oAccountingServiceClient As New AccountingService.AccountingServiceClient
  17.     oAccountingServiceClient.Endpoint.Binding = myBinding
  18.     oAccountingServiceClient.Endpoint.Address = ea
  19.  
  20.     oAccountingServiceClient.ClientCredentials.UserName.UserName = myUser
  21.     oAccountingServiceClient.ClientCredentials.UserName.Password = myPass
  22.  
  23.     Dim oResponse As New AccountingService.ServiceResponseOfAccountingOrder_PpHTjTPR
  24.     Dim oAccountingOrders() As AccountingOrder
  25.  
  26.     Try
  27.         oResponse = oAccountingServiceClient.GetListOfOrders(Me.dtOrdersDate.Value)
  28.  
  29.         If oResponse.responseCode.value <> ServiceResponseCode.Success Then
  30.             Throw New Exception(oResponse.messages.ToString)
  31.         End If
  32.  
  33.         'get the orders
  34.         oAccountingOrders = oResponse.returnValue
  35.  
  36.         'deserialize the orders into an XElement
  37.         Dim sw As New IO.StringWriter
  38.         Dim oSerializer As New Xml.Serialization.XmlSerializer(oAccountingOrders.GetType)
  39.  
  40.         oSerializer.Serialize(sw, (oAccountingOrders))
  41.         Dim xElem As XElement = XElement.Parse(sw.ToString)
  42.  
  43.         'display the xml in the form
  44.         Me.txtOutput.Text = xElem.ToString
  45.     Catch ex As Exception
  46.         MsgBox(ex.Message)
  47.     End Try
  48.  
  49.     Me.Enabled = True
  50.     Me.Cursor = Cursors.Default
  51.  
  52. End Sub
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us