0
votes

I have a WCF service hosted on IIS, which references a legacy tool, via COM interface. This legacy tool is a black box for me, so I can't change its code or fix bugs in it. When I restart my WCF service, or stop it via the IIS management tool, the referenced legacy tool should shut down as well, along with all other Unmanaged data it uses, and start again on the first call from the restarted WCF service.

Everything works fine, except one function call on this legacy tool. If I call this specific function, there is probably some bug inside the tool, which prevents it from going down, even if I stop my WCF service.

I thought about a workaround in which I'll call a Process.Kill function every time my WCF service app is being disposed. My difficulty is that I can't find the suitable place to call it.

First I tried to make my Service behavior class to inherit from IDisposable, and then implement a Dispose method. The problem is that the Dispose is called every time a function finished it's flow, but I can't kill the legacy tool every call of course.

My WCF service communicates with the client app via a Net Pipe channel, so from what I understand, I can't use a Global.asax file.

My question is - Where is the suitable place to call the Process.Kill code, if I want it to be called only once, when the whole WCF service application is being disposed.

Thanks.

2
if IDisposable is being called with every function, it means your service instance context mode is PerCall ? if so, would it be ok to switch it to Single ? this way your service instance lifetime will be aligned with the runtime lifetime? msdn.microsoft.com/en-us/library/…Ismail Hawayel
The client which I use to communicate with the service is closing the channel after every call, and I can't and shouldn't change it. If I change the Service context mode to Single, can it cause a problem in communication between the two?Max Mor
It should not, if it creates a new instance for every call, it should work fine with one single instance for all calls, it means the service is completely stateless, .. usually the opposite scenario is the one that could lead to problems, switching it from Single to Percall, coz it will kill any state used across calls. So I would try switching that service to Single, IDisposable then will be called once.Ismail Hawayel

2 Answers

0
votes

At the end, I solved the problem by calling the Kill process code in the static constructor of the service behavior. This constructor is called once even for PerCall context mode, so actually for me it has the same effect as if I called the code on the Dispose phase in single context mode. Thanks for all of you.

0
votes

You would need to implement a custom service factory to intercept the closing event see: Performing action before WCF service is shutdown