2009-04-27 82 views
4

我有給我回WCF服務ServiceClient回實例下面的代碼:WCF:如何將ServiceThrottlingBehavior添加到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); 

最近我遇到一些麻煩超時,所以我決定增加一個限制行爲,像這樣的:

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

我的問題是,在上面的代碼中,我應該添加這個throttlingBehaviour到我的MusicRepo_DBAccess_ServiceClient實例?


從一些我在網上找到的例子,他們都在做這樣的事情:

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

請注意,在上面的代碼中,他們使用的是ServiceHost,而我不是,和他們然後打開它(與Open()),而我打開MusicRepo_DBAccess_ServiceClient實例......這讓我感到困惑。

+0

你不能在配置文件中有這樣的? – rguerreiro 2009-04-27 16:56:49

+0

我需要與多個項目共享此wcf服務,而不需要他們擁有app.config文件...這就是爲什麼我要以編程方式構建配置 – 2009-04-27 16:58:52

回答

3

您可以在配置文件afaik中指定行爲,並且使用行爲生成的客戶端將服從。排除簡潔

一些配置部分

<service 
    behaviorConfiguration="throttleThis" /> 

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

節流是一個服務端(服務器)的行爲沒有客戶端的一個

17

可以在代碼做了那些像我一樣,誰在運行時進行配置。

vb版本:

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

C#版本:

ServiceThrottlingBehavior stb = new ServiceThrottlingBehavior { 
     MaxConcurrentSessions = 100, 
     MaxConcurrentCalls = 100, 
     MaxConcurrentInstances = 100 
    }; 
    ServiceHost.Description.Behaviors.Add(stb); 
相關問題