tags:

views:

193

answers:

2

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)]
A: 

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

Joe
I'm not overriding this attribute in web.config
Jader Dias
+2  A: 

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:

marc_s
always you that answers all my WCF questions =)
Jader Dias
Please continue
Jader Dias
Neither the throttling or the singleton worked for me in IIS. It worked as a self-hosted service though.
Jader Dias