Jader Dias
Jader Dias

Reputation: 90465

How to limit the number of instances of a WCF service in IIS?

My WCF service is instantiated multiple times until the system gets out of memory? How to set a single instance in IIS 7?

My WCF service is already set to a single instance through attribute, but IIS seems to ignore this:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
    ConcurrencyMode = ConcurrencyMode.Multiple)]

Upvotes: 1

Views: 3575

Answers (2)

marc_s
marc_s

Reputation: 754220

You could define a WCF service as a singleton - I would recommend not to do so. You'll get yourself into a lot of trouble - performance and scaling issues, and to solve those, you'll have to deal with multithreaded, thread-safe programming - all pretty messy.

What you should do is use the per-call instantiation setup, and just limit your server to whatever you feel is reasonable. There's a service behavior called "serviceThrottling", which allows you to define things like how many concurrent instances of your service class you want to allow, how many concurrent calls you want to handle etc.

Those values are set pretty low by default, if you have a well equipped server, you can easily crank those up a bit. You define this in your server's web.config like this:

<behaviors>
    <serviceBehaviors>
        <behavior name="throttled">
            <serviceThrottling
                maxConcurrentCalls="16"
                maxConcurrentInstances="26"
                maxConcurrentSessions="10" />
        </behavior>
    </serviceBehaviors>
</behaviors>

(The values above are the default values)

Read up on service throttling and all its details with these excellent blog posts:

Upvotes: 5

to StackOverflow
to StackOverflow

Reputation: 124686

Are you possibly overriding the attribute in web.config? How do you know your service is being instantiated multiple times by client calls (rather than by code on the server creating multiple instances)?

Upvotes: 0

Related Questions