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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.