Click here to Skip to main content
Click here to Skip to main content

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

By , 4 Nov 2003
Rate this:
Please Sign up or sign in to vote.

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:

<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown mode="Singleton" 
type="UsersNamespace.UserClass, Users" objectUri="Users.rem" />
      </service>
    </application>
  </system.runtime.remoting>
</configuration>
  • 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 = 
  (UserClass)Activator.GetObject(typeof(UserClass),
  "http://localHost/Users/Users.rem");
  
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()
{
 
    InitializeComponent()
    if(!System.Diagnostics.EventLog.SourceExists("DoDyLogSourse"))
    System.Diagnostics.EventLog.CreateEventSource("DoDyLogSourse",
                                                          "DoDyLog");

    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)
    {
        this.timer1.Start();
    }
  • Add timer stop on Onstop event
    protected override void OnStop()
    {
       this.timer1.Stop();
    }
  • Add timer start on OnContinue event
    protected override void OnContinue()
    {
        this.timer1.Start();
    }
  • 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

License

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

Comments and Discussions

 
Generalneed help Pinmemberhab19836-Jun-06 9:02 
GeneralLifetime Management &amp; Monitoring of Singleton PinmemberMichael1021-Feb-05 16:27 
Questionhow i change the lifetime of exist com Pinmembermuha25-Jan-04 19:47 
AnswerRe: how i change the lifetime of exist com PinmemberAlawi Alkaf25-Jan-04 19:51 
Generaldownload source code PinmemberMichael Murray13-Nov-03 14:22 
GeneralRe: download source code PinmemberAlawi Alkaf14-Nov-03 20:40 
Your comment is valid
I will update the source . UtilityLibrary.Common.RegistryData("PushUserTime") is the parameter to push the users in object ( in millseconds )
Reagrds

 
Alawi Alkaff
MCSD, MCT
I.T.
National Commercial Bank
Jeddah - Saudi Arabia
Generalsame but with windows forms Pinmemberdubem112-Nov-03 2:15 
GeneralRe: same but with windows forms PinmemberAlawi Alkaf14-Nov-03 20:42 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 5 Nov 2003
Article Copyright 2003 by Alawi Alkaf
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid