views:

71

answers:

2

Hi,

I am trying to build (csharp) one webservice /WCF engine that make two actions:

1 – Have one timer (thread), that will run in each 10-10 minutes, requesting some information (connecting with other server to grab some info - status) to update in one database. (This must be automatic and no human action will be available). The idea is the webservice automaticaly (10x10 minutes) update the database with the recent information status.

2 – One service method that get some information from one database. (This is one simple method that gives the information when someone request it). This method will responsible to select the status info from database.

The problem is the step 1, because step 2 e very easy.

Can anyone help me, with ideas or some code, how to the step 1. Any pattern should be used here ?

Thanks

A: 

Since it's a webapp (for instance, a "WCF Service Application" project type in VS2010), you can hook into the application events.

By default that project template type doesn't create a Global.asax, so you'll need to "add new item" and choose "Global Application Class" (it won't be available if you already have a Global.asax, FWIW).

Then you can just use the start and end events on the application to start and stop your timer, so something like:

public class Global : System.Web.HttpApplication
{
    private static readonly TimeSpan UpdateEngineTimerFrequency = TimeSpan.FromMinutes(10);
    private Timer UpdateEngineTimer { get; set; }

    private void MyTimerAction(object state)
    {
        // do engine work here - call other servers, bake cookies, etc.
    }

    protected void Application_Start(object sender, EventArgs e)
    {
        this.UpdateEngineTimer = new Timer(MyTimerAction,
                                           null, /* or whatever state object you need to pass */
                                           UpdateEngineTimerFrequency,
                                           UpdateEngineTimerFrequency);
    }


    protected void Application_End(object sender, EventArgs e)
    {
        this.UpdateEngineTimer.Dispose();
    }
}
James Manning
First of all thanks for the answer!Is one webapp (webservice) in IIS. This action (point 1) is very important in this project. I must avoid and trying to get the best solutions to avoid bad performance or stop services. The same webservice request some info to all 300 servers ( x in x minutes - requesting information using sockets) and is use as service to provide update info to clients apps. I need to use System.Threading.Timer but I have to find the best solution to made it. Do you know if I should use any pattern here? Any good info from any book your article ?Thanks again
rbfigueira
I need to use System.Threading.Timer in the webservice but I have to find the best solution to made it. The status info comes from more than 300 different and distinct servers. I must pooling (requesting information using sockets) each one 10-10 minutes.thanks
rbfigueira
reworded the answer to be specific to the WCF webapp/webservice scenario and give some sample code
James Manning
@James: Does the default configuration include ASP.NET compatibility?
John Saunders
Thanks again James for the sample code. ;DWe need to have the two points available (1 – timer requesting information; 2 –providing one service method that get some information from one database). To avoid performance issues, I think we should and need to isolate this timer using one different thread. What do you think?
rbfigueira
the timer will be run on a threadpool thread - i don't believe there's any benefit to making your own thread for this, IMHO.
James Manning
@John: i think so, although i'm not sure why that would matter here?
James Manning
@James: it would matter if `Application_Start` didn't work without it...
John Saunders
A: 

The Single Responsibility Principle suggests that you should split these two responsibilities into two services. One (a Windows Service) would handle the Timer. The second, the WCF Service, would have the single operation to query the database and return the data.

These are independent functions, and should be implemented independently.

Additionally, I would recommend against depending on IIS or Application_Start and similar methods. That will prevent your WCF service from being hosted in WAS or some other environment. Keep in mind that WCF is much more flexible than ASMX web services. It doesn't restrict where you host your service. You should think carefully before you place such restrictions on your own service.

John Saunders
John, you are right but, in this case, we need those two responsibilities in the same web service. Perhaps you can point more principles or patterns we need to use in this particularly case. Thanks
rbfigueira
Can you point me other solutions (patterns, ideas, etc) kipping in mind that I need those two responsibilities in the same web service?
rbfigueira
@rbfigueira: you haven't said why they need to be in the same service.
John Saunders
Like I said I understand your point but the prerequisites are that all be on web, none Windows dependences, and more control via web. We have lots of them in Java (webservices). This one is for one dotnet project and we must use dotnet. Thanks
rbfigueira
Other good benefit is that we can REUSE the same "engine" over several web applications based in parameters.
rbfigueira