Should WCF service typically be singleton or not?

后端 未结 3 852
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 10:29

I believe Jimmy Nillson said he generally made his webservices singletons. Is this the preferred method, and with WCF? Other than making the service methods static, is there

3条回答
  •  盖世英雄少女心
    2020-12-05 11:12

    Typically NOT. Singletons are a mess, since to make them perform well, you'll need to make them multi-threaded, and that's just asking for trouble unless you really really really know what you're doing.

    The best practice for WCF is to use per-call instantiation - each request gets its own copy of the service class, no multi-threading worries, good performance - store anything that needs to persist in a database - works like a charm.

    The only real scenario where singleton might make sense is if you have to have all service request be using/handled by a physical resource that's available only in a single instance - if your singleton service serializes and thus protects a single resource, then it makes sense to use it.

    Otherwise - spare yourself the trouble! :-)

提交回复
热议问题