2009-04-27 10 views
4

J'ai le code ci-dessous pour revenir en arrière une instance de mon service WCF ServiceClient:WCF: comment ajouter un ServiceThrottlingBehavior à un service WCF?

var readerQuotas = new XmlDictionaryReaderQuotas() 
    { 
     MaxDepth = 6000000, 
     MaxStringContentLength = 6000000, 
     MaxArrayLength = 6000000, 
     MaxBytesPerRead = 6000000, 
     MaxNameTableCharCount = 6000000 
    }; 


    var throttlingBehaviour = new ServiceThrottlingBehavior(){MaxConcurrentCalls=500,MaxConcurrentInstances=500,MaxConcurrentSessions = 500}; 
    binding = new WSHttpBinding(SecurityMode.None) {MaxReceivedMessageSize = 6000000, ReaderQuotas = readerQuotas}; 

    dualBinding = new WSDualHttpBinding(WSDualHttpSecurityMode.None) 
         {MaxReceivedMessageSize = 6000000, ReaderQuotas = readerQuotas}; 

    endpointAddress = new EndpointAddress("http://localhost:28666/DBInteractionGateway.svc"); 

    return new MusicRepo_DBAccess_ServiceClient(new InstanceContext(instanceContext), dualBinding, endpointAddress); 

Dernièrement, j'avais des problèmes avec les délais d'attente et donc j'ai décidé d'ajouter un comportement d'étranglement, comme par exemple:

var throttlingBehaviour = new ServiceThrottlingBehavior() { 
     MaxConcurrentCalls=500, 
     MaxConcurrentInstances=500, 
     MaxConcurrentSessions = 500 
    }; 

ma question est, où dans le code ci-dessus dois-je ajouter cette throttlingBehaviour à mon MusicRepo_DBAccess_ServiceClient exemple?


De quelques-uns des exemples que je sur le web, ils font quelque chose comme ceci:

ServiceHost host = new ServiceHost(typeof(MyService)); 
ServiceThrottlingBehavior throttleBehavior = new ServiceThrottlingBehavior 
{ 
    MaxConcurrentCalls = 40, 
    MaxConcurrentInstances = 20, 
    MaxConcurrentSessions = 20, 
}; 
host.Description.Behaviors.Add(throttleBehavior); 
host.Open(); 

Notez que dans le code ci-dessus, ils utilisent un ServiceHost alors que je ne suis pas, et ils l'ouvrent alors (avec Open()) alors que j'ouvre l'instance MusicRepo_DBAccess_ServiceClient ... et c'est ce qui m'a rendu confus.

+0

Pouvez-vous pas cela dans un fichier de configuration? – rguerreiro

+0

J'ai besoin de partager ce service wcf avec plusieurs projets sans qu'ils aient un fichier app.config ... c'est pourquoi je construis la configuration par programme –

+0

Où hébergez-vous le service? – rguerreiro

Répondre

3

Vous pouvez spécifier le comportement dans le fichier de configuration afaik, et le client généré obéira, en utilisant des comportements.

Certaines sections de configuration exclus par souci de concision

<service 
    behaviorConfiguration="throttleThis" /> 

     <serviceBehaviors> 
      <behavior name="throttleThis"> 
       <serviceMetadata httpGetEnabled="True" /> 
       <serviceThrottling 
        maxConcurrentCalls="40" 
        maxConcurrentInstances="20" 
        maxConcurrentSessions="20"/> 
      </behavior> 
     </serviceBehaviors> 
6

Throttling est un comportement côté service (serveur) non côté client un

Arnon

+0

Doit être configuré non codé? – FaizanRabbani

17

Peut-on faire dans le code pour ceux, comme moi, qui configure à l'exécution.

version vb:

Dim stb As New ServiceThrottlingBehavior 
    stb.MaxConcurrentSessions = 100 
    stb.MaxConcurrentCalls = 100 
    stb.MaxConcurrentInstances = 100 
    ServiceHost.Description.Behaviors.Add(stb) 

C# version:

ServiceThrottlingBehavior stb = new ServiceThrottlingBehavior { 
     MaxConcurrentSessions = 100, 
     MaxConcurrentCalls = 100, 
     MaxConcurrentInstances = 100 
    }; 
    ServiceHost.Description.Behaviors.Add(stb); 
Questions connexes