C# 增加WCF服务客户端的MaxItemsInObjectGraph

C# 增加WCF服务客户端的MaxItemsInObjectGraph,c#,wcf,C#,Wcf,我正在从C#dll引用WCF服务,因此不会读取生成的app.config文件。我正在手动尝试通过下面的代码创建服务客户端;但是,我得到了需要增加MaxItemsInObjectGraph的错误。正在运行的服务已经设置为int.MaxValue,所以我现在只需要在TestServiceClient中增加它。有什么想法吗??提前谢谢 var client = new TestServiceClient(GetBinding(), GetEndpointAddress()); private sta

我正在从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;
        }
private静态ITestServiceClient GetClient()
{
var factory=newchannelfactory(GetBinding(),GetEndpointAddress());
factory.Endpoint.Contract.Operations中的foreach(var dataContractBehavior
.Select(operation=>operation.Behaviors.Find())
.Where(dataContractBehavior=>dataContractBehavior!=null))
{
dataContractBehavior.MaxItemsInObjectGraph=Int32.MaxValue;
}
var client=factory.CreateChannel();
返回客户;
}

在client.Endpoint.Contract.Operations中尝试

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

谢谢你的帮助!我更新了我原来的帖子,把我构建的完整方法放在那里。