Microsoft SharePoint and .NET Technology Insight

SharePoint, .NET, Office365, Windows Azure, TFS, Project Server and SQL Server Technology Insights


Leave a comment

WCF MaxItemsInObjectGraph quota

The following errors often show up when trying to return a large number of items in a generic List<T> from a WCF service:

WCF System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly

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.

The  resolution for this issue is ensuring following behaviour for the dataContractSerializer in both the client and service configuration files..

<dataContractSerializer maxItemsInObjectGraph=”2147483646″/>

Use the MaxItemsInObjectGraph property set a limit on the number of items in an object graph that are serialized. You can also set this property using the <dataContractSerializer> element in an application configuration file.

The following provides an example of settings in the service layer and client layer:

Settings in the service layer:

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name=”DefaultBehavior” MaxItemsInObjectGraph=”2147483647″>
<dataContractSerializer maxItemsInObjectGraph=”2147483647″ />
<serviceMetadata httpGetEnabled=”true” />
<serviceDebug includeExceptionDetailInFaults=”true” />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration=”DefaultBehavior” name=”MyService”>
<endpoint address=”” binding=”wsHttpBinding” bindingConfiguration=”WSHttpBinding_MyService” contract=”IMyService”>
<identity>
<dns value=”localhost” />
</identity>
</endpoint>
<endpoint address=”mex” binding=”mexHttpBinding” contract=”IMetadataExchange” />
</service>
</services>
</system.serviceModel>

Settings in the client layer:

<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name=”ClientBehavior”>
<dataContractSerializer maxItemsInObjectGraph=”2147483647″/>
</behavior>
</endpointBehaviors>
</behaviors>

<client>
<endpoint address=http://localhost/MyService.svc
binding=”wsHttpBinding” bindingConfiguration=”WSHttpBinding_MyServiceConfiguration”
contract=”ServiceReferences.IMyService” name=”WSHttpBinding_MyService”
behaviorConfiguration=”ClientBehavior”>
<identity>
<dns value=”localhost” />
</identity>
</endpoint>
</client>
</system.serviceModel>

See the article below for additional information on the ServiceBehaviorAttribute.MaxItemsInObjectGraph Property:

http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.maxitemsinobjectgraph.aspx

See the article below for additional information on extending WCF wit Custom Behaviors:

http://msdn.microsoft.com/en-us/magazine/cc163302.aspx


Leave a comment

Adding Diagnostic information for WCF Services

Here is diagnostic information in the web.config for Web Hosted WCF services:

<!– child of the <configuration> element –>

<system.diagnostics>

<sources>

<source name=”System.ServiceModel”

switchValue=”All”>

<listeners>

<add name=”xmlTraceListener” />

</listeners>

</source>

<source name=”System.ServiceModel.MessageLogging”

switchValue=”All”>

<listeners>

<add name=”xmlTraceListener” />

</listeners>

</source>

</sources>

<sharedListeners>

<add name=”xmlTraceListener”

type=”System.Diagnostics.XmlWriterTraceListener”

initializeData=”ClientLogBasic.svclog” />

</sharedListeners>

<trace autoflush=”true” />

</system.diagnostics>

<!– child of the <system.serviceModel> element –>

<diagnostics>

<messageLogging maxMessagesToLog=”10000″

logEntireMessage=”true”

logMessagesAtServiceLevel=”true”

logMalformedMessages=”true”

logMessagesAtTransportLevel=”true”>

<filters>

<clear/>

</filters>

</messageLogging>

</diagnostics>