You are previewing Programming Windows® Services with Microsoft® Visual Basic® 2008.
O'Reilly logo
Programming Windows® Services with Microsoft® Visual Basic® 2008

Book Description

Get the practical reference to developing Windows services with Visual Basic 2008. Addressing the leading trend of software as a service, this guide illustrates how developers with intermediate Visual Basic skills can design and implement Web services. It addresses a critical gap in the developer literature today, showing that services can be developed with Visual Basic 2008 and are not restricted to XML-based applications or Microsoft Visual C#. Software as a service is changing the dynamic between rich and thin clients. As device types multiply, users expect always-on connectivity to link them to the robust Web and corporate applications they use on the desktop. Services, which offload data storage and processing tasks to a server, run efficiently on Web platforms on any device. Services do not need to be a complex coding task and this is the ideal resource for creating modern, service-oriented applications. It covers integration of services with other Microsoft technologies, including Microsoft BizTalk Server, Microsoft Internet Information Services (IIS), and Microsoft SQL ServerTM. Plus, you ll get extensive code snippets and sample applications.

Key Book Benefits

Delivers practical guidance on how to develop Windows services using Visual Basic 2008

Covers integration of services with other Microsoft platforms including BizTalk, IIS, and SQL Server

Features extensive code snippets and sample applications

Visit the catalog page for Programming Windows® Services with Microsoft® Visual Basic® 2008Visit the errata page for Programming Windows® Services with Microsoft® Visual Basic® 2008Visit the supplemental content page for Programming Windows® Services with Microsoft® Visual Basic® 2008

Table of Contents

  1. Programming Windows® Services with Microsoft® Visual Basic® 2008
    1. Introduction
      1. Who This Book Is For
      2. How This Book Is Organized
      3. System Requirements
      4. Find Additional Content Online
      5. The Companion Web Site
      6. Support for This Book
      7. Questions and Comments
    2. I. Defining Windows Services
      1. 1. Writing Your First Service in Visual Basic 2008
        1. Generating the Project
          1. Renaming Our Project Files
        2. Understanding the Wizard Code
          1. The <OnStart> Method
          2. The <OnStop> Method
          3. Other Events
            1. Adding <OnPause>
            2. Adding <OnContinue>
        3. Writing Our First Code
          1. Modifying the <OnStart> Method
          2. Modifying the <OnStop> Method
          3. Modifying the <OnPause> Method
          4. Modifying the <OnContinue> Method
        4. Making the Service Installable
          1. Setting the Service Properties
          2. Setting the Startup Options
          3. Additional Configuration Options
            1. Adding a Signature Key File
            2. Adding Security-Specific Settings
        5. Building, Installing, and Deploying
          1. Creating Your Service Storage Location
          2. Verifying That You Have .NET 2.0 Installed
          3. Verifying That Your Service Is Installed
          4. <OnStart> Message
          5. <OnPause> Message
          6. <OnContinue> Message
          7. <OnStop> Message
        6. Summary
      2. 2. Expanding Your Service with Threads
        1. Cleaning Up the Service from the Previous Chapter
        2. Understanding Threads
          1. Determining How Many Threads to Create
          2. Thread Synchronization
          3. Creating Threads
          4. Thread Methods
        3. The New Code
          1. Thread Function Code
          2. Event-Logging Code
          3. Updating the <OnStart> Method
          4. Updating the <OnStop> Method
          5. Updating the <OnPause> Method
          6. Updating the <OnContinue> Method
          7. Updating the Thread Method
          8. Executing the Thread
          9. Updating <OnStart>
        4. Install and Test Your Service
        5. What Is Thread Cleanup?
          1. Thread Cleanup Availability
          2. Threads and Accessibility
          3. A Problem with the Current <OnStart> Thread
            1. Fixing the Thread-Scope Issue
            2. Creating the Thread in <OnStart>
          4. Thread Cleanup
            1. Cleaning Up the <OnStop> Method
            2. About Thread Abort
          5. Making Thread Cleanup Useful
            1. Adding Code to ThreadAbortException
          6. Keeping the Thread Alive
          7. Install and Verify
        6. Extending <OnPause> and <OnContinue>
          1. Ways to Control Thread Processing
            1. Thread Suspension
          2. Updating <OnPause>
          3. Updating <OnContinue>
          4. Using Thread State Control
          5. Updating <OnPause>
          6. Updating <OnContinue>
          7. Updating <OnStop>
          8. Updating <ThreadFunc>
          9. Importance of the THREAD_WAIT Value
        7. Summary
      3. 3. Services and Security
        1. Security Privileges and Services
          1. Service Account Security
          2. Local System
          3. User Accounts
          4. Securing the Service
          5. Protecting Data
            1. Access to the Data Store That Holds the Customer Information
              1. As a User
              2. Impersonation
            2. Access to the Directory to Which the Service Downloads the Information Before Processing
            3. Access to the Secondary Data Store That Will Hold the Customer Information
            4. Access to the Temporary Data and Data Connections
        2. Summary
    3. II. Creating Interactive Windows Services
      1. 4. Services and Polling
        1. Polling the File System
        2. Adding a Module File
          1. Adding Event Log InstanceIds
        3. Adding New Polling Code
          1. Introduction to Instrumenting a Resource File
            1. Creating the Resource File
            2. Adding Previous Literals to the Resource File
        4. Updating the Service Events
          1. Modifying Our <OnStart>
          2. Modifying <OnStop>
          3. Modifying <OnPause>
          4. Modifying <OnContinue>
        5. Writing a New Thread Method
          1. Install and Verify
        6. Monitoring with Multiple Threads
          1. Expanding Processing
          2. Creating the Code
            1. Modifying the <ThreadFunc> Method
          3. Installation and Verification
            1. Changing the StartUp Account
        7. Extending the Threading Model
          1. Monitoring More Than One Folder
          2. Monitoring More Than One File Type Per Folder
          3. Outputting to More Than One Folder
          4. Processing More Than One File Type Per Folder
          5. When Complexity Steps In
        8. Adding a FileWorker Class
          1. Designing a New Class File
            1. Adding a Constructor
            2. Adding a Worker Thread
            3. Adding the Worker Thread Function
            4. Adding the Thread Action State
            5. Adding a WriteLogEvent
            6. Adding Our <Start> Method
        9. Implementing the Worker Class
          1. Adding a FileWorker Collection
          2. Adding New File Type and Input Locations
        10. Creating the FileWorkerOptions Class
          1. Updating the FileWorker Constructor
          2. Updating Our <FileWorker.ThreadFunc>
        11. Updating <Tutorials.ThreadFunc>
          1. Updating the <OnStop> Method
          2. Installation and Verification
        12. Using Configuration Files
          1. Application-Specific Configuration File
          2. The Application-Processing Configuration File
        13. Updating <Tutorials.ThreadFunc>
          1. Installation and Verification
        14. Summary
      2. 5. Processing and Notification
        1. SMTP Notifications
        2. File Processing
          1. Configuring Our New SMTP Class
          2. Updating the FileWorkerOptions Class
          3. Updating the FileWorker Class
            1. Updating the FileWorker Class Constructor
            2. Updating the <FileWorker.ThreadFunc> Method
          4. Updating the Tutorials Class
            1. Updating the <Tutorials.ThreadFunc> Method
          5. Installation and Verification
        3. Advanced Processing
          1. Exploring Processing Options
          2. Optimizing Processing
            1. Scenario 1: Synchronization with Threads and Queues
            2. Scenario 2: Optimizing Peformance with Delegates and Events
            3. Scenario 3: Combing Synchronized Queues and Eventing
            4. Deciding on the Best Solution
        4. Implementing a Solution
          1. Creating a New <FileWorker.ProcessingIncoming> Method
            1. Creating a New <FileWorker.ProcessFiles> Method
            2. Updating FileWorker Class Thread Property Support
          2. Updating the <Tutorials.OnStop> Method
          3. Queueing E-mail Notifications
          4. Decoupling Notifications Implementation Questions
            1. Do Notifications have to be successful before a file is moved?
            2. Can notifications be asynchronous to the file being moved?
          5. Decoupling: An Example
          6. SMTP Queueing Solution
            1. The Queue
            2. The Data
          7. Updating the SMTP Class
          8. Updating the FileWorkerOptions Class
          9. Updating the <Tutorials.ThreadFunc> Method
          10. Updating the <FileWorker> Constructor
            1. Balance and Performance
          11. Installation and Verification
        5. Summary
      3. 6. User Input, Desktop Interaction, and Feedback
        1. Understanding Service Feedback
        2. Configuring a Service to Interact with the Desktop
          1. Getting Started with Creating the Interactive Service
          2. Creating a Feedback Form
            1. Adding Text Selection Code
            2. Adding a Delegate Method
            3. Adding Required References
          3. Making the Form Visible to the Service
          4. Updating the FileWorker Class
            1. Creating a Delegate
            2. Creating the <FrmThread> Method
            3. Creating a Thread Instance in FileWorker Class
            4. Updating <FileWorker.Start>
            5. Adding the UpdateForm Method to the FileWorker Class
            6. Updating <FileWorker.ThreadFunc>
          5. Install, Configure, and Verify
        3. Summary
      4. 7. Data Logging: Processing and Storing Data in SQL Server 2005
        1. Configuring Microsoft SQL Server
          1. Creating a Tutorials Database
          2. Creating a Users Table
          3. Creating a User Stored Procedure
        2. Understanding a LINQSQL Class
        3. Using LINQ To SQL
          1. Creating the Users Table Data Entity Class with LINQ To SQL File
          2. Creating a SQL Class
        4. Updating the FileWorker Class
          1. Updating <FileWorker.ProcessFiles>
          2. Install and Verify
        5. Data Tracking Validation
          1. Creating Process Error Folder
          2. Error Processing Solution
          3. Updating the FileWorkerOptions Class
          4. Updating Our configuration.xml File
          5. Updating the FileWorker Class
            1. Updating the FileWorker Class Constructor
          6. Updating the <Tutorials.threadFunc> Method
        6. Implementing the Record Failure Code
          1. Adding a Process Failure Method
          2. The Worker Thread
          3. Install and Verify
        7. Data Migration from One Data Store to Another Data Store
          1. Creating the Back-End Data Store
          2. Creating a New Connection String
            1. Updating the LINQSQL Class Connectionstring
            2. Updating InsertRecord Implementations
            3. Implementing a Dispose Method for the LINQSQL Class
            4. Updating the <FileWorker.ProcessFiles> Method
          3. Creating a New <ProcessRecords> Method
            1. Updating the FileWorker Class
            2. Creating the New <LINQSQL.GetUsers> Method
          4. Install and Verify
        8. Reporting Processing Failures
        9. Optimizing the LINQSQL Class
          1. Install and Verify
        10. Summary
    4. III. Services That Support IT and the Business
      1. 8. Monitoring and Reporting with WMI
        1. Using WMI with Services
          1. WMI Architecture
            1. Adding the WMI Class Reference
          2. Creating the Generic WMI Class
          3. Understanding WMI Classes and Their Uses
          4. Specific WMI and Custom Classes
        2. Using the WMI Class
          1. Adding New EventLog Constants
          2. Updating <Tutorials.ThreadFunc>
          3. Adding the WMI Property Reader Method
        3. Extending the WMI Implementation
          1. Extending the WMI Class
            1. New WMI Member Variables
            2. New and Updated WMI Class Member Methods
              1. The <New> Constructor
              2. The WMIWorkerOptions Class
              3. The WMI Properties
          2. Creating the WMIWorkerOptions Class
            1. The Query Property
            2. The WMIRoot Property
            3. The Server Property
          3. Creating the Configuration File
          4. WMI Service Account
        4. WMI System Monitoring
          1. Updating the Configuration File
          2. WMI Win32_Process Usage
          3. Updating the WMIWorkerOptions Class
          4. Updating the <Tutorials.ThreadFunc> Method
          5. Updating the <Query> Configuration Value
          6. Updating the <WMI.ProcessWMIRequest> Method
          7. Service Function Validation
        5. Service Notification
          1. Updating the Configuration.xml File
          2. Updating the WMI Class
          3. Updating the WMIWorkerOptions Class
          4. Updating the <Tutorials.ThreadFunc> Method
          5. Updating the <WMI.ProcessWMIRequest> Method
          6. Service Validation
        6. Summary
      2. 9. Talking to the Internet
        1. Reading and Parsing ASP Pages
          1. Creating the ASP Master Page
          2. Calling the ASP Master Page
            1. The <MonitorUrl> Method
            2. The <CheckStatus> Method
          3. Application Log InstanceIds
          4. Storing ASP Page URL Monitoring Status
            1. Creating the URLS Status Table
            2. Creating the UpdateURLStatus Stored Procedure
          5. Creating LINQ To SQL Dependencies
            1. Adding a Table and Stored Procedure to the Canvas
            2. Creating the Wrapper LINQ Class
              1. The Constructor
              2. The <InsertRecord> Method
              3. The <GetUrls> Method
          6. Updating the Configuration File
          7. Updating the <Tutorials.TThreadFunc> Method
          8. Updating the Tutorials <OnStop> Method
          9. Service Validation
        2. Adding a Dynamic Status ASPX Page
          1. Creating a New ASP.NET Web Application
            1. Creating the SQL Datasource
            2. Adding a GridView Control
          2. Validating the Web Site
        3. FTP and Your Service
        4. Using FTP in the Service
          1. Creating FTP Directories
          2. Adding an FTP Class
            1. The <DownloadFiles> Method
            2. FTP Class Properties
            3. Adding New FTP Constants
          3. Modifying the Configuration File
          4. Modifying the <Tutorials.ThreadFunc> Method
          5. Updating the Tutorials <OnStop> Method
          6. Service Validation
        5. Uploading Data Using FTP
          1. Updating the FTP Class
          2. Updating the Configuration File
          3. Updating the <Tutorials.ThreadFunc> Method
          4. Updating the FTP Class
          5. Updating the <Start> Method
          6. Service Validation
        6. Summary
      3. 10. Services That Listen
        1. Listening with TCP/IP
          1. Design Points for Service Listeners
          2. Creating the First Listener Service
          3. Coding the Service Listener
            1. Adding a Configuration File
          4. Creating a Listener Class
            1. Listener Class Properties
            2. The <StartListener> Method
            3. The <StartListener> Processing Loop
          5. Listener Processing Methods
            1. The <ProcessCommand> Method
            2. The <GetDateTime> Method
            3. The <GetServiceStatus> Method
            4. The <GetProcessList> Method
          6. Updating the <Tutorials.ThreadFunc> Method
          7. Updating the Tutorials <OnStop> Method
        2. Service Validation
          1. The Test Client
        3. Allowing Multiple Connections
          1. Extending the Listener Class
            1. The <SocketThread> Method
          2. Updating the <StartListener> Method
          3. Service Validation
        4. Summary
      4. 11. Advanced Security Considerations and Communications
        1. What Does Securing the Service Mean?
          1. Service Logon Privileges
          2. Securing Your Service’s Configuration
            1. SQL Server Logon Information
            2. Remote File Locations
            3. Remote Server Identities
            4. File or Data Attributes
          3. Options for Securing Configuration Data
            1. Hash Values
            2. Private and Public Keys
            3. Impersonation
            4. Restricting Access and ACLs
            5. Using Windows Authentication for SQL Server
          4. A Closer Look at Security Options
            1. Restricted Access and ACLs
              1. The Registry
              2. File Folders
              3. Windows SQL Server Authentication
              4. Certificates, Impersonation, and Configuration Access
              5. Using Hash Values
        2. Services as Clients
          1. Securing the HTTP Client Service
            1. Updating the Configuration File
              1. The URL Property
              2. The AuthType Property
              3. The AuthMethod Property
              4. The UserName Property
              5. The Password Property
            2. Sample Configuration File
            3. Updating the Http Class
              1. Adding New Properties
              2. Updating the <CheckStatus> Method
            4. Updating the <Tutorials.ThreadFunc> Method
              1. Configuring the Web Server
          2. Securing the FTP Service
            1. Securing the Transport Channel and Authentication Method
              1. Updating the Configuration File
            2. Updating the FTP Class
              1. Updating FTP Class Properties
              2. Updating the <SendFile> Method
              3. Updating the <GetFile> Method
              4. Updating the <Tutorials.ThreadFunc> Method
          3. Securing the SMTP Client Class
            1. Securing the Transport Channel and Authentication Method
              1. Updating the Configuration File
              2. Updating the EmailDetail Properties
              3. Updating the SMTP Class Methods
              4. Updating the <Tutorials.ThreadFunc> Method
          4. Writing Secure Code
            1. Exception Handling
              1. Catching the Proper Exceptions
              2. Throwing Exceptions
              3. Determining Exceptions
              4. Understanding the Exception Stack
              5. Microsoft Recommendations
          5. Securing In-Memory and On-Disk Data
          6. Using SSL with Server Services
            1. Using TcpClient and TcpListener Classes
              1. Creating a Certificate
              2. Using Certificates in Code
            2. Updating the Configuration File
            3. Updating the Listener Class
              1. Adding the Proper Imports
              2. Adding New Listener Properties
              3. Creating the ClientStream Class
              4. Adding the ClientStream Class to the Listener Class
              5. Updating the <StartListener> Method
              6. Updating <SocketThread> Method
              7. Updating the <Tutorials.ThreadFunc> Method
          7. Updating the Test Client to Use SSL
            1. Using TcpClient with SSL
              1. Adding the Imports
              2. Adding the Server SSL Certificate Validation Callback Method
              3. Updating the Client Request Code
        3. Summary
    5. IV. Advanced Windows Services Topics
      1. 12. Scheduling, Configuring, Administering, and Setting Up Windows Services
        1. What Does Scheduling Mean?
          1. Scheduling Options
          2. Permission Requirements
          3. Determining the Type of Scheduling to Use
            1. The Event-Driven Model
            2. The Poll-Driven Model
        2. Administration of Services
          1. Types of Configuration Data
            1. Service Configuration Data
              1. Number of Active Threads
              2. Global Configuration Data
              3. Location of User Configuration Data
              4. Storing Service-Specific Configuration Data
            2. Creating Application Settings
            3. Accessing Application Settings
            4. User Configuration Data
          2. Advanced Service Administration
            1. Dynamic Service Configuration
              1. Dynamic File Configurations
              2. Microsoft Database Dynamic Configuration
            2. Administrative UI
        3. Installing Services
          1. Adding the Setup Project
            1. Add the Project Output
            2. Creating Custom Actions
              1. Creating a Service Custom Action
              2. Creating a Debug Symbol Custom Action
            3. Adding a Configuration File
            4. Testing the Setup Project
        4. Summary
      2. 13. Debugging and Troubleshooting Windows Services
        1. Debugging Services
          1. Using the Visual Studio IDE
          2. Writing Your Service as a Console Application
        2. Troubleshooting and Monitoring Services
          1. Task Manager
            1. Viewing Statistics in Task Manager
            2. Understanding Task Manager Columns Usage
              1. CPU Utilization
              2. Memory Utilization
              3. Handles
        3. Performance Monitor
          1. Performance Counter Consumers
          2. Using Perfmon as a Performance Counter Consumer
          3. Standard System Exposed Performance Counters
          4. Viewing Perfmon
        4. Examples of Debugging and Monitoring Your Service
          1. High CPU
            1. Debugging the High CPU Problem
              1. Viewing the Current Threads
              2. Viewing the Selected Threads Active Source Code
        5. Summary
      3. 14. Adding Performance Counters
        1. Types of Performance Counters
          1. Operating System-Exposed Counters
            1. The Process Category
              1. Threads
              2. Handles
              3. Private Bytes
              4. Virtual Memory
              5. CPU Usage
              6. Elapsed Time
              7. ID Process
          2. Application-Specific Counters
        2. Adding Counters to Your Service
          1. Creating Your Counters
            1. The PerformanceCounterCategory Class
              1. Defining the Counters
              2. Creating the CounterCreationDataCollection Class
            2. Creating Counters with the PerformanceCounterCategory Class
            3. Multi-Instance Counters vs. Single Instance Counters
        3. Implementing Our Counters in Code
          1. Creating Instances of Counters
          2. Updating Counter Values
            1. Incrementing the Counter
            2. Decrementing the Counter
          3. Sample Service with Performance Counters
          4. Service Validation
        4. Summary
    6. V. Appendices
      1. A. Microsoft Internet Information Server (IIS)
        1. Installing Microsoft IIS
          1. Installing IIS on Windows Vista Ultimate
          2. Installing IIS on Windows XP
      2. B. Microsoft File Transfer Protocol Service
        1. Installing Microsoft FTP Service
          1. Installing the FTP Service on Windows Server 2003
          2. Installing the FTP Service on Windows Vista Ultimate
          3. Installing FTP Services on Windows XP
      3. C. Microsoft SMTP Service
        1. Installing SMTP Services on Windows XP
    7. D. About the Author
    8. Index