多个服务主机有什么好处?一个 ServiceHost 是否支持一个端点上的多个同时连接?

发布于 2024-08-19 02:09:42 字数 154 浏览 6 评论 0原文

我正在考虑自行托管我的 WCF 服务,而不是使用 IIS。对我来说,一个大问题是我是否需要像 IIS 那样实例化多个服务主机,或者一个就足够了。

除了由于隔离导致的安全原因之外,多个服务主机还有什么好处吗?

一台服务主机可以同时为一个端点上的多个连接提供服务吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2024-08-26 02:09:42

实际上,没有任何好处或选择 - 一个 ServiceHost(该类的实例)只能托管一项服务,并且对于每一项服务,您都需要一个单独的服务主机。这是 1:1 的映射 - 总是如此,别无选择。

当然,您的 Windows NT 服务或控制台应用程序可以同时激活多个 ServiceHost 对象。如果您有一组逻辑上属于在一起的服务,并且没有彼此就无法真正存在,那么这会很有用 - 其中一个服务启动而另一个服务未启动是没有意义的。

是的,服务主机可以托管公开多个端点的服务,并且多个客户端可以同时连接这些单独的端点,没有问题。 WCF 运行时将启动多个工作线程来处理传入的请求(您可以通过 ServiceThrotdling 行为限制这些请求),彼此独立。


要设置和控制并发调用和请求的数量,您需要查看服务器端的 ServiceThrotdling 行为。

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

当然,您需要在服务声明中引用该服务行为配置:

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

这些是默认值。解释如下(摘自 Dan Rigsby 的博客文章,已缩短):

  • MaxConcurrentCalls(默认 = 16) [Per-message] 可主动处理的最大消息数。

  • MaxConcurrentInstances(默认值 = 26)服务中可同时执行的 InstanceContext 对象的最大数量。对于每会话服务,这等于最大会话数,对于每调用服务,它是最大并发调用数,对于单例,它是没有意义的。

  • MaxConcurrentSessions(默认 = 10)[每通道] 服务一次可以接受的最大会话数。仅适用于基于会话的绑定(wsHttp 或 netTcp)

仅在基于会话的绑定(wsHttp 或 netTcp)中 -a-wcf-service-help-prevent-dos-attacks-and-maintain-wcf-scalability/" rel="noreferrer">Dan Rigsby 关于该主题的精彩博客文章。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文