none
Instancecontext.PerCall and static variable class. RRS feed

  • Question

  • Hi all,

    I have a slight problem with freeing of resources when I set a wcf service Instance context mode to "PerCall".

    I have a class which does processing of some data.

    This class is created in the service and should stay alive until the service shuts down (there is a close method to stop the class processing).

    In "Single" context mode, I can put the close method in the Dispose method and it works fine. Below shows the service implementation code.

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class Service1 : IService1, IDisposable { private ProcessCls pc; private bool disposed; private static bool isInit = false; //other variables public Service1() { lock (this) { if (!isInit) { isInit = true; pc = new ProcessCls(); disposed = false; } } }

    //other methods

    public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { pc.Close(); } } } }


    However, if I use it in "PerCall" context mode, Dispose is executed at every proxy called. Thus this closes my class after the first proxy call.

    Where can I put the pc.Close method when I am using "PerCall" context mode to achieve close only at destructor effect (like in "Single" context mode)?

    Thanks




    • Edited by eva2002 Monday, April 30, 2012 9:10 AM
    Monday, April 30, 2012 9:08 AM

Answers

  • When your service process is terminated, all threads are terminated as well. This is out of your control. It's controlled by the OS. But if you mean you want to do something just before the service shuts down, you may be able to do it depending on where you host the service. If you host the service in IIS, you can handle Application_End event in Global.asax. If you host the service in a console, you can do something right after ServiceHost.Close.

    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.

    • Marked as answer by eva2002 Thursday, May 3, 2012 1:13 AM
    Wednesday, May 2, 2012 1:29 AM

All replies

  • Hello, do you want to use a single ProcessCls instance for all requests, or use one instance for each request? In the former case, you can make ProcessCls a static field, and do not close it unless something goes wrong and you have to recreate the instance. You want this instance to live during your service application's lifetime. If the service application process is terminated, the OS will automatically recycle the resource. In the latter case (you want a new instance of ProcessCls for each request), your existing will work fine. You're essentially closing that request's ProcessCls instance.

    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.

    Tuesday, May 1, 2012 1:22 AM
  • Thanks for your reply.

    I only want to use a single PrcessCls instance for all process. this instance will live for the whole duration of the service application's timelife.

    Inside my ProcessCls there are 2 threads running forever and 2 queues (1 for each thread).

    I would like to I would like to terminate the threads and perform some other process (e.g. closing a file) when the service shutdowns.

    I wasn't able to perform the mentioned stuff if I do it by using Dispose method.

    Wednesday, May 2, 2012 1:03 AM
  • When your service process is terminated, all threads are terminated as well. This is out of your control. It's controlled by the OS. But if you mean you want to do something just before the service shuts down, you may be able to do it depending on where you host the service. If you host the service in IIS, you can handle Application_End event in Global.asax. If you host the service in a console, you can do something right after ServiceHost.Close.

    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.

    • Marked as answer by eva2002 Thursday, May 3, 2012 1:13 AM
    Wednesday, May 2, 2012 1:29 AM
  • Thanks for the reply.

    The service will be hosted on IIS.

    I will see how to use the Application_End event in Global.asax.

    Thursday, May 3, 2012 1:15 AM