What are the benefits for several servicehosts? Do

2019-06-17 08:45发布

问题:

I'm thinking of self-hosting my WCF service instead of using IIS. A big question for me is whether I need to instantiate multiple servicehosts as IIS does or one wil be enough.

Do muptiple servicehosts give any benefit except security reasons dut to isolation?

Can one servicehost serve multiple connections on one endpoint simultaneously?

回答1:

There's no benefit or choice, really - one ServiceHost (instance of that class) can host exactly one service, and for each service, you need a separate service host. It's a 1:1 mapping - always, no choice.

But of course, your Windows NT service or console app can have multiple ServiceHost objects active at the same time. This can be useful if you have a set of services that logically belong together and can't really exist without one another - where it doesn't make sense to have one of them started and another one not started.

And yes, a service host can host a service which exposes multiple endpoints, and multiple clients can connect on those separate endpoints at the same time, no problem. The WCF runtime will spin up a number of worker threads to handle incoming requests (you can limit those with the ServiceThrottling behavior) independently of each other.


To set up and control, how many concurrent calls and requests you have, you'll need to look at the ServiceThrottling behavior on the server side.

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

and you need to reference that service behavior configuration in your service declaration, of course:

<service name="YourService" behaviorConfiguration="serviceThrottled">
  .....
</service>

These are the default values. The explanations are as follows (taken from Dan Rigsby's blog post, shortened):

  • MaxConcurrentCalls (default = 16) [Per-message] The maximum number of messages that can actively be processed.

  • MaxConcurrentInstances (default = 26) The maximum number of InstanceContext objects in a service that can execute at one time. For per-session service, this is equal to the maximum number of sessions, for per-call service, it's the maximum number of concurrent calls, and for singletons, it's pointless.

  • MaxConcurrentSessions (default = 10) [Per-channel] The maximum number of sessions that a service can accept at one time. Only comes into play with session-based bindings (wsHttp or netTcp)

Definitely also check out Dan Rigsby's excellent blog post on the topic.