XML Web Services

Creating ASP.NET Web Service

Having to program directly against HTTP, XML and SOAP is challenging.

ASP.NET provides model for building and consuming XML web services.

Define web service as .asmx file and related code behind file which ASP.NET will wrap as web service object, exposing service endpoint at URL of .asmx file.

Web Service Templates

XML web service defined as .asmx file, which can be added to existing websites.

Web service website looks like any other. Includes standard folders, config files, etc.

Web services exposed though URL.

Add .asmx file to site for each web service to be exposed.

Add .asmx files via Add New Item dialogue.

ASP.NET will create related code behind file in App_Code folder.

Top of .asmx file has WebService directive:

<%@ WebService Language="C#" CodeBehind="~/App_Code/Auhors.cs" Class="Authors %>

No additional markup in .asmx file, service defined entirely in code.

Web.Services Namespace

A web service class usually inherits from System.Web.Services.WebServive which provides wrapper for your web service code, leaving you to write web methods in pretty much same way as normal methods.

Attribute classes in namespace allow parts of class to be marked as related to web service allowing ASP.NET to know how to serialize / de-serialize, call the service, etc.

WebService class

Provides access to ASP.NET objects such as Application and Session.

Class is optional, do not need to derive from it to create XML web services. Instead use this class when want to gain access to and use features of ASP.NET. To simply expose web service over HTTP do not need to inherit from any class.

WebServiceAttribute Class

Provides information about your web service.

Provides namespace and description for the webservice.

[WebService(Description="Authors service" Namespace="http://mydomain.com/")]

public class Authors : WebService

WebMethodAttribute Class

Used to expose web methods.

Apply to any public method to be exposed as part of service.

PropertyDescription
enableSessionState  False indicates method does not require support for session state
transactionOptionWeb service can only act as root of transaction, so only Required and RequiresNew indicate transactions are supported. Other values (Disabled, NotSupported, Supported) indicate no transaction support for service.
cacheDurationNumber of seconds response should be cached by server.
bufferResponseIndicates if response should be buffered back to client - used when large amounts of data are to be returned.

 

[WebMethod(CacheDuration=300)] 
public DataTable GetAuthorTitle(string authorId)  
{}

Web Services and Data Types

Data to be returned must be serializable into XML.

The class to be returned must have default constructor that takes no arguments and be tagged with the Serializable attribute.

Referencing Web Service

Proxy object generated when reference web service.

Use this proxy object as if calling in-process code.

Proxy handles serialisation, SOAP messaging, etc.

To add reference right click project and choose Add Web Reference.

In dialogue define URL of service to be accessed and a name for the reference.

Proxy generated from WSDL (Web Services Description Language) of the web service.

Calling Web Service

Call web service through generated proxy class, e.g.:

AuthorsSrv.Authors auSrv = new AuthorsSrv.Authors();

AuthorsSrv.Author au = auSrv.GetAuthor("AuthorId");

string f = au.FirstName;

Only call to GetAuthor invokes web service. Object creation and property access are calls to proxy, thus in-process calls.

Can also bind to web services.

Calling Web Service via AJAX

Can use AJAX functionality in ASP.NET to call web service from client-side JavaScript.

Client-side page and XML web service must be in same domain.

  • Web service class must be marked with ScriptServiceAttribute
  • Client page must contain ScriptManager control, within which must be set a ServiceReference to the .asmx file-system.

<asp:ScriptManager ID="ScriptManager1" runat="server">

<Services>

<asp:ServiceReference Path="Authors.asmx" />

</Services>

</asp:ScriptManager>

  • Add JavaScript method to call web service.

<script type="text/javascript">

function GetAuthor() {

Authors.GetAuthor(document.getElementById("MainContent_TextBoxAuthorId").value, FinishCallback);

}

</script>

  • Optionally add JavaScript method to act as call back handler.

function FinishCallback(result) {

...

}

Authentication and XML Web Services

Use one of standard ASP.NET security methods.

Similar to securing any other ASP.NET resources.

Alternatively use Web Services Enhancements (WSE) 3.0 to secure web services.

Passing Credentials

To pass basic authentication credentials first create instance of NetworkCredentials class to contain user name, password and domain information.

Create CredentialCache object to which the NetworkCredentials instance is added.

Set web service proxy Credentials property to the CredentialCache object just created.

If using Windows Integrated Security set Credentials property to System.Net.CredentialCache.DefaultCredentials.

Download