2017-02-13 77 views
0

我需要代碼的參數在WCF客戶端中設置ServiceBehaviorAttribute?

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; 

回答

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

我只是回答您的問題一個又一個。 –