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

﹥>﹥吖頭↗ 提交于 2019-12-11 11:17:01

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/7532288/is-it-necessary-to-dispose-when-using-a-custom-servicehostfactory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!