This is the second article in the WSDAPI 101 series. You'll learn the most by starting at the beginning, although it's not required to understand the content in this article.
The first article in the WSDAPI 101 series presented an overview of the actors involved in Web Services (contracts, messages, clients, and services) and described how they interact to make network communication straightfoward. However, this high-level view doesn't describe the nuts and bolts, and that's what we'll get into in this article.
An example scenario: networked webcamI promised a concrete example, so here it is: an IP-connected webcam. This webcam talks Web Services (specifically, the Devices Profile for Web Services, or DPWS) and is a service that deals with clients, messages, and contracts in ways outlined in the previous article. So in specific, the webcam service exposes a webcam contract, which describes a handful of messages that can be passed between a client and the webcam service.
I'll skip over a lot of the detail (wait for a later article on WSDL) but the webcam service looks approximately like a C++ class or C# interface, which exposes methods that allow you to invoke webcam functionality. The most important operation would be GetWebcamImage(image *imageOut) which grabs the most recent frame, but you can imagine there are other minor operations like SetWebcamResolution(int xResolution, int yResolution) and so forth. Clients send messages to the service that clearly indicate that they're issuing GetWebcamImage or SetWebcamResolution requests.
In this example, we'll be writing a client application that exercises the functionality on this camera. The fundamentals are identical if you're writing a webcam service--although you'd receive calls into your service object instead of issuing calls into the Web Services stack.
The non-WS way: two layersBefore we talk about the Web Services way of solving this problem, let's take a step back and imagine that you have to implement a webcam client using only OS components, like sockets. Let's say that your IP-connected webcam talks over raw sockets, and you have to write your own network protocol for moving data back and forth.
If you were to write an application that exercised this webcam's functionality, you'd have essentially two layers inside your application:
Many of you would probably abstract out some of the webcam calls inside your application layer so that your high-level UI code could simply call into GetWebcamImage(image *) and SetWebcamResolution(int, int). But, you'd have to write both sides of this abstraction on your own.
The Web Services way: three layersModern WS applications, however, typically have three layers: there's your application code (which contains only the application-specific logic), there's a generated code layer, and then there's all of the OS and framework components. We'll go through these piece by piece.
Do bear in mind that when talking generically about "Web Services implementations," these are all generalizations. There are a million different ways to build Web Services stacks, and not all of them look exactly like this. However, WSDAPI follows this pattern, and Windows Communication Foundation in .NET is very similar.
Enough overview. What do these interfaces look like?If your app is built on WSDAPI, here's how the interfaces would lie out.
class webcam : public IWebcam { public: HRESULT GetWebcamImage( /* [out] */ image *imageOut ); HRESULT SetWebcamResolution( /* [in] */ int xResolution, /* [in] */ int yResolution ); // Code follows to manage IUnknown ... }
And that's it! As you can see, the bulk of the complicated messaging work is handled by the Web Services stack, and the generated code layer lets you use that stack without having to deal with building generic parameters that the stack can understand. It's very powerful, and lets you build networked applications very quickly.
The next article will dive deeper into the generated code layer to explain what's involved in turning these simple method calls into generic messaging calls that WSDAPI (or other WS stacks) can understand. See you next time!