Web services start event and dll

  • Thread starter Thread starter shashwat_k11
  • Start date Start date
S

shashwat_k11

Hi,

I am writing web service and I want to read a file only once when I
start the web service. How can I do that? Which event fires exactly
onece when we launch web service?



I am reading a xml file in the Init() so whenever the web service is
invoked, it is reading that file. This is what I want to avoid. Where
can I write the code so that the file gets read only once? Also where
should I place the dll so that it will be accessible as long as the
web service is running.
 
I'm assuming that you need to read the XML file for some class that
offers up the details in that file (which is treated as read-only, from what
I can tell).

The best way to do this is to create a static constructor for the class
which utilizes the file, and then load the file in the static constructor
into a static variable (or load the information in the file into whatever
static variables you want) and then access those from your class instances.
 
Thanks for your reply.

I am reading that file in a static variable and I am checking that
static variable before reading that file again. Everything works fine
for 10 min. or so.

But if I do not invoke the web services for more than 10 min, then I
guess the garbage collector runs and static variable looses all the
values stored in it. So I read that file again. Now, reading that file
is very expensive. So I want to reduce no. of times that I am reading
the file.

Where can I write the code so that it will be run exactly once and
will be alive as long as that session is alive.




I'm assuming that you need to read the XML file for some class that
offers up the details in that file (which is treated as read-only, from what
I can tell).

The best way to do this is to create a static constructor for the class
which utilizes the file, and then load the file in the static constructor
into a static variable (or load the information in the file into whatever
static variables you want) and then access those from your class instances.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I am writing web service and I want to read a file only once when I
start the web service. How can I do that? Which event fires exactly
onece when we launch web service?
I am reading a xml file in the Init() so whenever the web service is
invoked, it is reading that file. This is what I want to avoid. Where
can I write the code so that the file gets read only once? Also where
should I place the dll so that it will be accessible as long as the
web service is running.- Hide quoted text -

- Show quoted text -
 
Try to log Application_OnEnd event in your Global.asax.

I have a asp.net server that is run on a shared hosting (GoDaddy). Every now
and then their IIS unloads my application for no apparent reason. I do store
some data in static variables, and eventually they get lost. I have to back
them up in each Application_OnEnd() and restore them in each
Application_OnStart(). But as long as your app is alive/loaded, there
shoudn't be any problem with your variables.

Again, try and see if the reason for lost data is that your app gets
unloaded (log every Application_OnEnd). If that's the case, you need to come
up with some other ways of storing your data. Database vs File would be
another option.



Thanks for your reply.

I am reading that file in a static variable and I am checking that
static variable before reading that file again. Everything works fine
for 10 min. or so.

But if I do not invoke the web services for more than 10 min, then I
guess the garbage collector runs and static variable looses all the
values stored in it. So I read that file again. Now, reading that file
is very expensive. So I want to reduce no. of times that I am reading
the file.

Where can I write the code so that it will be run exactly once and
will be alive as long as that session is alive.




I'm assuming that you need to read the XML file for some class that
offers up the details in that file (which is treated as read-only, from
what
I can tell).

The best way to do this is to create a static constructor for the
class
which utilizes the file, and then load the file in the static constructor
into a static variable (or load the information in the file into whatever
static variables you want) and then access those from your class
instances.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I am writing web service and I want to read a file only once when I
start the web service. How can I do that? Which event fires exactly
onece when we launch web service?
I am reading a xml file in the Init() so whenever the web service is
invoked, it is reading that file. This is what I want to avoid. Where
can I write the code so that the file gets read only once? Also where
should I place the dll so that it will be accessible as long as the
web service is running.- Hide quoted text -

- Show quoted text -
 
I have some data in a XML file. I am storing that file path in
web.config under appSettings. When I start my application, I am
reading that XML document in Service.cs in init() and store it in a
static variable and query that document to get the information I need.
After that I run into the problem I have explained above.

I want to read that XML document only once and do not want to loose it
as long as web service is active. Any ideas?
 
In the Start method of the Service class, simply read the file and
then start the thread that does the work (you have to thread the main
code in the service or your service will never get out of start mode.

private System.Timers.Timer timer;

public MyService()
{
// InitializeComponents();
timer = new Timer(/* parameters for time interval */);
timer.Elapsed += // method handler with method for executing main
service code
}

// Changes status to running in service window while executing every
interval
public override void Start()
{
// Read file here
timer.Enabled = true;
}

// Changes status to stop in service window
public override void Stop()
{
// timer.Enabled = false;
}
 
I have some data in a XML file. I am storing that file path in
web.config under appSettings. When I start my application, I am
reading that XML document in Service.cs in init() and store it in a
static variable and query that document to get the information I need.
After that I run into the problem I have explained above.

I want to read that XML document only once and do not want to loose it
as long as web service is active. Any ideas?





- Show quoted text -

Don't make the variable static if possible. Otherwise, reread it every
time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top