Solved

WCF Global Variables in IIS Implementation

Posted on 2012-03-28
6
709 Views
Last Modified: 2014-10-18
To keep this simple, I'm going to pretend that all I want to do is record the number of times my WCF Service is called from a client in a static field called “Hits” and since a IIS hosted WCF service terminates if no requests are made after a period of time, “Hits” will be reset to zero when this happens, losing the data. I don't want each request fetching this data, but just getting it from the static field.

My questions are:
1.      What event can be trapped to read a starting value from a DB when the service starts (not a session start but the actual service start);
2.      How can I make the static field “Hits” threadsafe, since multiple workerthreads for this WCF will all be trying to update this static field;
3.      What event can be trapped, when the service self-terminates, to save the value in “Hits” to the DB.

Thanks
0
Comment
Question by:Vic-20
  • 2
  • 2
6 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37776201
I believe for 1 & 3 you can add a Global.asax file to your WCF project. Within that file you would have access to (for 1) Application_Start and (for 3) Application_End event handlers.

For 2, if you are only writing to variable, the I believe you just need to create a critical section around the call to write. If readers are involved as well, then it gets a bit more tricky.

For write only I believe you could do something like:

private static object SyncObject = new object();

...

lock (SyncObject)
{
    // write to field
}

Open in new window

0
 

Author Comment

by:Vic-20
ID: 37781171
Thanks for the Globals.asax idea. This works well.
Regarding the static field, I was able to hit the WebService with 300 requests per second from 2 different PC's asynchronously without any errors so I'm wondering if your lock example is needed. Perhaps a static field in IIS/WCF is threadsafe afterall?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 37781198
Perhaps a static field in IIS/WCF is threadsafe afterall?
No, static data is not thread safe until you make it that way. But now that I think about it:  if you are only writing to the variable, then I don't think it matters about locking. There is only one operation being applied to the variable, and nothing else is dependent on the field, so locking may not be required. If you later add code in which depends on the current value of the variable, then locking may be required.
0
 

Author Comment

by:Vic-20
ID: 37790157
Perhaps the question is what is the worst that can happen? Is it possible for data corruption to occur if the exact same memory location is updated at the same time by 2 different threads or is the OS preventing that when we get to that level?


This is the code sample btw:
{
  public class Service : IService
  {
    public DSQ.ClientsDataTable GetClients()
    {
      SGlobals.NoOfTimesCalled++;
…..
    }

    public int? NoOfTimesCalled()
    {
      return SGlobals.NoOfTimesCalled;
    }

  }
}
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now