Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
 
 
Windows XP

Visual Basic 2008 : Using FTP in the Service (part 2)

11/10/2011 11:33:08 AM

Modifying the Configuration File

We need to modify the configuration file to represent the changes in our service. We need to be able to store and then retrieve the information about the properties we have defined in our FTP class. Listing 4 shows the new configuration file.

Listing 4. FTP configuration file schema.
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<FTPUris>
<FTPUri>
<URI>FTP://mgern1/download</URI>
<LocalFolder>c:\temp\ftpsave</LocalFolder>
<User>anonymous</User>
<Pwd>anonymous@anon.com</Pwd>
<Port>21</Port>
<File>mydata.dat</File>
</FTPUri>
</FTPUris>
</Configuration>

The configuration file matches all the required property fields that we added to our FTP class.

Modifying the <Tutorials.ThreadFunc> Method

We once again have to modify the <ThreadFunc> method to retrieve and then assign the property values from the configuration file to the correct property of the FTP class instance that we created. Listing 5 shows the new <ThreadFunc> method.

Listing 5. The modified <ThreadFunc> method.
Private Sub ThreadFunc()
Try
'Load our Configuration File
Dim Doc As XmlDocument = New XmlDocument()
Doc.Load(My.Settings.ConfigFile)

Dim Options As XmlNode
'Get a pointer to the Outer Node
Options = Doc.SelectSingleNode("//*[local-name()=FTPUris]")

If (Not Options Is Nothing) Then
Dim tmpOptions As System.Xml.XPath.XPathNavigator =
Options.FirstChild.CreateNavigator()
If (Not tmpOptions Is Nothing) Then
Dim children As System.Xml.XPath.XPathNavigator
Do
Try
Dim tmpFTP As New FTP(m_ThreadAction)

children = tmpOptions.SelectSingleNode("URI")
tmpFTP.URI = children.Value

children = tmpOptions.SelectSingleNode("LocalFolder")
tmpFTP.LocalFolder = children.Value

children = tmpOptions.SelectSingleNode("File")
tmpFTP.File = children.Value

children = tmpOptions.SelectSingleNode("Port")
tmpFTP.Port = children.Value

children = tmpOptions.SelectSingleNode("User")
tmpFTP.User = children.Value

children = tmpOptions.SelectSingleNode("Pwd")
tmpFTP.Pwd = children.Value

m_WorkerThreads.Add(tmpFTP)
tmpFTP.Start()
Catch ex As Exception
WriteLogEvent(ex.ToString(), CONFIG_READ_ERROR,
EventLogEntryType.Error, My.Resources.Source)
End Try
Loop While (tmpOptions.MoveToNext)
End If
End If
Catch ex As Exception
WriteLogEvent(ex.ToString(), ONSTART_ERROR, EventLogEntryType.Error,
My.Resources.Source)
Me.Stop()
End Try
End Sub


This new code will open the configuration file and then navigate to the starting parent node of FTPWorkerOptions. When we have a pointer to that node, the code will read through each sibling and populate the appropriate FTP class property with the value read from the configuration file. After all the values have been set, the FTP class <Start> method is called, and monitoring for files and downloading of files commences.

Updating the Tutorials <OnStop> Method

We need to update the <OnStop> method to clean up the service threads for each class instance created. The updated method is displayed in Listing 6.

Listing 6. The updated <OnStop> service method.
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
Try
If (Not m_WorkerThread Is Nothing) Then
Try
WriteLogEvent(My.Resources.ServiceStopping, ONSTOP_INFO, _
EventLogEntryType.Information, My.Resources.Source)

m_ThreadAction.StopThread = True

For Each ftp As FTP In m_WorkerThreads
Me.RequestAdditionalTime(THIRTY_SECONDS)
ftp.Incoming.Join(TIME_OUT)
Next
Catch ex As Exception
m_WorkerThread = Nothing
End Try
End If
Catch ex As Exception
'We Catch the Exception
'to avoid any unhandled errors
'since we are stopping and
'logging an event is what failed
'we will merely write the output
'to the debug window
m_WorkerThread = Nothing
Debug.WriteLine("Error stopping service: " + ex.ToString())
End Try
End Sub


Service Validation

To test the new functionality, make sure that you have the service installed and the properly formatted configuration.xml file. After you run the service you should copy some files with the names that you wanted to download into the folder. You will notice a few things immediately.

First, you have no way to delete the remote file, so once you download it locally it is there forever, and you will start to see errors the next time you try to download the file because it can’t be saved again locally. Because I have not written my code to append to any already existing file, you should modify the code to either delete any existing file locally, delete the file remotely, or name the file something unique. If you choose this last option, I suggest that you use a GUID, because it is possible for two threads to try to write a file with the same name at the same time. If you try to do this, the file will overwrite itself.

The second thing you’ll notice is that the service only allows you to specify a single file to download, even though the service itself—or more specifically the FTP class—has the ability to download an array of passed-in file names. You can modify the service configuration file to pass in more than one file, and then have the <ThreadFunc> supply the FTP.Files property instead with an array of file names.

The last thing you’ll notice is that you cannot pass in a wildcard. You can allow for wildcards by querying the remote directory for the files in advance and then using the <GetFiles> method to download all the queried files.

Other -----------------
- Windows Presentation Foundation in .NET 4 : Introducing WPF - The Architecture of WPF
- Windows Presentation Foundation in .NET 4 : Introducing WPF - Resolution Independence
- Windows Presentation Foundation in .NET 4 : Introducing WPF - The Evolution of Windows Graphics & A Higher-Level API
- Silverlight and ASP.NET : WCF Services and Silverlight
- Silverlight and ASP.NET : Integrating with HTML & Animations
- Silverlight and ASP.NET : Silverlight and Layout
- Silverlight and ASP.NET : Adding Silverlight Content to a Web Page
- Silverlight and ASP.NET : XAML
- Silverlight and ASP.NET : Creating a Silverlight Application
- Microsoft ASP.NET 4 : Developing a Web Part
- Microsoft ASP.NET 4 : The Web Parts Architecture
- Microsoft ASP.NET 4 : Handlers and Session State & Generic Handlers (ASHX Files)
- Microsoft ASP.NET 4 : HTTP Handlers - Handlers and IHttpHandler
- Microsoft ASP.NET 4 : HTTP Handlers - The Built-in Handlers
- Microsoft ASP.NET 4 : ASP.NET Request Handlers
- Microsoft ASP.NET 4 : HttpModules (part 2) - Seeing Active Modules & Storing State in Modules
- Microsoft ASP.NET 4 : HttpModules (part 1) - Existing Modules & Implementing a Module
- Microsoft ASP.NET 4 : The HttpApplication Class and HTTP Modules - Overriding HttpApplication
- Microsoft ASP.NET 4 : Diagnostics and Debugging - Error Pages
- Microsoft ASP.NET 4 : Diagnostics and Debugging - Debugging with Visual Studio
 
 
Most view of day
- Maintaining Desktop Health : Monitoring Reliability and Performance (part 3)
- Microsoft Exchange Server 2010 : Managing Connectivity with Hub Transport Servers - Messages in Flight
- Microsoft Content Management Server Development : A Placeholder Control to Store All HTML Tags (part 2)
- Microsoft Content Management Server : Implementing Server-Side Validation
- Windows Phone 8 : Configuring Basic Device Settings - Accessing the Device Settings Screen - Changing the Device Theme
- Maintaining Windows Home Server 2011 : Deleting Unnecessary Files from the System Drive
- Using Voice and Sounds : Directing Your Computer with Voice Commands, Dictating Text
- Microsoft Project 2010 : Tracking Progress on Tasks (part 3) - Entering a Task’s Completion Percentage
- Microsoft Project 2010 : Tracking Progress on Tasks (part 4) - Entering Actual Values for Tasks
- Microsoft Visio 2010 : Creating and Validating Process Diagrams - Understanding BPMN, Creating and Validating BPMN Diagrams
Top 10
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 3) - Creating IPv4 DHCP Scopes
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 2) - Installing DHCP Server and Server Tools
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 1)
- Windows Server 2012 : DHCP,IPv6 and IPAM - Understanding the Components of an Enterprise Network
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 3) - Translating Text with the Mini Translator
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 2) - Translating a Word or Phrase with the Research Pane
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 1) - Setting Options for the Research Task Pane, Searching with the Research Task Pane
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 2) - Ending a Linked Notes Session, Viewing Linked Notes
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 1) - Beginning a Linked Notes Session
- Microsoft OneNote 2010 : Doing Research with Side Notes (part 3) - Moving Side Notes to Your Existing Notes
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro