» Web Services in .NET
These tutorials are no longer maintained. Some of these tutorials continue to be maintained for the lecture Advanced Concepts in Software Engineering.

This tutorial describes how to create and access Web services from .NET 4.0. Visual Studio 2010 is required.

  1. Creating a Web Service.
  2. Accessing a Web Service.

A. Creating a WCF Web Service Application

  1. Create a new "WCF Service Application" project ("Visual C# > WCF").
    • With this project type, the WCF service will be deployed on a web server as a web service. The wizard creates some sample files demonstrating the general structure of a WCF service.
  2. In order to start from scratch, delete the following generated files from the project: IService1.cs and Service1.svc. You can also delete the special Debug and Release configuration files which can be found under the Web.config node (do not delete Web.config itself).
  3. Adding a service to the application requires the following artifacts:
    1. A service contract in the form of an interface annotated with [ServiceContract] whose methods are annotated with [OperationContract].
    2. A class implementing this interface.
    3. Open the file Web.config and locate the XML node serviceHostingEnvironment (child of system.serviceModel). Add a child node serviceActivations under this node. For each service, a child node under serviceActivations like the following is required: <add service="#1" relativeAddress="#2"/>
      Replace #1 with the namespace qualified name of the implementation class. Replace #2 with a name ending with ".svc". The service will be accessible under the address http://localhost:XXXX/#2.svc (append ?wsdl to retrieve the WSDL).
      Instead of step 3, you can also use a .svc-file (see the output of the wizard from step 1).

B. Accessing a Web Service from .NET

The following applies regardless of the language in which the Web service is implemented.

  1. To access a Web service from any project type, right-click on the project and select "Add Service Reference...".
  2. Enter the URL of the WSDL under "Address" and click "Go".
  3. Define a "Namespace" and click "OK".
  4. The tool will generate several classes. Use the client class to access the Web service.