5.3 Writing Your Web Services Contract First with WSCF
Visual Studio makes it very simple to create and expose web services, as long as you follow its rules. Simply create a method, just like any other method in your application, and then decorate it with the [WebMethod]
attribute. That’s all that’s required; you have written a web service. The only issue is that you don’t really know what is being exposed to your clients, because Visual Studio automatically generates the web service contract for you. The contract contains the service names, the domain model, and the formats of the messages that the services expect as input and return as responses.
Because Visual Studio generates your contracts, you can’t easily control what is exposed to the consumers of your web services. You may end up with tightly coupled RPC-style services that unintentionally expose the inner workings of your application, thus defeating the purpose of your service layer. This technique is often called code-first, since you are writing the code before creating the contract, and it’s helpful to know that there are other options.
Developing interfaces before components has long been a best practice. Before writing your service, you should spend some time thinking about the interface and contract that your web service will expose and how it can be made as loosely coupled as possible.
Most web service contracts are defined using the Web Service Description Language (WSDL), an XML format used to define a service’s ...
Get Windows Developer Power Tools now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.