Getting Started
  Introduction
  What is ASP.NET?
  Language Support

ASP.NET Web Forms
  Introducing Web Forms
  Working with Server Controls
  Applying Styles to Controls
  Server Control Form Validation
  Web Forms User Controls
  Data Binding Server Controls
  Server-Side Data Access
  Data Access and Customization
  Working with Business Objects
  Authoring Custom Controls
  Web Forms Controls Reference
  Web Forms Syntax Reference

ASP.NET Web Services
  Introducing Web Services
  Writing a Simple Web Service
  Web Service Type Marshalling
  Using Data in Web Services
  Using Objects and Intrinsics
  The WebService Behavior
  HTML Pattern Matching

ASP.NET Web Applications
  Application Overview
  Using the Global.asax File
  Managing Application State
  HttpHandlers and Factories

Cache Services
  Caching Overview
  Page Output Caching
  Page Fragment Caching
  Page Data Caching

Configuration
  Configuration Overview
  Configuration File Format
  Retrieving Configuration

Deployment
  Deploying Applications
  Using the Process Model
  Handling Errors

Security
  Security Overview
  Authentication & Authorization
  Windows-based Authentication
  Forms-based Authentication
  Authorizing Users and Roles
  User Account Impersonation
  Security and WebServices

Localization
  Internationalization Overview
  Setting Culture and Encoding
  Localizing ASP.NET Applications
  Working with Resource Files

Tracing
  Tracing Overview
  Trace Logging to Page Output
  Application-level Trace Logging

Debugging
  The SDK Debugger

Performance
  Performance Overview
  Performance Tuning Tips
  Measuring Performance

ASP to ASP.NET Migration
  Migration Overview
  Syntax and Semantics
  Language Compatibility
  COM Interoperability
  Transactions

Sample Applications
  A Personalized Portal
  An E-Commerce Storefront
  A Class Browser Application
  IBuySpy.com

  Get URL for this page

Write a Simple Web Service

You can write a simple XML Web service in a few minutes using any text editor. The service you will create in this section, MathService, exposes methods for adding, subtracting, dividing, and multiplying two numbers. At the top of the page, the following directive identifies the file as a XML Web service in addition to specifying the language for the service (C#, in this case).
<%@ WebService Language="C#" Class="MathService" %>

In this same file, you define a class that encapsulates the functionality of your service. This class should be public, and can optionally inherit from the WebService base class. Each method that will be exposed from the service is flagged with a [WebMethod] attribute in front of it. Without this attribute, the method will not be exposed from the service. This is sometimes useful for hiding implementation details called by public Web Service methods, or in the case where the WebService class is also used in local applications (a local application can use any public class, but only WebMethod classes are remotely accessible as XML Web services).


Imports System
Imports System.Web.Services

Public Class MathService : Inherits WebService

   <WebMethod()> Public Function Add(a As Integer, b As Integer) As Integer
       Return(a + b)
   End Function

End Class
VB

XML Web service files are saved under the .asmx file extension. Like .aspx files, these are automatically compiled by the ASP.NET runtime when a request to the service is made (subsequent requests are serviced by a cached precompiled type object). In the case of MathService, you have defined the WebService class in the .asmx file itself. Note that if an .asmx file is requested by a browser, the ASP.NET runtime returns a XML Web service Help page that describes the Web Service.

 
VB MathService.asmx

[Run Sample] | [View Source]
 
VB MathService.asmx?wsdl

[View Sample]

Precompiled XML Web services
If you have a precompiled class that you want to expose as a XML Web service (and this class exposes methods marked with the [WebMethod] attribute), you can create an .asmx file with only the following line.
<%@ WebService Class="MyWebApplication.MyWebService" %>

MyWebApplication.MyWebService defines the WebService class, and is contained in the \bin subdirectory of the ASP.NET application.

Consuming a XML Web service from a Client Application
To consume this service, you need to use the Web Services Description Language command-line tool (WSDL.exe) included in the SDK to create a proxy class that is similar to the class defined in the .asmx file. (It will contain only the WebMethod methods.) Then, you compile your code with this proxy class included.

WSDL.exe accepts a variety of command-line options, however to create a proxy only one option is required: the URI to the WSDL. In this example, we are passing a few extra options that specify the preferred language, namespace, and output location for the proxy. We are also compiling against a previously saved WSDL file instead of the URI to the service itself:

wsdl.exe /l:CS /n:MathService /out:MathService.cs MathService.wsdl

Once the proxy class exists, you can create objects based on it. Each method call made with the object then goes out to the URI of the XML Web service (usually as a SOAP request).

 
VB MathServiceClient.aspx

[Run Sample] | [View Source]


Copyright 2001 Microsoft Corporation. All rights reserved.