我正在从 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;
}