This scenario simulates using Microsoft.NET by building the Warehouse as a Web service and the Retailer as a Web application that consumes this service. In the full SCM example, the Retailer is also a Web service, but as it isn't being consumed in this simulation, it is just built as a Web application that exposes a Graphical User Interface (GUI) to make testing easier. WSDL for the Warehouse ServiceThe WSI published a WSDL document [WSI2] that outlines the interface that they would expect to see for the Warehouse service. A snippet of this document can be seen in Listing 16-1. Listing 16-1. Warehouse Service WSDL
This snippet defines the contract for submitting an order asynchronously and how the ShipGoods request and response should be handled. SOAP for the Warehouse ServiceA snippet of an example SOAP document, also published by the WSI [WSI2] that conforms to this contract for a request, is shown in Listing 16-2: Listing 16-2. Warehouse Service SOAP
The important thing to note in building a service that implements this is the data types that are being passed to and from the service. The request message contains a number of items of type Item that is a structure containing a product number and a quantity, and the response message contains a number of items of type ItemStatus, which contain a product number and a status. The Request message is passed to the service containing the list of requested items based on their product number and the desired quantity. Taking a look at the preceding snippets, the request is for 23 items of type 605006, and the response to that is false, as sufficient quantity isn't available. Defining the Data Classes in C#The first step in implementing this service is to define classes in C# to handle these data structures. Listing 16-3 presents the class representing the Item used for the request: Listing 16-3. C# Item Class
The ItemStatus class is very similar to this, except that it has a property for Status instead of quantity. The Web service sets this property to true if there is sufficient quantity of the desired product available. The full source code is available in the download. The C# Web ServiceThe Warehouse Web service has to expose a method called ShipGoods, which, upon receiving a request containing an array of type Item, checks through the list to see if there is sufficient quantity on hand of the desired item. It creates an array of ItemStatus objects that it uses to return the status of the ordered items. Should there be sufficient quantity of the desired item, it flags the shipping status for that product as TRue; otherwise, it flags it as false. The current quantities are stored in an SQL Server database. Listing 16-4 shows this WebMethod: Listing 16-4. C# Web Service
Consuming the Web Service Using C#Finally, following is some example C# code that consumes this Web service. This code requires a Web reference, called Warehouse, to be added to the project in Visual Studio.NET. To call this service, asynchronously, using callbacks, the AsyncCallback class is used like this AsyncCallback cb = new AsyncCallback(ServiceCallback); theService.BeginShipGoods(itemsList, cb, theService); This creates a callback, cb, which maps to a function called ServiceCallback. This should be implemented as a public void that takes an IAsyncResult as a parameter as shown below in Listing 16-5. Listing 16-5. C# Web Service Callback
When the Web service completes processing, it calls back to this function, where the EndShipGoods function is called to complete the transaction. |