Tray Notify - Part I (Getting Started)

1. Introduction

This series illustrates how a Windows client app can communicate with a Windows Service application using a WCF service.

Frequently it can be a challenge to communicate between a Windows Service and a Windows application. Prior to Vista, Windows allowed a service to be registered with the "Interact with Desktop" property allowing the service to communicate with a client in the desktop of a logged in user. This was convenient because the service could display data in the user's desktop. Since Vista, this practice is no longer recommended due to security concerns.

Since interacting with the desktop is no longer an option, one of the challenging problems to overcome is how a Windows Service can communicate with applications running on a logged on user desktop. The difficulty is that a Windows Service runs on a different desktop (session 0) than user desktops (session n). While there are ways to overcome this problem such as enumerating current users, obtaining a handle to their desktops and so on, solving the various issues that arise with this approach isn't trivial.

Another approach is to use some form of interprocess communication and treat the Windows Service as a 'server' and require that the Windows client applications register with the 'server' on client start up. The service can then use the interprocess communication to notify clients of specific events or, in a less than ideal scenario, users could poll the service for changes.

The title of this article describes our approach - we will be using a WCF service to communicate between a Windows Service and a WPF Taskbar Tray application.

Our sample code will illustrate monitoring file change events from the Windows service and sending out file change notications to the task bar tray applications using the WCF service.

Figure 1.1 is a screen shot of the notification window that appears when a users makes changes to any file in the "My Documents" folder.


Figure 1.1

Since we need to cover a significant amount of ground and several different WxF technologies, we'll break the discussion into four parts.

  • Part I - Covers the basic architecture and the creation of the common class library.
  • Part II - Covers the creation of the Windows Service and service installer.
  • Part III - Covers the creation of the WCF service and how it's hosted inside the Windows Service.
  • Part IV- Covers the WPF tray application and its communication with the WCF service.
  • 2. Architecture

    2.1. Overview

    As mentioned our architecture is a Windows Service which hosts a WCF service used to communicate with a client taskbar application. The WCF service code monitors the My Documents folder of each user running the client, and sends a notification to the clients via the WCF. On startup, each client app registers itself with the WCF service. Figure 2.1 is a simple diagram outlining the approach.


    Figure 2.1

    About the Author

    Arjay Hawco is an application developer/architect and Microsoft MVP who works with the latest WxF .Net technologies. He is also cofounder of Iridyn, Inc, a software consulting firm.


    Tray Notify - Part I (Getting Started)

    2.2. Technologies

    The sample code is written in C# and uses WPF and WCF technologies. The Windows Service is a C# .net service, the task bar client app is a WPF C# app and the communication layer between the service and the client app is a WCF service hosted by the Windows service.

    2.3. Using the sample source code

    The TrayNotify.zip source file included in the download section is organized into the following folders:

    • SourceByArticle - Partial source code for each of the articles
    • Support Files - files for building the source code from scratch

    Readers that would like to follow along and build the solutions from scratch can use the completed source by article found in the SourceByArticles folder to refer to in case of trouble.

    3. Create the Solution and Projects

    Since the demo requires multiple C# projects, start by creating a blank solution and then create a few projects. Although the WCF Service and Tray Notify Client application won't be coded until Parts III and IV, create the blank projects now anyway. In addition, a class library will be created that will contain some utility classes as well as interfaces and contracts shared between the WCF Service and the client application.

    3.1. Create the blank solution

    Follow these steps to create a blank solution
    1. Open Visual Studio 2008
    2. Click "File\New\Project..." menu item
    3. Under "Project types:", expand the "Other Project types" node
    4. Click on "Visual Studio Solutions"
    5. Under "Templates:", select "Blank Solution"
    6. Enter "CG.TrayNotify" as the solution name.
    7. Set the desired location
    8. Press "OK".

    3.2. Create the Windows Service (Host) Project

    Follow these steps to add the Windows service that will host the WCF service.
    1. Right click on the "Solution 'CG.TrayNotify'" node in the Solution Explorer
    2. Click "Add\New Project..."
    3. Under "Project types:", expand the "Visual C#" node and select "Windows"
    4. Under "Templates", select "Windows Service"
    5. Enter "CG.TrayNotify.Host.Service" for the project name
    6. Verify that the Location is "...\CG.TrayNotify"
    7. Press "OK".

    3.3. Create the WCF Service Project

    Follow these steps to add the WCF Service to the solution.
    1. Right click on the "Solution 'CG.TrayNotify" node in the Solution Explorer
    2. Click "Add\New Project..."
    3. Under "Project types:", expand the "Visual C#" node and select "WCF"
    4. Under "Templates:", select "WCF Service Library"
    5. Enter "CG.TrayNofify.Wcf.Service" for the project name
    6. Verify the Location is "...\CG.TrayNofify"
    7. Press "OK"

    3.4. Create the Common Class Library

    Follow these steps to add a class library to the solution.
    1. Right click on the "Solution 'CG.TrayNotify" node in the Solution Explorer
    2. Click "Add\New Project..."
    3. Under "Project types:", expand the "Visual C#" node and select "Windows"
    4. Under "Templates:", select "Class Library"
    5. Enter "CG.TrayNotify.Common" for the project name
    6. Verify the Location is "...\CG.TrayNofify"
    7. Press "OK"

    About the Author

    Arjay Hawco is an application developer/architect and Microsoft MVP who works with the latest WxF .Net technologies. He is also cofounder of Iridyn, Inc, a software consulting firm.


    Tray Notify - Part I (Getting Started)

    4. Build out the Common class Library

    The common class library contains shared code that is used in Windows/WCF service project and in the Taskbar client app project.

    4.1. Add the Support Files to the Common Library

    There are multiple utility classes included in the demo source code. These get added to the Common library. If you haven't already done so, download and unzip the CGTrayNotify.zip source code that accompanies this article.

    4.1.1. Copy the support folders into the Common folder

    From explorer copy the Control, Service, and Threading folders located under the Support Files folder of the demo source into the CG.TrayNotify.Common folder.

    4.1.2. Add the support folders to the Common project

    From the Solution Explorer, select the CG.TrayNofity.Common node, and click on the "Show all files" icon in the upper left corner of the Solution Explorer window (it's the 2nd icon from the left.

    If you've copied the folders properly in explorer, you should see the following in Figure 4.1.2.1

    [Figure4121.jpg] Figure 4.121

    Highlight the Control, Service and Threading folders, right click and choose "Include In Project". You should end up with the following in Figure 4.1.2.2

    [Figure4122.jpg] Figure 4.122

    The Control, Service, and Threading folders that have been added to the project contain utility classes that will be used and explained in more detail later.

    4.2. Add the Common Project References

    Right Click on the References node under the CG.TrayNotify.Common project and chose "Add References...". Add the following references:

    • Presentation Core
    • Presentation Framework
    • System.Drawing
    • System.Runtime.Serialization
    • System.ServiceModel
    • System.Windows.Forms
    • Windowsbase

    4.3. Remove the Class1.cs file

    Remove the class1.cs file from the project by right clicking on the file and press "delete".

    4.4. Build the Common Project

    Build the entire solution (Build\Rebuild Solution). If you get any errors at this point, it's likely that one or more project references have been missed. Be sure to build and resolve any issues before moving on.

    5. Reference the Common class Library in the other projects

    Since the common class library is used by the other projects, we'll need to add a reference to the CG.TrayNotify.Host.Service and CG.TrayNotify.WCF.Service projects.

    1. Right click on the 'CG.TrayNotify.Host.Service" node in the Solution Explorer
    2. Click "Add Reference..."
    3. Click the "Projects" tab
    4. Select the "CG.TrayNotify.Common" item
    5. Press OK
    6. Repeat for the "CG.TrayNotify.Wcf.Service" project

    6. Summary

    This article created the solution, basic common library project, Wcf Service project and Windows Service project. Part II will cover the creation and registration of the Windows Service.

    7. About the Author

    Arjay Hawco is an application developer/architect and Microsoft MVP who works with the latest WxF .Net technologies. He is also cofounder of Iridyn, Inc, a software consulting firm.

    8. References

    Windows Services (Platform SDK) - good background read
    Window Service Applications (.net)


    Downloads

    Comments

    • There are no comments yet. Be the first to comment!

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

    • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

    Most Popular Programming Stories

    More for Developers

    Latest Developer Headlines

    RSS Feeds