IIS 中的 WCF 启用 ASP.NET 兼容模式,HTTP 请求线程与 WCF 服务操作不同
我有一个 WCF 服务托管在 IIS 上,具有 ASP.NET 兼容模式。
问题是我已经检查了 Global.asax 中处理的 HTTP 请求的线程与执行服务操作的线程不同。
如何使服务操作在 HTTP 请求的同一线程中调用?
先感谢您。
更新:
根本问题是我使用 HTTP 模块以便在请求期间使某些对象可用,并且当要发送响应时,这些对象将被处置/释放。< /em>
由于其中一些对象存储在线程静态字段中,因此无法在 WCF 服务操作中共享它们。
也许还有另一种方法可以做到这一点。例如,在调用某些 WCF 服务操作之前以及该操作结束之后执行某些操作。如有任何建议,我将不胜感激。
I've a WCF service hosted on IIS with ASP.NET compatibility mode.
The problem is I've checked the thread for the HTTP request, handled in the Global.asax, isn't the same as the one executing a service operation.
How can I make the service operations to be invoked in the same thread of HTTP request?
Thank you in advance.
UPDATE:
The underlying problem is I was using an HTTP module in order to make some objects available during a request, and when a response is going to be sent, these are disposed/released.
Since some of these objects are stored in thread static fields, there's no way of sharing them in the WCF service operation.
Maybe there's another way of doing so. For example, doing something before some WCF service operation is invoked, and after this has ended. I'll appreciate any suggestion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将任何内容存储在线程变量中并不是一个好主意,因为运行时无法直接控制 IIS 中 ASP.NET 管道和 WCF 主机管道之间的线程管理方式。
您是否可以使用
Items
容器?在 ASP.NET 兼容模式下,容器将在整个请求管道中可用。这样您就可以轻松地在其中存储/检索一些数据,并且不必太担心资源消耗,因为容器将在管道末端自动销毁。This is a bad idea to store anything in thread variables as there's no direct control on the way the runtime manages threads between ASP.NET pipeline and WCF host pipeline in IIS.
Is it possible that you use the
Items
container? In the ASP.NET compatibility mode the container will be available throughout the whole request pipeline. This way you can easily store/retrieve some data there and do not worry much about resource consumption as the container will be automatically destroyed at the end of the pipeline.如果可能的话,我强烈建议将逻辑从 HTTPModule 移动到由 WCF 服务调用执行的代码中。我们为每个服务调用执行和提供通用功能和验证,为了支持这一点,我们要求开发人员在每个服务调用开始时调用单个通用方法。
由于需要额外的代码,这并不像 HTTPModule 解决方案那么优雅,但通过这样做,我们最终会为每个服务调用提供一个很好包含的工作单元,该工作单元具有众所周知的设置和拆卸点。
If it is possible, I would strongly recommend moving the logic from your HTTPModule into code that is executed by the WCF service calls. We have common functionality and validation that is performed and provided for every service call and to support this, we require the developers to call a single, common method at the start of each service call.
This isn't quite as elegant as an HTTPModule solution due to the extra code, but by doing this we end up with a nicely contained unit of work for each service call that has well-known setup and teardown points.