1

Say I have a simple WCF application that the client calls in order to get a number. There's not much processing in it and the service contract is attributed as SessionMode=SessionMode.NotAllowed.

When is the constructor called? When is the object destructed? Is a constructor called per request?

Are there any reference documents or resources that have this information? I can't seem to find it.

4

1 回答 1

1

WCF 由 IIS 托管,因此受制于其生命周期规则。服务类本身可能会根据需要在应用程序中创建和销毁;该类将在收到请求、调用方法和返回结果后构建,之后对象将离开作用域并被释放/完成。

但是,包含您的服务的项目对于 IIS 看起来就像一个普通的 ActiveServer.NET Web 应用程序(查看应该在其中的 Global.asax 文件;它包含一个 HttpApplication 类型的类,并代表 IIS 应用程序的入口点可以用来控制它),并且 IIS 将维护这些应用程序的“池”来处理来自多个客户端的请求。只要请求不断进入,并且 IIS 没有确定应用程序已“过时”并刷新它或整个池,应用程序将继续运行。因此,您声明的任何静态类,例如您的单例 IoC 容器,或您添加到用作子类型的派生 HttpApplication 类的任何内容,都将保留在内存中,直到应用程序被回收。

于 2010-09-28T22:24:52.363 回答