简体   繁体   中英

Increase MaxItemsInObjectGraph for WCF Service client

I am referencing a WCF service from a C# dll so the app.config file generated is not being read. I am manually trying to create a service client via the code below; however, I am getting errors that I need to increase the MaxItemsInObjectGraph. The service that is running is already set to int.MaxValue so I just need to increase it in the TestServiceClient now. Any ideas?? Thanks in advance!

var client = new TestServiceClient(GetBinding(), GetEndpointAddress());

private static EndpointAddress GetEndpointAddress()
        {
            var endpoint = new EndpointAddress("https://localhost:8000/ServiceModel/service");

            return endpoint;
        }

        private static Binding GetBinding()
        {
            var basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport)
            {
                MessageEncoding = WSMessageEncoding.Text,
                TextEncoding = Encoding.UTF8,
                BypassProxyOnLocal = false,
                UseDefaultWebProxy = true,
                CloseTimeout = new TimeSpan(10, 0, 0),
                OpenTimeout = new TimeSpan(10, 0, 0),
                SendTimeout = new TimeSpan(10, 0, 0),
                ReceiveTimeout = new TimeSpan(10, 0, 0),
                HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
                MaxBufferPoolSize = Int32.MaxValue,
                MaxReceivedMessageSize = Int32.MaxValue,
                AllowCookies = false,
                TransferMode = TransferMode.StreamedResponse,
                ReaderQuotas =
                {
                    MaxDepth = 32,
                    MaxStringContentLength = Int32.MaxValue,
                    MaxArrayLength = 6553600,
                    MaxBytesPerRead = 4096,
                    MaxNameTableCharCount = 16384
                }
            };

            return basicHttpBinding;
        }

Below is my solution:

private static ITestServiceClient GetClient()
        {
            var factory = new ChannelFactory<ITestServiceClient >(GetBinding(), GetEndpointAddress());

            foreach (var dataContractBehavior in factory.Endpoint.Contract.Operations
                .Select(operation => operation.Behaviors.Find<DataContractSerializerOperationBehavior>())
                .Where(dataContractBehavior => dataContractBehavior != null))
            {
                dataContractBehavior.MaxItemsInObjectGraph = Int32.MaxValue;
            }

            var client = factory.CreateChannel();

            return client;
        }

Try in client.Endpoint.Contract.Operations

foreach (var operation in operations)
{
   var dataContractBehavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
   if (dataContractBehavior != null)
   {
      dataContractBehavior.MaxItemsInObjectGraph = value;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM