.NET Web Services Solutions
|
|
Using a Web Service to Coordinate a Shared File’s Use
To manage program source code within projects that allow multiple programmers to access and edit programs, many organizations employ source-code control systems. In general, the systems control which programmers can access specific source-code files and how. In this way, two programmers do not unknowingly make changes to the same file at the same time. In a similar way, many project-team environments exchange Word documents, Excel documents, and other documents on a regular basis. To better manage which team members can view or change a project document’s contents, many companies use “librarian-like” software that lets users “check out” a project document for read or write operations and then later return the document after the user finishes editing it.
By using a web service, you can build a similar file-control system. The FileLibrary web service in Listing 13.10 lets users check files into and out of the server’s ProjectFiles folder. To use the web service, you must create the ProjectFiles folder and CheckedOut subfolder on your server. The web service provides the following methods:
string() AvailableFiles() byte() CheckoutFile(String Filename) Integer FileUpload(Byte() FileData(), String Name)
To create the FileLibrary web service, perform these steps:
-
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
-
Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name FileLibrary. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.
-
Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 13.10.
Listing 13.10 FileLibrary.asmx.vb
Imports System.IO <WebMethod()> Public Function AvailableFiles() As String() AvailableFiles = Directory.GetFiles("C:\ProjectFiles") End Function <WebMethod()> Public Function CheckoutFile(ByVal Filename As String) _ Ä As Byte() Dim InputFile As FileStream Dim FileLength As Long Try InputFile = File.Open(Filename, FileMode.Open, _ Ä FileAccess.Read) FileLength = InputFile.Length Dim DataBuffer(FileLength - 1) As Byte InputFile.Read(DataBuffer, 0, FileLength) InputFile.Close() File.Move(Filename, "C:\ProjectFiles\CheckedOut\" & _ Ä Path.GetFileName(Filename)) CheckoutFile = DataBuffer Catch Ex As Exception Throw New Exception("Unable to open specified file " & Filename) End Try End Function <WebMethod()> Public Function FileUpload(ByVal FileData() As Byte, _ Ä ByVal Name As String) As Integer Dim Status As Integer = 0 Dim OutputFile As FileStream Dim FileLength As Long Try OutputFile = File.Open("C:\ProjectFiles\" & Name, _ Ä FileMode.Create, FileAccess.Write) FileLength = FileData.Length OutputFile.Write(FileData, 0, FileLength) OutputFile.Close() Status = 1 If (File.Exists("C:\ProjectFiles\CheckedOut\" & Name)) Then File.Delete("C:\ProjectFiles\CheckedOut\" & Name) End If Catch Ex As Exception Status = 0 End Try FileUpload = Status End Function
The web service provides three methods:
-
The first, AvailableFiles, returns an array of character strings that contain the names of files available in the ProjectFiles folder.
-
The second, CheckoutFile, lets a user retrieve a file from the ProjectFiles folder. After the method returns the file, it moves the file into the CheckedOut folder, which makes the file no longer available for users to check out.
-
The final method, FileUpload, lets a user upload a file to the ProjectFiles folder. If a user has previously checked out the file (meaning the file would reside in the CheckedOut folder), the method deletes the old copy.
Ideally, a file management program would use a database to track which users checked out each file (and would then allow only the user who had checked out the file to upload a new copy). Likewise, the program might restrict users to checking out files for either read or write access. Further, a more robust system would also allow for multiple versions of a file to exist.
The Visual Basic .NET program in Listing 13.11, AccessProjectFiles.vb, uses the FileLibrary web service to control access to the server’s ProjectFiles folder. When you run the program, it will display a form you can use to upload or download files to or from the server’s ProjectFiles folder, as shown in Figure 13.10.
To create the AccessProjectFiles program, perform these steps:
-
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
-
Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name AccessProjectFiles. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls.
-
Using the Toolbox, drag and drop the button and text boxes, list box, label, and hyperlink label previously shown in Figure 13.10 onto the page.
-
Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.
-
Within the Address field, type localhost/FileLibrary/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.
-
Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 13.11.
Listing 13.11 AccessProjectFiles.vb
Private Sub Page_Load(ByVal sender As System.Object, _ Ä ByVal e As System.EventArgs) Handles MyBase.Load Try Dim WS As New localhost.Service1() Dim Filelist As String() Filelist = WS.AvailableFiles() Dim Str As String If (ListBox1.Items.Count = 0) Then For Each Str In Filelist ListBox1.Items.Add(Str) Next End If Catch Ex As Exception TextBox2.Text = Ex.Message End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e _ Ä As System.EventArgs) Handles Button1.Click Dim InputFile As Byte() Dim WS As New localhost.Service1() Dim Filename As String = ListBox1.SelectedItem.ToString() If (Filename <> "") Then Try InputFile = WS.CheckoutFile(Filename) Dim OutputFile As FileStream Try Filename = "C:\TEMP\" + Path.GetFileName(Filename) OutputFile = File.Open(Filename, FileMode.Create, _ Ä FileAccess.Write) OutputFile.Write(InputFile, 0, InputFile.Length) OutputFile.Close() Catch Ex As Exception TextBox2.Text = "Error writing file " + Ex.Message End Try Dim Filelist As String() Filelist = WS.AvailableFiles() Dim Str As String ListBox1.Items.Clear() If (ListBox1.Items.Count = 0) Then For Each Str In Filelist ListBox1.Items.Add(Str) Next End If Form.ActiveForm.Refresh() Catch Ex As Exception TextBox2.Text = ex.Message End Try End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As _ Ä System.EventArgs) Handles Button2.Click Dim InputFile As FileStream Dim FileLength As Long If TextBox1.Text.Length > 0 Then Try TextBox2.Text = "" InputFile = File.Open(TextBox1.Text, FileMode.Open, _ Ä FileAccess.Read) FileLength = InputFile.Length Dim DataBuffer(FileLength - 1) As Byte InputFile.Read(DataBuffer, 0, FileLength) InputFile.Close() Dim WS As New localhost.Service1() If WS.FileUpload(DataBuffer, Path.GetFileName(TextBox1.Text)) _ Ä = 1 Then TextBox2.Text = "File upload successful" Else TextBox2.Text = "Error uploading file" End If Dim Filelist As String() Filelist = WS.AvailableFiles() Dim Str As String ListBox1.Items.Clear() If (ListBox1.Items.Count = 0) Then For Each Str In Filelist ListBox1.Items.Add(Str) Next End If Form.ActiveForm.Refresh() Catch Ex As Exception Throw New Exception("Unable to open specified file") End Try End If End Sub
When the program runs, the Page_Load method will use the FileLibrary web service to retrieve a list of the files that reside in the server’s ProjectFiles folder. The program assigns the filenames to a list box. If the user “checks out” a file from the ProjectFiles folder, the application will store the file in the C:\Temp folder on the user’s system. The program will then update the files that appear in the list box. In a similar way, if the user uploads a file to the server, the program will again update the file list.
|
|