Creating a Windows Service Application

Creating a Service

To create and configure your service
  1. On the File menu, click New Project.
    The New Project dialog box opens.
  2. Select the Windows Service project from the list of Visual Basic, Visual C#, or Visual C++ project templates, and name it MyNewService. Click OK.
  3. Click the designer to select Service1. Then, in the Properties window, set the ServiceName and the (Name) property for Service1 to MyNewService.
  4. Set the AutoLog property to true.
  5. On the View menu, click Code to open the Code Editor. Edit the Main method to create an instance of MyNewService. When you renamed the service in step 3, the class name was not modified in the Main method. In Visual C# applications, the Main method is located in the Program.cs file.

    // Change the following line to match.
    ServicesToRun = new System.ServiceProcess.ServiceBase[]
       { new MyNewService() };

Adding Features to the Service

In the next section, you add a custom event log to the Windows service. Event logs are not associated in any way with Windows services. Here the EventLog component is used as an example of the type of component you could add to a Windows service. To add custom event log functionality to your service
  1. In Solution Explorer, right-click Service1.vb or Service1.cs and select View Designer.
  2. From the Components tab of the Toolbox, drag an EventLog component to the designer.
  3. In Solution Explorer, right-click Service1.vb or Service1.cs and select View Code.
  4. Edit the constructor to define a custom event log.

    public MyNewService()
    if (!System.Diagnostics.EventLog.SourceExists("MySource")) 
    eventLog1.Source = "MySource";
    eventLog1.Log = "MyNewLog";

To define what occurs when the service starts
  • In the Code Editor, locate the OnStart method that was automatically overridden when you created the project, and write code to determine what occurs when the service starts running:

    protected override void OnStart(string[] args)
    eventLog1.WriteEntry("In OnStart");

To define what occurs when the service is stopped
  • In the Code Editor, select the OnStop procedure from the Method Name drop-down list, which was automatically overridden when you created the project. Write code to determine what occurs when the service is stopped:

    protected override void OnStop()
    eventLog1.WriteEntry("In onStop.");

Some custom actions have to occur when a Windows service is installed, which can be done by the Installer class. Visual Studio can create these installers specifically for a Windows service and add them to your project.

To create the installers for your service

  1. In Solution Explorer, right-click Service1.vb or Service1.cs and select View Designer.

  2. Click the background of the designer to select the service itself, instead of any of its contents.

  3. With the designer in focus, right-click, and then click Add Installer.

    By default, a component class that contains two installers is added to your project. The component is named ProjectInstaller, and the installers it contains are the installer for your service and the installer for the service's associated process.

  4. In Design view for ProjectInstaller, click ServiceInstaller1 or serviceInstaller1.

  5. In the Properties window, set the ServiceName property to MyNewService.

  6. Set the StartType property to Automatic.

  7. In the designer, click ServiceProcessInstaller1 (for a Visual Basic project), or serviceProcessInstaller1 (for a Visual C# project). Set the Account property to LocalSystem. This will cause the service to be installed and to run on a local service account.

To build your service project

  1. In Solution Explorer, right-click your project and then click Properties. The project's Property Designer appears.

  2. On the Application page, from the Startup object list, click MyNewService.

  3. 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 that are required to run the Windows service. To create a complete setup project you will have to add the project output, MyNewService.exe, to the setup project and then add a custom action to have MyNewService.exe installed. For more information about setup projects, see Setup Projects. For more information about custom actions, see Walkthrough: Creating a Custom Action.

To create a setup project for your service

  1. In Solution Explorer, right-click to select your solution, point to Add, and then click 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. Click OK.

    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, and then click Project Output.

    The Add Project Output Group dialog box appears.

  2. MyNewService is selected in the Project box.

  3. From the list, 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, and then click Custom Actions.

    The Custom Actions editor appears.

  2. In the Custom Actions editor, right-click the Custom Actions node and click Add Custom Action.

    The Select Item in Project dialog box appears.

  3. Double-click the Application Folder in the list 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. In Solution Explorer, right-click the MyServiceSetup project and click Build.

To install the Windows Service

  1. To install MyNewService.exe, right-click the setup project in Solution Explorer and select Install.

  2. Follow the steps in the Setup Wizard. Build and save your solution.

Postback vs Callback


A Postback occurs when the data (the whole page) on the page is posted from the client to the the data is posted-back to the server, and thus the page is refreshed (redrawn)...think of it as 'sending the server the whole page ( full of data'.

On the other hand, a callback is also a special kind of postback, but it is just a quick round-trip to the server to get a small set of data (normally), and thus the page is not refreshed, unlike with the postback...think of it as 'calling the server, and receiving some data back'

With Asp.Net, the ViewState is not refreshed when a callback is invoked, unlike with a postback.

The reason that the whole page is posted with ASP.Net is because ASP.Net encloses the whole page in a <form> with a post method, and so when a submit button is clicked in the page, the form is sent ot the server with all of the fields that are in the form...basically the whole page itself

If you are using FireBug (for Firefox), you can actually see callbacks being invoked to the server in the Console. That way, you will see what specific data is being sent to the server (Request) and also the data the server sent you back (Response)

The below image illustrates the Page Life Cycles of both a postback and a callback in a ASP.NET based Website:

ASP.NET Page Life Cycles


Thus, a postback occurs when a request is sent from the client to the server for the same page as the one the user is currently viewing. When a postback occurs, the entire page is refreshed and you can see the typical progression on the progress bar at the bottom of the browser.

A callback, generally used with AJAX, occurs when a request is sent from the client to the server for which the page is not refreshed, only a part of it is updated without any flickering occurring on the browser

Mobile: WAP vs. Native Application Development (Apps)


There are development platform options to consider when building a mobile application. Here is a comparison of WAP vs. Native platform solutions (Apps). This includes areas to consider not only upon initial deployment but post launch.

WAP vs. Native Application Decision (Apps)
Apps Advantages:
  • Library update
  • Direct technical support
  • User has more control
  • App store and device portal solution
  • Can apply existing User Interface (UI) standard for mobile users
  • Better UI design result, can take full advantage of each platform
  • Data persistent – data could be available even with no network connection; more option on data cached

WAP Advantages:

  • Open Source solution
  • One programming language or solution could be applied for different browsers
  • Centralized – more control – easy to update
  • UI design takes less time and code to implement
  • Fast development
  • No code size and memory limitations
  • Better integrated tracking
Apps Disadvantage:
  • Frequent library updates – Fluid technology
  • Not all are Open Source solution
  • Different programming languages
  • Different UI design pattern
  • Slow development time
  • Code size and memory limitation
  • Update depends on the app store or user to update

WAP Disadvantage:

  • Library update could be slow from time to time
  • Limited direct technical support
  • Library is still limited to utilize the native features
  • Browser rending issue for different version and browsers
  • Data persistent – limited option and fewer data space can be cached