簡體   English   中英

在WCF客戶端中設置ServiceBehaviorAttribute?

[英]Set ServiceBehaviorAttribute in WCF Client?

我需要通過代碼設置參數

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;

一件事是端點配置(即,您發布的代碼),另一件事是服務行為。

要設置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;
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM