简体   繁体   中英

How to set ServiceThrottlingBehavior for IIS hosted WCF service programmatically

I'm trying to figure out where to shove this object:

ServiceThrottlingBehavior stb = new ServiceThrottlingBehavior
{
    MaxConcurrentSessions = 100,
    MaxConcurrentCalls = 100,
    MaxConcurrentInstances = 100
};

I've found info on how to configure this in the web.config but I'm a little confused about that. We used to have stuff in our web.config that looked like this:

<service name="AuthenticationService.AuthenticationService" behaviorConfiguration="Development">
    <endpoint address="http://services.local/0.0.0.5/AuthenticationService.svc"
              binding="basicHttpBinding"
              bindingConfiguration="TUPSOAPBinding"
              contract="AuthenticationService.ServiceDomain.ISecurityService"
              name="SOAPCatalogService"  />
  </service>

If we still used that I would know exactly how to configure the throttling via the web.config, but we found that we could take all those endpoints out of the web.config and everything still worked and it was less maintenance since we didn't have to go update the address for different versions and environments anymore.

I've also found info on how to set this programmatically on the ServiceHost, but I'm not creating the ServiceHost programmatically. I'm letting IIS take care of that.

So is there a programmatic way for me to set the throttling without the web.config and without having to create the ServiceHost myself?

EDIT: Or is there a way for me to do this in web.config without having to create a <service /> entry for every one of our services?

One way to do this is to use the markup in the .svc file to instruct IIS to use your custom service host and custom service host factory. To do this of course you need to have a custom service host. For example:

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

public class MyServiceHost : ServiceHost
{

    public MyServiceHost()
        : base() { }

    public MyServiceHost(Type serviceType, params Uri[] baseAddresses)
        : base(serviceType, baseAddresses) { }

    public MyServiceHost(object singletonInstance, params Uri[] baseAddresses)
        : base(singletonInstance, baseAddresses) { }

    protected override void OnClosing()
    {
        base.OnClosing();
    }

    protected override void ApplyConfiguration()
    {
        base.ApplyConfiguration();
        this.Description.Behaviors.Add(new ServiceThrottlingBehavior
        {
            MaxConcurrentSessions = 100,
            MaxConcurrentCalls = 100,
            MaxConcurrentInstances = 100
        });
    }
}

The key point above is the override of ApplyConfiguration() , where you can add your ServiceThrottlingBehavior to the custom service host.

IIS will use ServiceHostFactory to instantiate MyServiceHost , so you will also create a custom service host factory, like so:

public class MyServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, params Uri[] baseAddresses)
    {
        return new MyServiceHost(serviceType, baseAddresses);
    }
}

The above code will create the actual instance of your custom service host.

The final step is to alter the markup of the .svc file to use your custom service host and factory:

<%@ ServiceHost Langauge="C#" Service="MyCompany.MyService" 
    CodeBehind="MyService.svc.cs" Factory="MyCompany.MyServiceHostFactory" %>

The service name needs to be the fully qualified name of the service, and the factory will also need to be the fully qualified name of your custom service host factory.

Obviously, you can add a lot of stuff to your custom service host (we have monitoring and error handling in ours). This was originally done in .NET 3.5, so there may be newer or additional ways to do this in 4.0/4.5 (For example, I know you can specify the factory in the config file for file-less activation, but that would go in the <system.serviceModel> section, which you seem to want to avoid.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM