为WCF服务客户端增加MaxItemsInObjectGraph

时间:2012-10-10 13:46:11

标签: c# wcf

我从C#dll引用WCF服务,因此生成的app.config文件未被读取。我手动尝试通过以下代码创建服务客户端;但是,我得到的错误是我需要增加MaxItemsInObjectGraph。正在运行的服务已经设置为int.MaxValue所以我现在只需要在TestServiceClient中增加它。有任何想法吗??提前谢谢!

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

以下是我的解决方案:

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

1 个答案:

答案 0 :(得分:2)

尝试使用client.Endpoint.Contract.Operations

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