Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

asp webservice variables and persistence

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob - 04 Apr 2008 01:35 GMT
Hi,
How does one initiate and persist variables between users in an asp
webservice or is that even possible?  For instance, I did a very
simple test case below.  I would like the timeStart variable to be
initialized once at server startup and have all users who subsequently
call the webservice get the initial time, not the time when they call
the service.  It seems like the whole process is restarted everytime
the service is called, with a new timeStart being generated each tme.

Thanks,
Bob

 public class Service1 : System.Web.Services.WebService
   {
       DateTime timeStart = new DateTime();

       public Service1()
       {

           timeStart = DateTime.Now;
       }
       [WebMethod]
       public string HelloWorld()
       {
           return "Hello World"+timeStart;
       }
   }
Arne Vajhøj - 05 Apr 2008 17:56 GMT
> How does one initiate and persist variables between users in an asp
> webservice or is that even possible?  For instance, I did a very
[quoted text clipped - 3 lines]
> the service.  It seems like the whole process is restarted everytime
> the service is called, with a new timeStart being generated each tme.

>   public class Service1 : System.Web.Services.WebService
>     {
[quoted text clipped - 11 lines]
>         }
>     }

Make it static.

Or save it in Application object.

Arne
Mr. Arnold - 05 Apr 2008 20:04 GMT
> Hi,
> How does one initiate and persist variables between users in an asp
[quoted text clipped - 4 lines]
> the service.  It seems like the whole process is restarted everytime
> the service is called, with a new timeStart being generated each tme.

<http://msmvps.com/blogs/omar/archive/2006/08/18/how-to-use-asp-net-2-0-profile-o
bject-from-web-service-code.aspx
>

You could also use an Application Object too or possibly writing an XML file
out with the Date and read it back into the Web service.
Arne Vajhøj - 05 Apr 2008 20:18 GMT
>> How does one initiate and persist variables between users in an asp
>> webservice or is that even possible?  For instance, I did a very
[quoted text clipped - 8 lines]
> You could also use an Application Object too or possibly writing an XML
> file out with the Date and read it back into the Web service.

Reading and writing a XML file combined with the locking to make it
thread safe would have a devastating effect on performance of the web
service.

Arne
Marc Gravell - 05 Apr 2008 22:41 GMT
In the specific case you mention (wanting to record the time the
service is first accessed), then a static variable (initialized in a
static constructor) would be a reasonable option. However, this is not
a good option for the general case:
* this doesn't handle app-pool recycling
* this doesn't work in a cluster
* this doesn't provide connection granularity
* this doesn't address the real issues of thread safety

You can have stateful services, but they don't really scale to
clusters in any way... in an enterprise setup, you'd probably want to
look at a session-state database, probably keyed by a guid held as a
header on the request. If you want something simpler (but less scale),
then WCF can handle a lot of this for you in-memory (on a single
machine - not a cluster) by keeping an instance of the service object
per client.

Marc
Bob - 06 Apr 2008 16:14 GMT
> In the specific case you mention (wanting to record the time the
> service is first accessed), then a static variable (initialized in a
[quoted text clipped - 14 lines]
>
> Marc

Great, thanks.  As a start I am using the Application object.
Bob - 07 Apr 2008 00:09 GMT
> > In the specific case you mention (wanting to record the time the
> > service is first accessed), then a static variable (initialized in a
[quoted text clipped - 16 lines]
>
> Great, thanks.  As a start I am using the Application object.

Is this the correct way to do it?

Application.Add("ws", ws);
in the construction and then

objectClass ws = Application.Get("ws") as  objectClass;

Thanks,
Bob

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.