WCF MaxItemsInObjectGraph setting not working

安稳与你 提交于 2019-12-04 11:13:05

问题


I have been getting the following error trying to access my WCF service.

'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

Doing some research, it looks like all I need to do is update this setting to be a higher value. This is what I am trying to do, but the setting does not seem to be getting read from the configuration. I keep getting the same exception with the 65536 value in it.

I followed the instructions found at this Link, but am having no luck.

Here is what I have configured on the WCF Service's Web.Config.

    <behaviors>
        <serviceBehaviors>
            <behavior name="metadataBehavior">
                <serviceMetadata httpGetEnabled="true"  httpGetUrl="" />
                <serviceDebug includeExceptionDetailInFaults="false" />
                <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>

This is what is in the Client's app.config:

        <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="True" />
                <serviceDebug includeExceptionDetailInFaults="False" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior >
                <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
            </behavior>
        </endpointBehaviors>
    </behaviors>

And lastly, I have the following attribute on the WCF service itself:

[ServiceBehavior(MaxItemsInObjectGraph = 2147483646, IncludeExceptionDetailInFaults = true)]

Despite the configurations above, I still get an Exception complaining about the 65536 value. Why aren't any of these settings being used by the applications? Is there something else that needs to be set somewhere?


回答1:


You were on the right track! All you had to do was add a name to the behavior

<behavior name="MyBehavior">
    <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
 </behavior>

And then on the end point add

<endpoint .... behaviorConfiguration="MyBehavior"/>



回答2:


Had to go nuclear and update that machine.config;

Directions Here

The gist of it is to add the following to the "system.serviceModel" section.

    <commonBehaviors>
      <endpointBehaviors>
        <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      </endpointBehaviors>
      <serviceBehaviors>
         <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      </serviceBehaviors>
    </commonBehaviors>



回答3:


I wrote a program to modify the machine configs for this, because support. It works for me, but I haven't done tons of testing.

using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace FixMachineConfigBehavior
{
    class Program
    {
        public static XElement IfNotExistsAdd(XDocument xd, XElement rootElement, string childName, XElement newChild)
        {
            if (rootElement.Elements(childName).Count() == 0)
            {
                Console.WriteLine("  adding " + childName + " node...");
                rootElement.Add(newChild);
            }

            return rootElement.Element(childName);
        }

        static void Main(string[] args)
        {
            foreach (var file in Directory.EnumerateFiles(Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\","machine.config",SearchOption.AllDirectories))
            {
                Console.WriteLine("fixing: " + file);

                TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
                double ms = t.TotalMilliseconds;

                File.Copy(file, file + "." + ms + ".bak", true);

                var xd = XDocument.Load(file);

                XElement i = xd.Root;
                i = IfNotExistsAdd(xd, i, "system.serviceModel", new XElement("system.serviceModel"));
                i = IfNotExistsAdd(xd, i, "commonBehaviors", new XElement("commonBehaviors"));
                i = IfNotExistsAdd(xd, i, "endpointBehaviors", new XElement("endpointBehaviors"));
                i = IfNotExistsAdd(xd, i, "dataContractSerializer", new XElement("dataContractSerializer", new XAttribute("maxItemsInObjectGraph", Int32.MaxValue)));

                xd.Save(file);
            }

            Console.ReadLine();
        }
    }
}



回答4:


I had the same problem and tried several options but I found the solution here: https://msdn.microsoft.com/en-us/library/ms732038.aspx

In "Controlling the serialization process".

Adding ...

[ServiceBehavior(MaxItemsInObjectGraph=100000)] class My Service ...

good luck




回答5:


I had the same issue , There was some enums in returning class. What found out they cannot be null. Check whether you have any Enums that are to be returned.



来源:https://stackoverflow.com/questions/17622408/wcf-maxitemsinobjectgraph-setting-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!