【问题标题】:Set ServiceBehaviorAttribute in WCF Client?在 WCF 客户端中设置 ServiceBehaviorAttribute?
【发布时间】:2017-02-13 13:42:44
【问题描述】:

我需要通过代码设置参数

ServiceBehaviorAttribute

private static BasicHttpBinding getBinding()//BasicHttpBinding getBinding()
{

    //WSHttpBinding binding = new WSHttpBinding();
    //WSHttpBinding binding = new WSHttpBinding();
    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);

    binding.TextEncoding = System.Text.Encoding.UTF8;
    binding.ReaderQuotas.MaxArrayLength = int.MaxValue;

    binding.ReceiveTimeout = new TimeSpan(8, 0, 0);
    binding.SendTimeout = new TimeSpan(8, 0, 0);

    binding.MaxReceivedMessageSize = int.MaxValue;
    binding.MaxBufferSize = int.MaxValue;
    binding.MaxBufferPoolSize = int.MaxValue;


    binding.ReaderQuotas.MaxDepth = 64;
    binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
    binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;

    return binding;
}


private static EndpointAddress getEndPoint()
{
    EndpointAddress endPoint = new EndpointAddress(HTTP_SERVER);
    return endPoint;
}


ConnectionToServer = new ConnectionToServer (getBinding(), new EndpointAddress(HTTP_SERVER));

热插入ConnectionToServer这个代码???

ServiceBehaviorAttribute sba = new ServiceBehaviorAttribute();
sba.MaxItemsInObjectGraph = int.MaxValue;

【问题讨论】:

    标签: c# winforms wcf


    【解决方案1】:

    一件事是端点配置(即您发布的代码),另一件事完全不同的是服务行为。

    要设置 SBA.MaxItemsInObjectGraph,您需要在服务合同的执行行为中指定它,这是通过 WCF 服务中的 ServiceBehaviorAttribute 完成的(而不是您的代码所暗示的客户端)。

    即:

    [ServiceBehavior(
        InstanceContextMode = InstanceContextMode.Single,
        ConcurrencyMode = ConcurrencyMode.Reentrant,
        MaxItemsInObjectGraph = 34)]
    public class WcfService : IDuplexService
    {
        //service implementation goes here
    }
    

    这是在 ChannelFactory 上设置 MaxItemsInObjectGraph 的方法:

            DuplexChannelFactory<IService> cf = new DuplexChannelFactory<IService>(typeof(ServiceCallback), Server.ServerBinding(), ep);
    
            foreach (OperationDescription operation in cf.Endpoint.Contract.Operations)
            {
                var dc = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
                if (dc != null)
                {
                    dc.MaxItemsInObjectGraph = int.MaxValue;
                }
            }
    

    【讨论】:

    • 我刚刚回答了你的另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多