asp.net-web-api


Timer event in web api


I have created a timer event try to running in the background of my web api, I found it works fine when I debugger on local dev machine. However, The timer does not work when I added them to IIS on server. It stops after the first web request finishes( I tested it by writing some text into log files, it seems stopped after a few triggers, once the web request completed)
Here is some example code.
void refreshTimeStart()
{
refreshTimer = new System.Timers.Timer(Convert.ToInt32(ConfigurationManager.AppSettings["TIMER_INTERVAL"]));
refreshTimer.Elapsed += new ElapsedEventHandler(connectionResetEvent);
refreshTimer.AutoReset = true;
refreshTimer.Enabled = true;
}
void connectionResetEvent(object source, ElapsedEventArgs e)
{
testIndex = testIndex + 1;
WriteToFile(testIndex);
}
static void WriteToFile(int i)
{
string text = "This start trigged. ";
System.IO.File.WriteAllText(#"C:\Projects\abc" + i.ToString() + ".txt", text);
}
Any idea of how to achieve this? Thanks a lot.
The question was asked a long time ago but here's an answer anyway.
First of all, using a timer in a Web API is probably not the best idea. A windows service would be more appropriate. That being said, your problem must come from two issues:
a Web API awaits a request and only initializes after the first request. So you'll have to initiate a request for your timer to start.
the default settings of application pools in IIS have a timeout. So even if you initialize the Web API, the application pool will terminate after the timeout period has elapsed. You could disable the timeout.

Related Links

Elmah.axd on WebAPI 2.2 - No HTTP Resource was found
OData attribute routing for deleting relationship between m:m entities
Sitecore GetChildren empty just after child item created
JsonFormatter in WebApi is ignored
Partial Authentication after expired token
Web api routing use only RoutePrefix
Differentiate between MVC and WebAPI in ASP.NET 5 / MVC 6
How to authenticate ionic app with Asp.net WebApi
Web Api and where should I contorol Request Header data
Authorize roles specific to category-branch
Cannot read property 'client' of undefined using SignalR
Odata endpoint returning 404 bad request when the “$” in filter is encoded
Odata web api 404 error
Webapi Controller - Routing not working
AutoFac does not register api controller
Using multiple Authorization schemes (HMAC and OATH)

Categories

HOME
hyperlink
rdbms
jelastic
pclxl
knitr
can
web-hosting
g++
mel
eip
scsi
easyphp
smartclient
jmp
nodemcu
ag
babel
question2answer
jboss6.x
froala
beautifier
jquery-multiselect
android-things
tizen-tv
realm-mobile-platform
mustache
utorrent
weather
papaparse
biztalk-rule-engine
instantiation
nested-lists
html-encode
mnist
google-knowledge-graph
reactivekit
verifiable-c
metaprogramming
diacritics
dcmtk
install.packages
file-sharing
importerror
libgphoto2
serde
vugen
podscms
rocks
parfor
udpclient
kiosk
phpwebsocket
aspen
ascii-art
crouton-os
gocql
wiredep
microsoft-reporting
opserver
doctype
teamcity-9.1
user-permissions
viewer
reflector
memset
buffering
websphere-esb
square-bracket
windows-phone-8-sdk
rolify
mongohq
tlf
android-sdk-2.3
dopostback
cgimage
htdocs
automationelement
massive
ie-compatibility-mode
delimited-text
facebook-iframe
aspmenu
ajax-polling
lalr
interop-domino
fail-fast-fail-early
idatareader

Resources

Encrypt Message