WCF MaxItemsInObjectGraph setting not working

拟墨画扇 提交于 2019-12-03 06:16:24

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"/>

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>

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();
        }
    }
}

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

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.

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