All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
Windows Services
In this chapter, you will
CHAPTER
26
• Learn how to create a Windows service
• Write code that is executed when a Windows service is started and stopped
• Implement a serviced component
• Create interfaces that are visible to COM
• Create a strongly named assembly
• Register the component in the Global Assembly Cache
• Manage the component by using the Component Services tool
In this chapter, we will cover two of the major objectives of Exam 70-320, “Developing
XML Web Services and Server Components.” The first objective deals with Windows Ser-
vices—we will look at how to create and manage a Windows service, which is a compo-
nent that runs in the background of the Windows operating system. The second
objective is to create and consume a serviced component. Towards this objective, we
will investigate serviced components and create interfaces that are visible to COM and
work with strongly named assemblies.
The goal of this chapter is to provide you with the framework for developing mid-
dle-tier components (serviced components) and background services. Services that you
may already be familiar with include file-transfer services (like FTP Server), web server
services, and so forth. You can also build your own types of services that users can access
locally or over the network.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:03 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
In Figure 26-1, you can see that some of the background services include Simple Mail
Transport Protocol, Task Scheduler, Telephony, Telnet, Workstation, and more. Each
system will display a different list of services depending on the services installed on the
machine. These services can be active (providing a service) or waiting to be activated
(listening for activity that requests their service). For example, SMTP is started but not
active until an e-mail message needs to be moved. (The SMTP service is the service that
allows e-mail to leave your computer.) The Workstation service is started and waiting
for requests to be made of remote hosts.
Let’s take a closer look at the SMTP service. If you were to right-click on the service
name and select Properties from the submenu, you would see a Properties dialog box, as
shown in Figure 26-2. Notice the different properties on the General tab:
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:03 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
For our purposes, we should also investigate the Log On tab of the Properties dialog
box (see Figure 26-3). This is where you can set the account that is used when the service
starts. Since a service really doesn’t operate through user intervention or user logon, the
service needs a user account under which it “logs on.”
The logon account can be a local system account or a user account that has been
added specifically for it (actually, it can use any account, like the Administrator ac-
count, but it is advisable for it to have its own account. That way, if an account is de-
leted, the service can still run under its own account). Notice in Figure 26-3 that you
can also set the service to be able to interact with the desktop. You may have need of a
service that provides a user interface to the logged on user account. For example, an
Event Log service needs a user interface in which to display the logged entries. In this
case, the Allow Service to Interact with Desktop check box should be selected.
PART V
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:04 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
EXAM TIP Make sure that you have a separate “log on” account configured
for your Windows service. It is not a good idea to use a normal user account.
Using Microsoft Visual C#, we can build our own Windows services that will operate
similarly. In the next section, we will look at how you can build a Windows service using
Visual Studio .NET, and then we’ll explore how you can use and configure the service.
1. Start a new project from Visual Studio .NET. Select File | New | Project and
select Visual C# Projects in the left pane. Select Windows Service in the
Templates pane (shown here). Name the service ClockService and
locate it in the E:\C Progs\Ch26 folder. Click OK.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:04 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
EXAM TIP Make sure that you select Windows Service from the template
list in the New Project dialog box. You cannot choose Web Service.
PART V
3. In the Properties Explorer, you can set the name of the service (see Figure 26-4).
This should be done immediately, as the IDE calls the service Service1, and
code will be automatically built for you using that name. You need to change
that name to ClockService.
4. Switch to the Code Editor and change the class name to ClockService (don’t
forget to change the constructor as well).
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:05 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
5. At this point, you will also need to change the code that has been generated in
the Main() method. Be sure that your code looks like the following:
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{
new ClockService();
}
System.ServiceProcess.ServiceBase.Run (ServicesToRun);
}
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:05 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
2. When you add the EventLog component to the Design view, you get a new
instance of the component—eventLog1.
3. Return to the code window and add the following code to the service constructor.
This code can be added anywhere—if you add it to the constructor, the event
log will be ready when you need it.
public ClockService()
{
//This call is required by the Windows.Forms Component Designer
InitializeComponent();
// Check if the event log exists—if not, create the event log
if (!System.Diagnostics.EventLog.SourceExists ("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource ("MySource", "MyNewLog");
}
// Set the Source and Log properties for the EventLog component
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:05 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
1. In the code editor, find the OnStart() method and code the following:
protected override void OnStart (string[] args)
{
eventLog1.WriteEntry ("We are in the Start method");
}
2. Find the OnStop() method and code the following:
protected override void OnStop()
{
eventLog1.WriteEntry ("We are in the Stop method");
}
1. Change back to the Design view and click on the background. Do not select
anything.
2. Make sure that the Properties Explorer is displayed, and click the Add Installer
link from the bottom of the Properties Explorer window. (The mouse pointer
in Figure 26-5 points out the link).
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:05 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
One is the installer for your service (the latter), and the other is the installer for
the service’s associated process, which we will discuss in the “Windows Service
Architecture” section later in this chapter.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:06 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
You are now ready to build the project. As a final step, you should set the startup ob-
ject to ClockService. Do this by selecting the project in the Solution Explorer and
right-clicking to access the submenu. Select Properties from the menu, and you will see
the Property Pages dialog box shown in Figure 26-6. Select the General option in the left
pane, the Startup Object option in the right pane, and select your new service—
ClockService.ClockService from the drop-down list. Click OK to accept the
properties, and then build the project by pressing CTRL-SHIFT-B (or by selecting Build |
Build from the menus).
1. Add a Setup project to the solution. Select File | Add Project, and choose
New Project. Select Setup and Deployment Projects in the left pane of the
Add New Project dialog box, and select Setup Project from the list of templates.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:06 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:06 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
5. Now you need to add a custom action to the Setup project to install the
executable file. Right-click on the Setup project and select View | Custom
Actions from the menu. You will see the Customer Actions editor. Choose
Add Custom Action.
6. In the Select Item in Project dialog box, select the Application Folder from
the Look In drop-down box. Then select Primary Output from ClockService
(Active), as shown next.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:06 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
7. You will notice that the primary output has been added to all four nodes—
Install, Commit, Rollback, and Uninstall:
PART V
8. Build the Setup project and exit Visual Studio .NET.
You have now successfully created a Setup project for your Windows service. If you
are unsure of the purpose of any of the preceding steps, be sure to spend some time in
Chapter 30. The setup and deployment of Windows services, .NET Remoting, and XML
Web Services will be covered in that chapter.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:07 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
2. As the service installs, you will be requested enter the username and password
that will be used to log into the service in the Set Service Login dialog box
(shown next). As we mentioned previously, you should have an account prepared
for the service. We have created an account called ClockServiceUser. If you know
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:07 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
Figure 26-7 Setting configuration properties in the Property Pages dialog box
how to set up a user in Windows 2000 (our platform for development) and
have already done so, skip to step #7.
PART V
3. In order to set up a new user in Windows 2000 that can be used for the service,
select Start | Settings | Control Panel | Administrative Tools | Computer
Management. This will open the Computer Management console. Locate
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:07 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
4. Check the User Cannot Change Password check box and the Password Never
Expires check box, which do exactly what they say. This is important, since you
don’t want a service’s password to expire—the next time the service was started,
it would not be able to start under the old password. Click Create to add the
new user to the accounts database.
5. Select the account from the right pane of the Computer Management console
and right-click on it to access the account properties. You will see a window
similar to that displayed in Figure 26-20.
6. Make sure that the account belongs to the Administrators local group. You may
want to read Chapter 30 and look at security issues here, but in order to have
the service start up while the operating system starts, you will need an account
with more power than just a local user. Click on the Member Of tab, select the
Administrators group, and click the Add button. Click Close to exit the properties
of the new user.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:07 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
7. You have now added the new user and set the group membership. Specify the
user account for service log in, and the installation process continues to
completion.
8. To test the newly installed service, open the Computer Management console
and look at the services (in Windows 2000, select Start | Settings | Control
PART V
Panel | Administrative Tools | Computer Management). You should see your
new service listed in the group of services:
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:08 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:08 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
4. To test whether you are getting a log entry when the service is stopped, return PART V
to the Services node on the left side of the console. Right-click on ClockService
and select Stop from the menu. The service will then be stopped.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:08 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
You have now verified that your Windows service is running and is responding to the
start and stop commands.
• Service program This is the actual program code that you created at the
beginning of the chapter. This program tells the service what to do if stopped,
started, or sent any other type of command.
• Service control program In our example, the service control program was
found in the Computer Management console under Services. Through this
program, you are able to send the service commands, such as Start, Stop,
and so on.
• Service configuration program Services must be registered in the Windows
Registry, meaning that you must deploy a service as you did the one demonstrated
in the last section. A simple xcopy will not work. Using the installation program,
you can set configuration parameters and properties. For example, you can set
the startup mode—started, stopped, or disabled.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:08 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
This base class is necessary in order to communicate with the service control program,
which makes the necessary calls to the service’s Main() method, and through the
ServiceBase class calls the native methods that will ensure the successful start of
the service.
PART V
following.
The System.ServiceProcess namespace includes the following classes that are
required for a Windows service to operate:
• ServiceBase This is the parent class of your Windows service. This is the
class that handles the start and stop requests.
• ServiceProcessInstaller Along with ServiceInstaller, this class
is used to install and configure the service programs.
• ServiceController This class implements the service control program
and is used to send requests to services. See the next section for more information
on the ServiceController class.The configuration of your Windows service
can be done in the local Registry. Access the Registry by typing regedt32.exe
at the Run prompt (select Start | Run). This will open the Windows Registry
(we are using Windows 2000 Professional—check the documentation
for other versions of the Windows NT kernel). Locate the service in
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services,
and make any necessary changes (see the following illustration). You can
modify such properties as the type of service, the display name, startup
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:09 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
EXAM TIP A service must be configured in the Registry. All services can be
found in HKEY_LOCAL_MACHINE\System\CurrentControlSet\
Services. Using the regedt32 program, you can make modifications to
the service configuration.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:09 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:09 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
Some of the properties of the ServiceController class that you may find useful
are listed in Table 26-1. Figure 26-10 illustrates the members of the
ServiceControllerStatus enumeration, which is described in the table.
Property Description
ServiceName Identifies the name of the service.
MachineName Identifies the name of the machine running the service.
DisplayName Specifies the name that will be displayed for the service.
ServiceType Identifies the service type. If the service interacts with the desktop,
this will be InteractiveProcess. If the service runs along with other
services inside a shared process, ServiceType will be
Win32ShareProcess. Otherwise, a service running by itself in its own
process space will have a ServiceType of Win32OwnProcess.
Table 26-1 Properties of the ServiceController Class
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:10 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
The service can also be controlled programmatically by using the methods of the
ServiceController class listed in Table 26-2.
PART V
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:10 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
As a final note on Windows Services, let’s create an application that will use our ser-
vice and control the service through a service controller. Follow these steps:
3. Add a button to your form with the text “Start Service” (see Figure 26-33). Add
the following code to the Click event of the button.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:10 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
Server Components
In this section of the chapter, we will look at working with components that are handled
by a middle-tier server. In order to set the stage for this kind of .NET development, let’s
take a few moments to review the architecture of a multi-tiered solution. Chapter 10
covered this when dealing with data—most of the time, middle-tier components pro-
vide data services to a client component, but that doesn’t have to be the case.
Multi-tiered Architecture
An application can be broken down into three distinct services that can be provided by
the same program or split between two or more programs that work together to provide the
overall solution. These are the services:
• Presentation This service is responsible for the rendering of the user interface.
Typically, you can think of this service as the Windows or Web Form. The program
displays the interface on a client computer. Examples of this include programs
that allow data entry (a bank entering customer information), and that display
data (web or Windows pages that display customer details).
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:11 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
Single-Tier Model
A single-tier (1-tier) model is typical of a mainframe application. Dumb terminals gain
access to the mainframe computer, which is running the application. The application
combines all three services into a single location or a single program. All users of the ap-
plication must connect to the mainframe computer and access the application remotely.
Every process runs on the mainframe computer.
Since all of the services are located physically in one spot, there are limitations to
this model. The first and foremost is that there is a single point of failure—a network
breakdown or application bug causes the whole application to fail, and every user is
affected directly. Equally as important is the concern that a small change can cause.
One simple business-logic change can affect the application so greatly that the whole
thing needs to be redeveloped.
On the flip side of that, an application of this sort is comparatively easier to develop
because all the services are in one location and become easier to manage.
Two-Tier Model
Many years ago, developers introduced a new architecture designed to replace (or aug-
ment) the mainframe single-tier model. This is called client/server architecture, whereby
a client application connects to a server application. The server application handles the
back-end functionality, which in a 2-tier model would include database connectivity
and perhaps business logic.
Since the presentation service is now separated from the business and data services,
small logic changes are easier to manage and less intrusive to the client application.
However, in reality, true abstraction was not achieved. Some of the business logic re-
mained on the client, and some was removed to the server. This causes problems when
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:11 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
Three-Tier Model
Arising from the concerns of the 2-tier model came the establishment of the 3-tier
model. In this case, all three services are handled by different applications and, in most
cases, by three different machines. The client or front-end application provides the visual
interface for the user. Business logic is retrieved from components running on remote
servers (although this is not absolutely necessary—it can be found on the same ma-
chine). Business services attach to the back-end data store and retrieve the data for the
presentation service.
The advantages of this model are many. Different platforms can provide separate ser-
vice functionality. The client can be a browser-based application connecting to a mid-
dle-tier provided by an entirely different operating system. The middle tier can be
ignorant of the type of service on either side of it, meaning that the database engine is
not important to it, and the vehicle producing the user interface is immaterial to it. It is
a separate and distinct component that can be used by multiple applications.
-Tier Model
We now get to the crux of the matter—by taking the services and separating them (mak-
ing them completely distinct and non-cohesive), we now can take advantage of the
scalability that this model provides. The business service can be split amongst many
different applications or servers. Data services can be provided by multiple platforms
and data vendors. The interaction of these services brings us to the topic of the rest of
PART V
this chapter.
Components provide the functionality and separation that we have been talking
about in an n-tier model. Life would be wonderful if everyone in the development
world chose the same platform and techniques to produce these components, but that
is just not the case. We have spoken in earlier chapters about working with COM com-
ponents in Windows and web applications. In this chapter, we will look at implement-
ing a COM+ component, creating interfaces that are visible to COM, and working with
strongly named assemblies. The goal is to make sure that the middle-tier (the business
services) are dealt with in an n-tiered environment.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:11 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
Serviced Components
A serviced component, according to Microsoft’s definition, is “a class that is authored in
a CLS-compliant language and that derives directly or indirectly from the Sys-
tem.EnterpriseServices.ServicedComponent class.” This means that the
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:11 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
In order to create an environment whereby the developer can take advantage of .NET
Framework objects and COM+ services working together, you need to understand the
architecture of a COM+ application hosting serviced components. Figure 26-11 shows
the two environments and the boundaries created between them.
Services can flow between .NET objects and COM+ services. Each environment is re-
sponsible for the implementation and the execution of the object, but COM+ provides
the context for the object: transaction control, pooling, and so on.
EXAM TIP COM+ services include automatic transaction handling, JIT object
activation, security, object pooling, message queuing, and load balancing.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:11 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
• Message queuing COM+ services will deal with queuing messages that
may not be received by resources (for example, if a database server crashes
and doesn’t respond for a while). The queuing service records the method
calls so that they can be played back when the target server responds.
• Load balancing COM+ services will distribute object calls across a “farm”
of servers, thereby keeping one or more servers from becoming congested.
1. Start a new project using the Console Application template and add a reference
to System.EnterpriseServices.dll. Right-click on the project and select
Add Reference from the menu. The Add Reference dialog box will open, as
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:12 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
System.EnterpriseServices.ServicedComponent:
public class MyServicedComponent:
System.EnterpriseServices.ServicedComponent
3. Add a constructor to your class, like this:
public MyServicedComponent()
{
}
4. Specify the attributes that will work with the COM+ services. In order to
do this, you need to add attributes to the AssemblyInfo.cs file. (Review
Chapter 6 if you are unsure of how to do this). The following code listing
gives you an example:
[assembly: ApplicationName("MyServicedApplication")]
[assembly: Description("My First Serviced Application")]
The attributes that are available within the System.EnterpriseServices
namespace are listed in Table 26-3. In the table, you will see the scope value that specifies
whether the attribute belongs to the method, the class, or the assembly. You will also see
the unconfigured default value, which represents the value that COM+ assigns to the
field if the attribute is omitted from your code. The configured default value is the value
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:12 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
COM+ gives the field if you specify the attribute but give it no value. Check the MSDN
documentation for a full range of attributes.
The following code segment shows an example of coding to get the configured de-
fault value for LoadBalancingSupported:
using System.EnterpriseServices;
[LoadBalancingSupported]
public class MySC: ServicedComponent
{
static void Main()
{
}
}
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:12 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
using System.EnterpriseServices;
using System.Reflection;
[assembly: ApplicationName("CustomerComponent")]
[assembly: AssemblyKeyFileAttribute("CustomerComponentKeys.snk")]
namespace CustomerComponent
{
[Transaction(TransactionOption.Required)]
public class Customer: ServicedComponent
{
public Customer()
{
}
public void Update(string custCode)
{
// perform the database operations here
}
}
PART V
using CustomerComponent;
namespace CustomerComponentConsoleClient
{
public class CustomerClient
{
public CustomerClient
{
}
public static int Main()
{
Customer c = new Customer();
c.Update("Matthew");
}
}
}
Compile the two classes as follows (refer to the “Review of Strongly Named Assem-
blies” section for more information on the sn.exe utility):
sn –k CustomerComponentKeys.snk
csc /t:library /r:System.EnterpriseServices.dll Customer.cs
csc /r:Customer.dll CustomerClient.cs
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:13 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
EXAM TIP Using dynamic registration means that the assemblies are
not placed in the Global Assembly Cache (GAC). You must use manual
registration to place them in the GAC.
However, if you want to have a server application that is called by COM clients, you
must register the assembly manually. In order to do this, you use the .NET Framework
Services Installation tool—regsvcs.exe. The regsvcs.exe tool will manually reg-
ister an assembly that contains serviced components.
EXAM TIP Before you register the assembly, be sure to add the assembly
to the GAC using gacutil.exe. Review Chapter 6 for more information.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:13 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
sn –k CustomerComponentKeys.snk
This will create a file with public and private keys that will be used to give the assembly a
guaranteed unique name. Refer to Chapter 6 for more information on creating the
strong-name key file.
The next step is to tell your application to use the key. You do this by signing the as-
sembly (adding the key to the assembly). Locate the file in the Solution Explorer called
AssemblyInfo.cs and add the following line:
[assembly: AssemblyKeyFile("CustomerComponentKeys.snk")]
The compiler will now know to digitally sign the assembly using the keys found in
the .snk (strong-name key) file.
PART V
EXAM TIP You need administrative privileges to access the GAC.
Table 26-4 displays the options available with the GAC tool.
EXAM TIP Example of installing our serviced component into the GAC—
gacutil /i CustomerComponent.dll
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:13 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
In the Windows 2000 operating system, COM+ services are easier to manage. In Win-
dows NT, you may remember that you had to add the MTS (Microsoft Transaction
Server) from the Windows Option Pack. Fortunately, with Windows 2000, it is an integral
part of the operating system. Access the Component Services Tool from Start | Settings |
Administrative Tools (in Windows 2000 Professional). This will open a Component
Services console as shown in Figure 26-12.
Expand Component Services in the left panel, as shown in Figure 26-13. You will see
the hierarchical display on the left side and the details on the right side of the console.
By expanding COM+ Utilities, you can drill into the actual components and manage them.
Each component is represented by the round circle with a plus (+) sign in the middle
(see Figure 26-13). When a component is being accessed, the circle will spin. You can
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:14 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
Figure 26-13 Looking at the components
see the properties of the component by right-clicking on the component (in the right
pane) and selecting Properties (see Figure 26-14).
Figure 26-14 shows the General properties—name, description (this can be modi-
fied), the DLL, class ID (CLSID), and the application identifier. You can manage the
component using the following tabs on the Properties dialog box:
• Transactions In the Transactions tab (see Figure 26-15), you can specify the
transaction support—Disabled, Not Supported, Supported, Required, and
Requires New. You can also specify the transaction timeout if supported.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:14 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
• Security If the security attribute has been added to your component, you will
be able to configure the security using Security tab (see Figure 26-16). Here you
can set the authorization and the security roles.
• Activation Using the Activation tab (see Figure 26-17), you can set object
pooling properties, as well as JIT (just-in-time) activation. Remember that
these properties are all COM+ services.
• Concurrency In Figure 26-18, you can see that threading and synchronization
support can be configured through the Concurrency tab. Notice that our threading
model is set, by default, to Any Apartment. An apartment is a logical container
for processes.
Take a moment to look at the interfaces that have been created to make your compo-
nent visible to COM. Figure 26-19 shows the Component Services console tree ex-
panded to dig deeper into the CustomerComponent.Customer component.
By opening the Interfaces folder (see Figure 26-19), you can see that behind the
scenes a number of interfaces have been created to work with COM. By right-clicking on
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:14 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:14 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:15 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
Figure 26-19 Interfaces of the component
that can be set for each particular interface (see Figure 26-20). Each interface exposes the
methods specific to the COM requirements. We have opened the Object interface
(which is not really too exciting, but useful for our demonstration).
In Figure 26-21 you can see the methods of the Object class exposed to COM. This
is the crux of the whole process—our .NET Framework component has been resolved in
order to expose interfaces that are visible to COM.
Summary
When building enterprise applications, you can take advantage of the support that
COM+ services gives to your components. In this chapter, we have reviewed creating
and working with Windows services (components that run in the background of the cur-
rent Windows session) and serviced components (components that are run on a server
and accessed using COM+ services).
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:15 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
In this chapter, you need to be sure that you know how to do the following for the
exam:
Test Questions
1. You have created a serviced component that will interface with COM+ services.
You want to install the component in the Global Assembly Cache. Which utility
will allow you to do this?
A. gacutil.exe
B. regsvsc.exe
C. install.exe
D. sc.exe
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:15 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
Figure 26-21 Exposed methods of the Object class
2.
You are creating a Windows service for Windows ME. You want to install the
service in the Registry. What utility will do this for you?
A. gacutil.exe
B. regsvsc.exe
C. sc.exe
D. installer.exe
E. None of the above.
3. Which project template will allow you to create a background service in
Visual Studio .NET?
A. Windows service.
B. Web service.
C. Windows application.
D. Service.
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:15 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:15 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
You want to register the component manually. Which utility will allow you
to do this?
A. gacutil.exe
B. regsvsc.exe
C. install.exe
D. sc.exe
6. You have created a serviced component that will interface with COM+ services.
You want to register the component automatically. Which utility will allow you
to do this?
A. gacutil.exe
B. regsvsc.exe
C. xcopy.exe
D. sc.exe
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:16 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
B. public MyService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("Application")
{
System.Diagnostics.EventLog.CreateEventSource ("Application", "Application");
}
eventLog1.Source="Application";
eventLog1.Log = "Application";
}
protected override void OnStop (string[] args)
{
eventLog1.WriteEntry ("Here we are!");
}
C. public MyService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("System")
{
System.Diagnostics.EventLog.CreateEventSource ("System", "System");
}
eventLog1.Source="Application";
eventLog1.Log = "Application";
}
protected override void OnStart (string[] args)
{
eventLog1.WriteEntry ("Here we are!");
}
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:16 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
8. Where would you find the Add Installer link within Visual Studio .NET?
A. Under the Project menu.
B. Under the Build menu.
C. In the Toolbox.
D. In the Properties Explorer window.
9. Which of the following methods will install a Windows service? Choose all
that apply.
A. xcopy.exe
B. regedit.exe
PART V
C. setup.exe
D. service.exe
10. A Windows service must inherit from which class?
A. System.Service.ServiceBase
B. System.ServiceProcess.Service
C. System.ServiceProcess.ServiceBase
D. System.Service.Service
11. Which Registry key would lead you to find the installed Windows service?
A. HKEY_LOCAL_MACHINE\Services
B. HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services
C. HKEY_LOCAL_MACHINE\System\Services
D. HKEY_LOCAL_MACHINE\CurrentControlSet\Services
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:16 AM
Color profile: Generic CMYK printer profile
Composite Default All-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
26
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:16 AM
Color profile: Generic CMYK printer profile
All-In-One / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter
Composite Default screen
26
PART V
P:\010Comp\All-in-1\443-6\ch26.vp
Monday, August 26, 2002 11:53:16 AM