Click here to Skip to main content
13,458,910 members
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 4 Nov 2003

How to make a shared object to read set data from memory rather than from a database, to enhance the server performance

, 4 Nov 2003
Rate this:
Please Sign up or sign in to vote.
How to make a shared object and then reading it from memory rather than from a database

Remote object on IIS

Problem: Some times we need the same unchanged set of data from database such as user, cities, countries, etc. How to avoid accessing the database repeatedly, to enhance the server performance?

Solution: Use the .NET remoting Singleton object hosted by IIS. So the object will be shared for all applications. One program can populate the data from the database to be run as service. Other applications can access this object from IIS (see the following figure).

Let us take the following example (for Users).

Remote Object Lifetime

InitializeLifetimeService inherited from MarshByREfObejct (see code below).

// This would make the object have an infinite lifetime
// (as long as the host is active)
public override Object InitializeLifetimeService ()
    return null;

IIS configuration

In the Web.Config file:

        <wellknown mode="Singleton" 

type="UsersNamespace.UserClass, Users" objectUri="Users.rem" />
  • Mode=Singleton means a single instance of object services all clients
  • type="UsersNamespace.UserClass - The format is namespace.class.assembly
  • objectUri="Users.rem" - Must be .rem or .soap

Note: The other mode is Singlecall which means it will be a new instance of object on every client use.

How to call the remote object from the client

Add reference to remote object.

UserClass obj = 
return  obj.GetUsers(User);
  • typeof(UserClass) - The object type
  • http://localHost/Users/Users.rem - The remote object

IIS setup

Create new directory in C:\interup\wwwroot. Let us call it "Users".

This directory contains: the bin directory (which contains the User.dll Object) & web.config file.

Complete the wizard as default values. Then your object is hosted on IIS.

Develop a Windows service

Problem: Schedule a task. Time bound base. To populate the user from databases to above mentioned remote object.

Solution: Develop a Windows service.

To make benefit of Event Log:

public MyNewService()

    eventLog1.Source = "DoDyLogSourse";
    // the event log source by which 

    //the application is registered on the computer

    eventLog1.Log = "DoDyLog";

Use the System.Timers.Timer component.

Warring: Use Component Timer not Form timer.

  • Add timer start on Onstart event
    protected override void OnStart(string[] args)
  • Add timer stop on Onstop event
    protected override void OnStop()
  • Add timer start on OnContinue event
    protected override void OnContinue()
  • Add the required code of populate user under timer1_Elapsed event.

To build your service project

  1. In Solution Explorer, right-click your project and select Properties from the shortcut menu. The project's Property Pages dialog box appears.
  2. In the left pane, select the General tab in the Common Properties folder.
  3. From the Startup Object list, choose MyNewService. Click OK.
  4. Press Ctrl+Shift+B to build the project.

Now that the project is built, it can be deployed. A setup project will install the compiled project files and run the installers needed to run the Windows service. To create a complete setup project, you will need to add the project output, MyNewService.exe, to the setup project and then add a custom action to have MyNewService.exe installed.

To create a setup project for your service

  1. On the File menu, point to Add Project, and then choose New Project.
  2. In the Project Types pane, select the Setup and Deployment Projects folder.
  3. In the Templates pane, select Setup Project. Name the project MyServiceSetup.

A setup project is added to the solution. Next you will add the output from the Windows service project, MyNewService.exe, to the setup.

To add MyNewService.exe to the setup project

  1. In Solution Explorer, right-click MyServiceSetup, point to Add, then choose Project Output. The Add Project Output Group dialog box appears.
  2. MyNewService is selected in the Project box.
  3. From the list box, select Primary Output, and click OK.

A project item for the primary output of MyNewService is added to the setup project. Now add a custom action to install the MyNewService.exe file.

To add a custom action to the setup project

  1. In Solution Explorer, right-click the setup project, point to View, then choose Custom Actions. The Custom Actions editor appears.
  2. In the Custom Actions editor, right-click the Custom Actions node and choose Add Custom Action. The Select Item in Project dialog box appears.
  3. Double-click the application folder in the list box to open it, select primary output from MyNewService (Active), and click OK. The primary output is added to all four nodes of the custom actions — Install, Commit, Rollback, and Uninstall.
  4. Build the setup project.

To install the Windows service

Browse to the directory where the setup project was saved, and run the .msi file to install MyNewService.exe.

To start and stop your service

  1. Open the Services Control Manager by doing one of the following:
    • In Windows 2000 Professional, right-click My Computer on the desktop, then click Manage. In the Computer Management Console, expand the Services and Applications node.

      - Or -

    • In Windows 2000 Server, click Start, point to Programs, click Administrative Tools, and then click Services.

    Note: In Windows NT version 4.0, you can open this dialog box from Control Panel.

  2. You should now see MyNewService listed in the Services section of the window.
  3. Select your service in the list, right-click it, and then click Start.

Right-click the service, and then click Stop.

To verify the event log output of your service

Open Server Explorer and access the Event Logs node. For more information, see Working with Event Logs in Server Explorer.

Note: The Servers node of Server Explorer is not available in the Standard Edition of Visual Basic and Visual C# .NET.

To uninstall your service

  • On the Start menu, open Control Panel and click Add/Remove Programs, and then locate your service and click Uninstall.
  • You can also uninstall the program by right-clicking the program icon for the .msi file and selecting Uninstall


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alawi Alkaf
Web Developer
Saudi Arabia Saudi Arabia
No Biography provided

You may also be interested in...


Comments and Discussions

Generalneed help Pin
hab19836-Jun-06 9:02
memberhab19836-Jun-06 9:02 
GeneralLifetime Management &amp; Monitoring of Singleton Pin
Michael1021-Feb-05 16:27
memberMichael1021-Feb-05 16:27 
Questionhow i change the lifetime of exist com Pin
muha25-Jan-04 19:47
membermuha25-Jan-04 19:47 
AnswerRe: how i change the lifetime of exist com Pin
Alawi Alkaf25-Jan-04 19:51
memberAlawi Alkaf25-Jan-04 19:51 
Generaldownload source code Pin
Michael Murray13-Nov-03 14:22
memberMichael Murray13-Nov-03 14:22 
GeneralRe: download source code Pin
Alawi Alkaf14-Nov-03 20:40
memberAlawi Alkaf14-Nov-03 20:40 
Generalsame but with windows forms Pin
dubem112-Nov-03 2:15
memberdubem112-Nov-03 2:15 
GeneralRe: same but with windows forms Pin
Alawi Alkaf14-Nov-03 20:42
memberAlawi Alkaf14-Nov-03 20:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180323.1 | Last Updated 5 Nov 2003
Article Copyright 2003 by Alawi Alkaf
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid