使用自定义ServiceHostFactory时是否需要Dispose()?

发布于 2024-12-06 14:13:57 字数 565 浏览 2 评论 0 原文

使用自定义ServiceHostFactory时是否需要Dispose()?

在我的 WCF .svc 文件中,我将自定义工厂定义为: <%@ ServiceHost Factory="Service.ServiceHostFactory" %>

似乎没有调用 Dispose(),因为在每次执行应用程序时都没有调用重写的 CreateServiceHost() 方法调用服务。 (此外,除其他外,每次调用后都不会执行日志记录,并且我生成的trace.xml 文件表明它正在被另一个进程使用)。

我确实有服务装饰 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 所以我预计还会发生一些我不知道的事情。在创建服务实例的客户端应用程序中,我通过finally块对引用进行Dispose()操作,但是是否有必要在服务器端的工厂中执行类似的操作?

    Finally
        service.Dispose()
    End Try

谢谢

Is it necessary to Dispose() when using a custom ServiceHostFactory?

In my WCF .svc file I have defined a custom Factory as:
<%@ ServiceHost Factory="Service.ServiceHostFactory" %>

It appears that a Dispose() is not being called since the overridden CreateServiceHost() method is not being called at each execution of the application calling the service. (Also, among other things, logging is not being performed after each call and the trace.xml file I generated says that it is in use by another process).

I do have the service decorated with
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
so I expect something else is going on that I'm not aware of. In the client application where the instance to the service is created I am Dispose()'ing of the reference via a finally block but is it necessary to perform a similar operation in the Factory on the server side?

    Finally
        service.Dispose()
    End Try

Thanks

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

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

发布评论

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

评论(1

久隐师 2024-12-13 14:13:57

服务主机工厂返回一个服务主机,而不是服务类的实例。该工厂通常在每次激活服务时只调用一次,并且使用它返回的主机,直到 IIS 应用程序池被回收为止。服务实例由 IInstanceProvider 处理,而不是由服务主机处理(尽管在创建主机时,如果您想要处置服务实例,可以更改实例提供程序 - 有关实例提供程序的更多信息,请参阅 http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspxhttp://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iinstanceprovider.aspx)。

简而言之,您不应该处置从服务主机工厂返回的服务(或者是主机?)。如果您想处理服务处置,您应该实现自己的实例提供程序。

The service host factory returns a service host, not an instance of the service class. The factory is usually called only once per activation of the service, and the host it returns is used until the IIS application pool is recycled. The service instance is handled by an IInstanceProvider, not the service host (although as you're creating the host you can change the instance provider if you want to dispose the service instance - for more information about instance providers, see http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx and http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iinstanceprovider.aspx).

So in short, you should not dispose the service (or is it the host?) which you're returning from the service host factory. If you want to handle service disposal, you should implement your own instance provider.

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