Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 69 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
69
Dung lượng
1 MB
Nội dung
Chapter 13 42 The response entry clearly shows us what was returned to the client: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <GetShippingDetailsResponse xmlns="http://wrox.com/1861005555"> <GetShippingDetailsResult> <RequestOk>true</RequestOk> <RequestProblem xsi:nil="true" /> <HasBeenShipped>true</HasBeenShipped> <ShippedDate>1996-07-16T00:00:00.0000000+01:00</ShippedDate> <ShippingMethod xsi:nil="true" /> <ShippedToName>Vins et alcools Chevalier</ShippedToName> <ShippedToAddress>59 rue de l'Abbaye</ShippedToAddress> <ShippedToCity>Reims</ShippedToCity> <ShippedToRegion xsi:nil="true" /> <ShippedToPostalCode>51100</ShippedToPostalCode> <ShippedToCountry>France</ShippedToCountry> </GetShippingDetailsResult> </GetShippingDetailsResponse> </soap:Body> </soap:Envelope> Again, I've highlighted the important parts in gray. This XML will be used to populate our ShippingDetailsResult object on the client. One thing to watch when using proxyTrace is that, if you send another request, another entry might not appear in the list on the left. Whenever a request is made, the connection remains open for a short time. (This makes communication more efficient.) If you don't see the new request, but connected is shown in the Status column, you'll have to select another request in the list and then reselect the original one. Unfortunately, you can't just click on the blank area of the list to select nothing and then flip back again so, if you only have one request, close and restart proxyTrace. How It Works proxyTrace acts as a proxy server, intercepting and examining requests for web resources before forwarding them on to the server. Most proxy servers let you examine the data that they handle and that, of course, is the sole purpose of proxyTrace. It is useful for situations when you're getting errors from a Web Service as you can determine whether or not the service is returning the expected response. If the Web Service does appear to be working properly, you know that the problem must lie in the client-side code. In my experience, I've found the tool extremely useful for capturing errors returned from the Web Service. .NET doesn't properly trap SOAP Fault messages from some Web Service implementations, and comes up with some fairly cryptic messages, like this one: Web Services 43 If you get an error similar to this when calling a web method, crack out proxyTrace and look at the response packet. You might find something like this: <SOAP-ENV:Fault> <faultcode>SOAP-ENV:Client</faultcode> <faultstring>Client Error</faultstring> <faultactor>lcTk##SBA-CSOAPBusinessArea-SOAP</faultactor> <detail> <e:details xmlns:e="http://tempuri.org/"> <message>ERR: Schema for that business area and process are missing</message> <errorcode>57126</errorcode> </e:details> </detail> </SOAP-ENV:Fault> If you get an error like this and can't figure out how to fix it, try contacting the Web Service owner for advice. Directory Services Although it's likely that, in time, you'll want to build your own Web Services, there are a growing selection of Web Services supplied by third-party sources for us to use. The question remains, however, of how to find these Web Services once companies have made them available. In the next two sections, we'll take a look at how to use two kinds of directory services to find Web Services, namely UDDI and brokerages. UDDI UDDI, for Universal Description, Discovery, and Integration, is a type of directory geared towards business process integration. They are mainly used if you are looking for a business partner that provides some specific task, and who publishes a Web Service for that task. Chapter 13 44 The UDDI initiative was jointly launched by Microsoft, IBM, and Ariba in May 2001. Although all three organizations were to maintain sites that would allow searching and administration of a single directory, after a little over a month Ariba announced that Microsoft and IBM would be responsible for managing the directory. It's also expected that by the time this book is published, Hewlett-Packard will have another site. All of these sites synchronize their data so that it won't matter which of the two or more sites are used to query UDDI. The ultimate goal of UDDI is to bring business partners together. Once they've done this, the companies can either interact in the usual way, that is through e-mails and phone calls, or they can use the directory to obtain the WSDL documents that describe the Web Service that each offers. Let's take a look now at how a book distributor looking for potential new publishers might go about it. We'll also see how that publisher could find the Web Service that will allows orders to be placed automatically. Although we're going to look at the case of a Web Service for distributors to place orders, the service doesn't really exist. This is a hypothetical scenario for demonstration purposes. Remember, the WSDL document is all you need to consume a Web Service, by following the same steps we took for the NorthwindWebService service. Try It Out – Finding a Business Partner with UDDI 1. Microsoft and IBM each manage two directories. One is a live site that provides working business information and the other is a test site for testing how UDDI actually works. I've registered a sample set of services on the Microsoft UDDI test site. Open a web browser and go to http://test.uddi.microsoft.com/. 2. To find a business partner, you must either know their name, or know something about their business. In this hypothetical case, we're looking for book publishers. Standard Industry Classifications (SIC) codes can do this and, as long as you know the SIC code, tools on the site can find everyone in the specified category. Luckily, I happen to know that book publishing comes under Manufacturing | Printing and Publishing | Books and that the code we want is 2730. Click the Advanced Search link on the UDDI page, and enter the following: 3. Click the arrow button next to the drop down to bring up a list of businesses: Web Services 45 4. Click on Wrox Press. This will bring up the company listing. Half way down the page you'll find an entry marked Services. This is a list of the services that the company offers. These aren't limited to Web Services, and can include traditional services offered by the company: 5. Click BookBuyer. This will bring up a list of bindings, which are particular to Web Services, and you'll find a single binding on this page that points to a WSDL file: 6. Right-click on the URL of the WSDL file to bring up the context menu, and select Copy Shortcut. 7. Open Visual Studio .NET and select New Visual Basic project | Windows Application. Call it BookBuyer. The name doesn't matter too much because we'll throw it away after having used it to demonstrate the principles here. 8. Right-click on the BookBuyer project in Solution Explorer and select Add Web Reference. 9. Right-click the URL box at the top and select Paste. Click the green arrow. This will download the WSDL file from the Wrox site ready for use: Chapter 13 46 As the Web Service described by the WSDL file doesn't in reality exist, we'll stop our discussion here. Hopefully, though, you now understand how UDDI works. We use the tools supplied to find a business partner in the directory and, ultimately, a URL (a "binding") for their Web Service. Had this been a real, existing Web Service, we'd just need to click the Add Reference button to get Visual Studio .NET to create the classes that consume the service. With those new classes in place, we could then start using the service straight away. Web Service Brokerages With UDDI, we saw an example of a Web Service directory that can help us find commercial business partners that expose Web Services as part of their line of business. This is just half the market. Over the coming months, we can expect to see companies deploy Web Services that add useful functionality to our applications. This is, after all, the central premise of Web Services – "software as services". Microsoft's push into this area was initially dubbed Hailstorm, but is now known as ".NET My Services". This describes a set of common, fundamental services that web sites and desktop applications are likely to want to use. My Services will include the "Passport" concept and other central Internet- based services such as a diary, a file storage facility, and so on. At the time of writing, however, My Services is still very much hype, so we're not going to dwell on it here. Another way to sift through the hundreds of Web Services coming on to the market is through a Web Service brokerage, such as Salcentral (http://www.salcentral.com/) or Grand Central (http://www.grandcentral.com/). Chapter 13 48 ❑ The TextBox controls (from top to bottom) need to have their Name property set thus: ❑ txtPhoneNumber ❑ txtMessage – this control also needs its Multiline property set to True ❑ txtSenderId ❑ txtSendPasskey The Label control marked (chars) needs to have its Name property set to lblChars. Call the CheckBox chkUseProxy and set its Checked property to True. Also the button needs to be called btnSendMessage. 3. There is a limit to the number of characters that can be sent through to the service, so we want to keep the user informed of how much space is left for the message. The maximum length of the message is 120 characters and this includes the length of the sender ID, the word "from", and two spaces. Double-click on the txtMessage control and add the method call highlighted below to the event handler, followed by the UpdateCharacterCount method itself: Private Sub txtMessage_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles txtMessage.TextChanged UpdateCharacterCount() End Sub Private Sub UpdateCharacterCount() ' add the number of chars Dim numChars As Integer = " from ".Length numChars += txtMessage.Text.Length numChars += txtSenderId.Text.Length ' report the length lblChars.Text = numChars & " characters" If numChars > 120 Then Web Services 49 lblChars.ForeColor = Color.Red Else lblChars.ForeColor = SystemColors.ControlText End If End Sub 4. Flip back to the Form Designer and double-click on the txtSenderId box. Add this code to the new event handler: Private Sub txtSenderId_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles txtSenderId.TextChanged UpdateCharacterCount() End Sub Referencing the Web Service Now that we've built the basic form, we are ready to add a reference to the SMS service on Salcentral's site. Try It Out – Adding a Web Reference 1. Open your browser and go back to the http://www.salcentral.com/wrox/smsreg.asp page. On this page you'll find a link to the WSDL file describing the Web Service. It will look something like this: http://sal006.salnetwork.com:83/lucin/SMSMessaging/Process.xml 2. Select the entire URL with your mouse, and choose Edit | Copy from the menu. 3. Go back to Visual Studio . NET, right-click on the SMS project in Solution Explorer, and select Add Web Reference. 4. In the Address bar at the top, paste in the URL copied from Salcentral. Click the green arrow button. The WSDL file will be loaded and displayed in the left pane: Chapter 13 50 5. Click the Add Reference button to add a reference to the service to our project. 6. The new reference will appear as com.salnetwork.sal006 or something similar. Right-click on this and select Rename. Change the name to SMSService and press Return: Sending Messages With the reference added, Visual Studio .NET has automatically created a class to access the service, and all we have to do is create an instance of the class and call the SendMessage method. Web Services 51 Try It Out – Calling a Web Method 1. Open Form1 in Design view, and double-click on the Send Message button to create a new Click handler. Add this code: Private Sub btnSendMessage_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSendMessage.Click ' create a new message box Dim smsService As New SMSService.SMSMessagingprocessService() ' make sure the message goes through proxytrace If chkUseProxy.Checked = True Then smsService.Proxy = New System.Net.WebProxy("localhost", 8080) End If ' send the message Try ' did we do it? Dim result As Boolean = smsService.SendMessage( _ txtPhoneNumber.Text, txtMessage.Text, _ txtSenderId.Text, txtSendPasskey.Text) If result = True Then MsgBox("The message was sent to " & txtPhoneNumber.Text & ".") Else MsgBox("The message could not be sent.") End If Catch ex As Exception ' we got an exception MsgBox("An exception occured. " & ex.Message) End Try End Sub 2. Open proxyTrace and tell it to connect to port 8080. (We covered proxyTrace earlier, so if you need a refresher go back a few pages.) 3. Run the project. Phone numbers have to be entered in international format. If necessary, this means you must drop the first zero of the number, and add a plus sign followed by the international dialing code for that country, for instance: ❑ For the US, numbers are prefixed with 1, so 06025551234 is +16025551234. ❑ For the UK, the dialing code is 44, so 07790123456 becomes +447790123456. Enter any message you like, but remember to set the sender ID and passkey fields to whatever you were given at the end of the registration process: [...]... This provider will have the intelligence to know whether it should be drawing data directly from the database or through a Web Service We'll do this by inserting a layer between the application calls that require database access and the database itself This layer will either connect directly to the database (through the SqlClient objects like we have been doing), or indirectly through a Web Service... Web Service will then act as a proxy for the application's instructions, passing them on to the database in the usual way 2 Disconnected Data The database package provides the application with the data it needs in order to function properly Inside LAN Web Server Web Service Database Package Firewall Firewall Database Server The Internet If the same application is running remotely, the access layer connects... that accesses a database either locally or by using a Web Service Specifically, we are going to: ❑ Look at how and why we would want to use disconnected data ❑ Build a basic application to directly retrieve data ❑ Add functionality to our application to allow us to retrieve the data both directly and remotely ❑ Add the code to allow us to change any data and save the changes to the database Disconnected... the principle behind an application that can consume data from the provider that we'll build a little while later Try It Out – Building the Application 1 Open Visual Studio NET and select File | New | Project from the menu Create a new Visual Basic Windows Application project and call it Product Editor 3 Chapter 14 2 The Form designer for Form1 will automatically open Layout a DataGrid, Label, TextBox,... Cursors.WaitCursor End Sub Public Sub ResetProcessText() StatusText = "" Me.Cursor = Cursors.Default End Sub That will do for the basic form layout Let's look now at how we can retrieve information from the database 6 Disconnected Data Retrieving Products We're going to encapsulate all of the database functionality in a separate class library The first step in achieving this goal is to put together a stored procedure... it needs The application uses an “access layer” to connect to the database package when running on the LAN Remote User Access Layer Access Layer Application Application User Outside LAN What this means is that we can build one application that works both inside and outside of the LAN If the layer can make a direct connection to the database of choice, then it will work in "direct" mode If the layer... Application In this chapter, we'll build a single desktop application for editing product information on the NorthwindSQL database This application will use a data provider class to determine whether a direct or remote connection is required The first thing that we should do is to build the basic Product Editor application This is a simple application to demonstrate the principle behind an application that... creating one as an example that would allow customers to view their own orders placed with the Northwind system We devised and implemented a basic security system, and finally tested the service With our service created, we built a reference client implementation, using Visual Studio NET's tools to automatically generate classes to consume the Web Service After illustrating how simple this is, we looked... intranet Although Web browser technology has come along in leaps and bounds in recent years, the user interface that you can build with a Web browser is harder to develop and use than a traditional Visual Basic application Chapter 14 Without using an intranet, the only way to make your organization's applications available outside of the LAN is to physically install it wherever you're working This,... build as part of this exercise with the word "Provider" This will help us keep them separate from other stored procedures that may already be in the database 3 Using the Server Explorer, drill down until you find the Stored Procedures node of the NorthwindSQL database (In this screenshot, my server is called chimaera Your machine will have a different name.) 4 Right-click on the Stored Procedures node . the WSDL file to bring up the context menu, and select Copy Shortcut. 7. Open Visual Studio .NET and select New Visual Basic project | Windows Application. Call it BookBuyer. The name doesn't. application's instructions, passing them on to the database in the usual way. Disconnected Data 3 Database Server Database Package Inside LAN The database package provides the application with the. later. Try It Out – Building the Application 1. Open Visual Studio .NET and select File | New | Project from the menu. Create a new Visual Basic Windows Application project and call it Product