|
Chapter 5: Connecting with Customers
5 Connecting with CustomersThis chapter focuses on connecting with customers. You might not directly deal with your customers; other companies might do so for you. But whether the relationship is direct or not, it's important to support your customers and maintain that relationship.First we'll examine indirect customer relationships. Although another company deals with your customers for you, you still might want to provide these customers with support. Customer support can also be a great way to promote related products or services, and examples of this type of service can be found on the Internet. For example, delivery companies receive a significant amount of business by shipping products from a Web site where the customer bought a product. Indirectly, the customer has paid for delivery of the item. Often, customers can track the progress of their parcels using the delivery company's Web site. Sometimes, but not always, they can track the parcels from the site where they actually purchased the items. You might be thinking that having users track orders through the delivery company is not really a problem. After all, most users are happy to browse to the site of the delivery company to check the status of their parcels. However, not all indirectly linked companies are so well known. The customers have placed their orders with you, so they should be able to come back to you to check on the status of their orders, including any services provided by a third party. There are many different approaches to integrating third-party data into your own Web site, including:
This is not a complete list, but it's enough to highlight the different approaches that can be taken and some of their disadvantages. Hopefully, seeing the mention of HTTP as a transport made you think of XML-based Web services as a possible solution to this problem.
Using Web Services to Support CustomersWhat you really want is to have pages on your Web site that access and present the data the user needs, whether that data is from your own business objects or from the business objects of your partners. Using Web services to access this data allows you to integrate with functionality on remote servers no matter what that platform is.In the previous chapters, we have started each time by creating the Web service and then creating the client-side proxy based on the Web Services Description Language (WSDL) file from the Web service. Although this approach is the most common, clients of the Web service might need to define the interface they expect on the remote Web service. Let's look at how to define Web service interfaces.
Defining a Web Service Interface to Be Implemented by OthersThe sample application for this chapter contains a few pages of the Satellite Installation Web site. These pages allow customers of the Web site to enter their personal details, select a satellite system that they want installed, and then check for an available installation appointment. For now, assume that you're technically responsible for this Web site. Your company does not actually have installation engineers of its own; instead, you contract out this part of your service to many different installation companies throughout the country. You want the service your customers receive to be quick and efficient. You need to tightly integrate your Web site to the systems of all the different installation companies. You decide to use Web services because being platform-agnostic is definitely a necessity of this solution. But even though you're going to be a client of many remote Web services, you define the interfaces they should support, which makes integrating with each remote Web service very simple. In fact, all you need to do is keep a list of each Web service's URL.You inform each partner company (the installation providers) what interfaces they need to support by sending them a WSDL file. We've covered WSDL files previously, so they shouldn't be new to you. You could write the WSDL file manually, but in this case, it's easier to create the Web service interfaces in Microsoft Visual Studio .NET 2003 yourself and then query the service for the WSDL file. You do not need to implement each method; just define what the method should look like. For example, for the Satellite Installation company, you want the remote Web services to support three interfaces: GetAppointment, ConfirmAppointment, and CancelAppointment, as shown in the following code:
namespace InstallCompany2 {As you can see in the preceding code, the three methods have no implementation, apart from returning the necessary data types. Now you can compile the code and then retrieve the WSDL file in a browser by entering the URL to the Web service with ?WSDL appended to the end. The WSDL file for the Satellite Installation Web site looks like this:
<?xml version="1.0" encoding="utf-8"?> You could describe the methods you need the remote Web services to support in other ways, such as using a simple Word document, but giving the installation companies a WSDL file has some benefits:
Each remote installation company should use this WSDL file to implement the Web service that you'll use from within your Web site. If you have downloaded this book's sample files, you can look at the code within the Web service of InstallCompany1 and InstallCompany2. Both solutions are located under Microsoft Press\XMLWSOrg\Chapter5. Each one implements the same WSDL file, but the actual code implemented in the methods is different. Also, notice how the names of the services differ between the two solutions. For InstallCompany1, the service is named Install.asmx, and for InstallCompany2, the service is named Service1.asmx. Although you'll need to know this URL, changing the name of the service changes only the service port section of the WSDL file, which does not cause you any problems. The code contained in Install.asmx.cs for InstallCompany1 looks like this:
namespace InstallCompany1 {If you compare the specification of the earlier WSDL file to the methods defined in the preceding code, you'll see that they match. The preceding code does not make a complete solution, but it's enough for our purposes. This code will always return the same appointment data. The code within the Service1.asmx.cs file for the InstallCompany2 Web service looks like this:
namespace InstallCompany2 {This code, from the Service1.asmx.cs file, looks similar to the code from the Install.asmx.cs file. The defined methods in these two files must be the same, and although a lot of the code within the methods is the same, there are differences. After all, this installation company covers a different set of postal codes. As long as the methods defined match those defined in the WSDL file, the implementations can vary. In our sample applications, these Web services are .NET based. Actually, as long as the methods defined are correct, it doesn't matter which Web service toolkit you're using to create the Web services or which platform the Web services are based on.
Examining User Interaction with the Satellite SiteBefore we look at the code within the Satellite Site Web pages, we'll examine the user experience. When customers are ready to order a satellite system, they're directed to OrderPage.aspx. This page allows the user to enter personal information, such as his or her name and address, and to specify which satellite system to install. (See Figure 5-1.) This page should be secured using HTTPS because personal information is being passed over the Internet.Figure 5-1 An example of a user completing the OrderPage.aspx page Now the user clicks the Check Availability button. The Web site uses this information and queries the installation companies to see if they cover the area specified by the customer's address, which is done by matching the postal code. If an installation company doesn't cover the customer's address, the customer is presented with the RejectOrder.aspx page. If it covers the customer's address, the customer is presented with the ConfirmOrder.aspx page. (See Figure 5-2.) On this page, the customer can see the appointment date and time and the name of the company that will install the equipment. The customer can then confirm the order. If the order is confirmed, the customer is presented with the OrderComplete.aspx page. (See Figure 5-3.) Figure 5-2 The ConfirmOrder.aspx page Figure 5-3 The OrderComplete.aspx page With an understanding of the user experience, you can now look at what's happening between the Web site and the Web services of the installation companies. Figure 5-4 is a representation of the interactions between the Web site and two installation companies. There are seven steps spread over three Web pages, as the following figure shows. Figure 5-4 The Web site SOAP interactions
Setting a Web Service URL at Run TimeThe OrderPage.aspx page is coded to call the same Web method of the two different remote Web services. As mentioned, the Web services for Install Company 1 and Install Company 2 both support the same Web method interfaces. Armed with this fact, the code can easily call the Web method of either company and get an expected response. To call the Web method of different Web services, you need to change the URL of the Web services at run time. You can accomplish this task by changing the Url property of the proxy instance you create. Look at the code for the Click event of the Check Availability button (Button1_Click).
private void Button1_Click(object sender, System.EventArgs e) {The URL of each Web service is held in an array, making it easy to step through each URL. The line of code that sets the URL of the Web service is shown in boldface. Setting the URL in code is a very powerful and yet extremely simple capability. The preceding code can easily be changed to support many more remote Web services by increasing the size of the array that stores the URLs and changing the for loop.
Now that we've looked at indirect customer relationships, it's time to see how Web services can assist in direct customer relationships. You'll use the same basic set of Web service tools but with different clients.
Supporting Direct Business-to-Customer RelationshipsHow you connect directly to your customers depends on the nature of your product or service. The Microsoft software product Streets and Trips connects to a Web service to retrieve updated road construction information. A financial institution might want to allow its customers to check the value of their investments from their mobile phones. Your product might be a Global Positioning System (GPS) device that allows its users to periodically update the navigational data based on the data held on your servers.Providing data to your customers can be as simple as creating the right Web pages on your site. But more and more these days customers are equipping themselves with mobile devices such as Personal Digital Assistants (PDAs), mobile phones, Pocket PCs, or other purpose-specific devices. Many of these devices can display Web pages, but for your customers to use your data, the device needs a connection to the Internet or to cache the page offline. You can provide your customer with a dedicated piece of software that caches your data on the local device, refreshing the data when a connection is available. This feature allows the user to benefit from your functionality even when a connection is not available. Web services are an ideal way to link from the remote device to your servers. The Microsoft Windows .NET Compact Framework allows you to use the power of .NET from either the Windows CE or the Pocket PC platform. Therefore, the great Web service functionality built into the .NET platform is available on these platforms. You might remember that allowing people to use their data anytime, anywhere, on any device is one of the original goals of .NET. When developing a client application to be used by your customers, carefully consider which platform is most suitable. For example, an application that a user could use while shopping to track the cholesterol of items being purchased would not be helpful if it ran only on a laptop computer. It isn't easy to carry the laptop and groceries at the same time! A smart device application would be far more suitable. Also, if your application provides data that the user will want to carry out complex calculations on, a smart device would not be suitable because it is resource-limited.
Using the .NET Compact FrameworkThe .NET Compact Framework is a subset of the .NET Framework written to provide developers with a hardware-independent environment targeting resource-constrained devices. Visual Studio .NET 2003 integrates the .NET Compact Framework. Within Visual Studio .NET 2003, you can write applications in any installed language that target either the Pocket PC platform or Windows CE.You can find considerable documentation on the similarities and differences between the .NET Framework and the .NET Compact Framework in Visual Studio .NET 2003. One thing to remember is that the .NET Compact Framework is targeted toward providing a rich client experience. Technologies such as ASP.NET are not part of the .NET Compact Framework. As you'll see in a moment, developing a .NET Compact Framework application in Visual Studio .NET 2003 is very similar to developing a Windows application. As you work with the different classes and controls, you might notice that not all support as much functionality as their full .NET counterparts. To make development easier, classes within the .NET Compact Framework use the same names as the equivalent classes in the .NET Framework. For example, the TreeView control exists in both the .NET Framework and the .NET Compact Framework. Visual Studio .NET 2003 includes device emulator support. The emulator allows you to develop and test your smart device applications even if you do not have access to the actual hardware. Follow these instructions to build an application targeted for the Pocket PC platform:
The Smart Device Application Wizard appears. This wizard allows you to specify the target platform and the type of application you want to create.
Now you're ready to design the user interface, add any necessary code, and then test the application. Notice the list of controls in the Toolbox is slightly less than what's available when developing Windows applications. Add a button to the form, and double-click the button to add some code to the Click event, perhaps to display a message using
MessageBox.Show("A simple message")To test the application, follow these steps:
The emulator window will appear. The device will launch, and the .NET Compact Framework will be installed on the device. Be patient; unless the device needs to be restarted, subsequent tests take less time because the device is already loaded with the .NET Compact Framework. When the button appears on the device screen, click it. You should end up seeing something like the application shown in Figure 5-5. Figure 5-5 A simple Pocket PC application developed with Visual Studio .NET 2003 and the .NET Compact Framework In the previous chapters, we've seen a number of uses for Web services in either Windows applications or ASP.NET applications. For the remainder of this chapter, we'll see how easy it is to integrate Web services into a smart device application.
Implementing the Wine Guide ClientThe Wine Guide application is a Pocket PC application that allows the user to view a rating of a particular wine based on votes entered by users of the system. The user is presented with a tree view where the first level is the French wine-growing regions. Within each region, the major wines are listed, and then within each wine, a number of specific vintages are listed. Figure 5-6 shows how the user interface is represented.Figure 5-6 The user interface of the Wine Guide application Open the Visual Studio solution for this application at Microsoft Press\XMLWSOrg\Chapter5\WineGuideClientPPC.sln. With the solution open in Visual Studio .NET 2003, you can follow these steps to demonstrate the Wine Guide functionality:
This application references a Web service named WineWebService. This reference was added in the same way references to other Web services have been made in previous applications: using the Add Web Reference tool. You can examine the Web service by opening the solution at Microsoft Press\XMLWSOrg\Chapter5\WineWebService\WineWebService.sln. The WineWebService supports the following five methods:
In the client applications covered so far in this book, each call to a Web service has been synchronous; that is, the code makes the call to the Web service and waits for the response before continuing. In the following section, we introduce calling Web services asynchronously.
Calling Web Services AsynchronouslyThere are a number of reasons to call a Web service asynchronously. In the Wine Guide smart device application, the tree view is built using the results of multiple calls to a Web service. First the regions are loaded into the tree view. Then, as the user clicks on a region, the wines for that region are requested and added into the tree. If the user clicks on a wine, the vintages are then requested and added into the tree. By only requesting a part of the tree at a time, the size of the data being requested at any one time is reduced. However, more calls are made overall. When you consider that the bandwidth from the device to the Web service might be limited, calling a Web service synchronously could suspend the user interface and give the appearance of a hung application. By calling the Web service asynchronously, you can allow the application to continue responding. In this application, an activity bar is displayed until the response is received, informing the user that the application is waiting for the response. If you play with the application, you'll see that very few vintages are included in the data source (an XML file held on the server). Because the data in the file is minimal, downloading all of it at once would not be a problem. This application has been implemented this way deliberately to highlight the use of asynchronous Web service calls.The first call made asynchronously in this client is contained in the Form1_Load procedure, as shown here:
This code is just the part of the procedure that deals with initiating the call to the Web service method, GetRegions. The first statement creates an instance of the Web service class. The second statement sets the URL of the Web service. Normally, for examples in this book, this statement would not be necessary, but because this Web service client is running on a smart device (albeit a device emulator), the Web service needs to be referenced by the name of the machine that hosts it. The third statement creates an instance of the AsyncCallback delegate using a reference to the procedure that should be called when the Web service response is received, which is the UpdateRegions procedure in this case. The fourth statement actually initiates the call to the Web service. When the Add Web Reference tool creates the proxy class for the Web service client, it creates three methods for each Web service method. Within this proxy class, the synchronous method is named GetRegions. The other two methods support the asynchronous use of the Web service method. The method name to start an asynchronous call is the name of the method prefixed with Begin. The method that actually gets the result of the call, used in the UpdateRegions method, is the name of the method prefixed with End. Therefore, in this case, the name of the method to start the call is BeginGetRegions. This method requires two parameters, the AsyncCallback instance and an object referred to as the asyncState, which we'll use to hold the instance of the Web service. Now the call has been made to the Web service, and execution continues. When the response is received from the Web service, the UpdateRegions procedure will be called. This client application uses a Timer control to animate the activity bar until the response is received. The code within the UpdateRegions method looks like this:
private void UpdateRegions(IAsyncResult ar){The method accepts a single parameter of type IAsyncResult. The first line of code within the procedure sets up an instance of the Web service proxy class by casting the content of the ar.AsyncState property back to the proxy class. The second line of code uses the EndGetRegions method of the Web service proxy instance to get the result of the Web service call. The rest of the method adds the results into the tree view control. The client application asynchronously calls the GetWines and GetVintages methods. Calling a Web service synchronously or asynchronously is purely a client issue; no changes are necessary to the Web service itself.
SummaryAll the examples covered so far in this book support the common Web service design where many clients use a single Web service. The Web site example used earlier in this chapter is different in that it demonstrates the use of Web services with a single client and multiple Web services. Although this use is perhaps less common, it's still a valid design.The early part of this chapter discussed using a WSDL file to help your trading partners correctly implement a Web service that you specified. Once these remote Web services are implemented, you can use the Url property of the proxy class to dynamically reference a particular remote Web service. Using the .NET Compact Framework, you can use your knowledge of .NET to write compelling smart device applications as easily as you develop Windows or ASP.NET applications. Whether you're programming for Windows or a smart device, you can use Web services to bridge the gap between the client and remote functionality. Within the Wine Guide application, you saw how to call a Web service method asynchronously, which is not only powerful, but also fairly simple to implement. With asynchronous calls, you can continue to have a responsive interface while possibly time-consuming tasks are accomplished in the background. You're now prepared enough to turn to the next chapter and dig into securing your Web services.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||