1

I have a ASP.Net MVC application, that needs to query certain state information from a windows service and then update clients with the new state information.

How can periodic querying be best implemented in ASP.Net? I would perhaps be using something like SignalR to update clients with the state information.

But what I need a solution to is the polling of the windows service.

This ASP.Net MVC application uses C#.

Saibal
  • 792
  • 5
  • 15
  • What type of state information are we talking about? Perhaps something that could be exposed using performance counters? – sisve Mar 25 '14 at 18:58
  • I am not sure if this information can be exposed via performance counters. But the actual issue I am try to solve is the correct way to periodically query this information from within the ASP.Net application. – Saibal Mar 25 '14 at 19:18
  • could you periodically post that state from windows service to ASP.NET app ? – Antonio Bakula Mar 25 '14 at 19:36

3 Answers3

1

You can host WCF-service in windows service and connect to it from ASP.NET MVC application. How to: Host a WCF Service in a Managed Windows Service

If service already written, you can use CacheItemRemovedCallback: easy-background-tasks-in-aspnet.

Hope it help.

Community
  • 1
  • 1
Alex Erygin
  • 2,991
  • 1
  • 19
  • 20
  • The service is already written and works. The state information in the service periodically changes. What I want to understand is how to correctly implement a background task in ASP.Net MVC which would periodically query the service for state information – Saibal Mar 25 '14 at 19:15
  • @Saibal, perhaps this article can help you: [easy-background-tasks-in-aspnet](http://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/) – Alex Erygin Mar 25 '14 at 19:28
0

I do not know of any way to do this without polling the service your MVC application. When the state of the service changes, you can then use SignalR to contact the clients of your web application that the state changed.

evasilevsky
  • 165
  • 5
0

I can quickly think of 3 solutions right now.

  1. Use a simple text file to store state informations. Write it with your Windows Service, and read it from the ASP.NET application. PRO: extremely primitive and simple to implement, and has minimal overhead. CON: If the state query gets more complex, the implementation can get more complex quickly.

  2. Use a shared database to store these informations in a specific table. PRO: More platform independent solution, can be queried from anywhere; easier to maintain or extend. CON: bigger overhead.

  3. Implement a very simple WCF service, host it in your Windows Service, and query it from anywhere for state informations. See more info here. PRO: easy to maintain or extend in the future. CON: harder to implement, expecially if you don't have any WCF experience; it's a bit too big gun for only simple state information.

On the client side the best and simplest approach would be IMHO a periodic tiny AJAX request to poll the state information from your web application, and present it to the user.

UPDATE

If you want to poll the service from ASP.NET MVC application periodically in the background, check WebBackgrounder project. However, I don't think it's a good practice to follow unless you really need to due to some special needs.

A better approach would be to serve this information when it's needed, when a request comes for it in some Action of some Controller. You can also apply ASP.NET caching to it if it is requested too frequently. Then on the client side you can do the periodic request via AJAX.

UPDATE2

If you don't like the periodic AJAX requests, you can check out AJAX push technics, for example here. I don't have any experience though, and cannot give you more points. I still believe that a frequent polling could be satisfactory, because these would be really tiny data transfers I guess.

Community
  • 1
  • 1
Zoltán Tamási
  • 10,479
  • 3
  • 51
  • 73
  • The service is already written and works. The state information in the service periodically changes. What I want to understand is how to correctly implement a background task in ASP.Net MVC which would periodically query the service for state information – Saibal Mar 25 '14 at 19:16
  • Ah I see. Why exactly do you need to periodically query the state? Doesn't it make sense to query it when a user request comes for it? Anyway I will update my answer accordingly. – Zoltán Tamási Mar 25 '14 at 19:18
  • Thanks... AJAX query is what I am doing right now. Problem is the state information may change at different times. I was wondering if it is possible to inform the client only when the state information changes. – Saibal Mar 25 '14 at 19:30
  • Probably some kind of PUSH technic will help then, answer updated. – Zoltán Tamási Mar 25 '14 at 19:36