locked
Background Thread in WCF application RRS feed

  • Question

  • I have managed a background service via background thread in my asp.net application from global.asax events.Is there any possibility I can make some background thread in WCF application hosted inside IIS.[I wanted to avoid windows service]

    I am using .net framework 4.0


    Kamran Shahid Senior Software Engineer/Analyst (MCP,MCAD,MCSD.NET,MCTS,MCPD.net[web])
    Thursday, May 12, 2011 6:19 PM

Answers

  • Why not just start the background worker when the service is first instantiated?

     

    public class MyService
    {
     private static BackgroundWorkerThread = null;
    
     static MyService()
     {
       var backgroundWorkerThread = new Thread(BackgroundWorker);
       backgroundWorkerThread.IsBackground = true;
       backgroundWorkerThread.Start(); 
     }
    
     public static BackgroundWorkerThread()
     {
      while (1=1)
      {
       DoStuff();
      }
     }
    }
    

    The background worker will not start until your Service Application is called, this cannot be avoided as it is by design. You can however use AppFabric to call your service application whenever it ends (so it is always running). You should also keep an eye on the recycling options for your service application, it may cause your application to restart periodically.

    Alternative methods are to create a custom ServiceHost or service behaviour to start the background worker. These have the same issues as above, but may help separate the background worker code from your service code (if you like things looking clean).

    • Edited by Ben E D Ellis Thursday, May 12, 2011 11:18 PM Added alternatives.
    • Proposed as answer by Ben E D Ellis Thursday, May 12, 2011 11:18 PM
    • Marked as answer by Kamran Shahid Sunday, May 15, 2011 6:27 PM
    Thursday, May 12, 2011 11:16 PM

All replies

  • Can you describe your requirements in more details? I can understand that you want to spawn a background thread using WCF, but what is that you want to acheive in the end? Based on that I will be able to suggest further.
    Thursday, May 12, 2011 7:23 PM
  • Why not just start the background worker when the service is first instantiated?

     

    public class MyService
    {
     private static BackgroundWorkerThread = null;
    
     static MyService()
     {
       var backgroundWorkerThread = new Thread(BackgroundWorker);
       backgroundWorkerThread.IsBackground = true;
       backgroundWorkerThread.Start(); 
     }
    
     public static BackgroundWorkerThread()
     {
      while (1=1)
      {
       DoStuff();
      }
     }
    }
    

    The background worker will not start until your Service Application is called, this cannot be avoided as it is by design. You can however use AppFabric to call your service application whenever it ends (so it is always running). You should also keep an eye on the recycling options for your service application, it may cause your application to restart periodically.

    Alternative methods are to create a custom ServiceHost or service behaviour to start the background worker. These have the same issues as above, but may help separate the background worker code from your service code (if you like things looking clean).

    • Edited by Ben E D Ellis Thursday, May 12, 2011 11:18 PM Added alternatives.
    • Proposed as answer by Ben E D Ellis Thursday, May 12, 2011 11:18 PM
    • Marked as answer by Kamran Shahid Sunday, May 15, 2011 6:27 PM
    Thursday, May 12, 2011 11:16 PM