The technique presented next requires both the web service provider and the client to write their code in a slightly different way, in order to make use of interface-based web services. To create an interface-based web service, first expose the web service interface definition. For simplicity’s sake, assume the service provider is responsible for both defining and implementing the interface. There is an easy way for the client or any other third party to expose the web service interface definition and have anybody implement it, but that requires one or two additional steps (described later).

Service-Side Steps

In Visual Studio 2005, select File New Web Site... to bring up the New Web Site dialog box. Select ASP.NET Web Service and name it CalculationServices. Visual Studio 2005 will create a skeletal web service called Service. You have no use for it, so remove the file Service.asmx from the root of the project and remove Service.cs from the App_Code folder. To add the web service interface, right-click on the project, then select Add New Item... from the context menu to bring up the Add New Item dialog. Select Web Service in the dialog, and name it ICalculator. Make sure that the “Place code in separate file” checkbox is checked, and click Add.

Open the ICalculator.cs file in the App_Code folder and change the ICalculator type definition from class to interface. Remove the constructor and the HelloWorld() method and the derivation from WebService. You can also remove ...

Get Programming .NET Components, 2nd Edition now with O’Reilly online learning.

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