0

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?

4
  • You'll have to create a custom ServiceHost and a custom ServiceHostFactory, and tell IIS to use that instead of the default one. Then you can add the behavior programatically to your custom host. If I get a chance I'll post an example later.
    – Tim
    Jul 1, 2015 at 23:55
  • @Tim Thanks, if you have an example or can point me to one that would be great!
    – BVernon
    Jul 2, 2015 at 17:33
  • I do have an example, but I probably won't be able to post it until later tonight (I'm on the west coast of the US).
    – Tim
    Jul 2, 2015 at 18:36
  • @Tim That would be great, thank you!
    – BVernon
    Jul 2, 2015 at 19:00

1 Answer 1

1

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.)

1
  • I think base.ApplyConfiguration(); should go at the end of the method. Otherwise it gives an error.
    – BVernon
    Jul 6, 2015 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.