Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Synchronize Your Databases with .NET Web Services (Part I) : Page 4

The ever-increasing use of XML is an exciting development in Web site design and construction, and provides new ways for site authors to expose information to visitors. In this two-part article, you'll see how to create a data-driven Web service and explore three different ways to consume it. Then, in part II, you'll see how to use such services to synchronize the content in distributed databases automatically.

Building a Web Service
In essence, all you need to do in .NET to create a Web service is to decorate the Class declaration with the WebService attribute, and then mark each method that you want to expose with the WebMethod attribute. To expose the method via your Web server, create an .asmx page containing a WebService page directive, supplying the language and class name of the Web service implementation as shown below.

<%@WebService Language="VB" Class="PublicIISLogs"%>

Alternatively, you can create a new Web service file using Visual Studio, which automatically creates an .asmx page containing the WebService page directive and a class file in the project language and associates the two via the Codebehind attribute in the page directive:

<%@ WebService Language="vb" Codebehind="Service1.asmx.vb" Class="WebServiceTestApp2.test" %>

Unlike ASP.NET Web forms (.aspx files), ASP.NET and Visual Studio do not import basic namespaces into Web service files by default, so at minimum, you'll need to import the System, System.Web, and System.Web.Services namespaces. In addition, import the namespaces for any data access classes you use (the sample code uses both System.Data and System.Data.SqlClient). Don't forget the System.Configuration namespace, which lets you access settings in the web.config file to extract a connection string or other configuration information. You may need other namespaces as well, such as System.Collections (if your code uses a collection class such as an ArrayList), or System.Xml to access XML documents:

Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Web Imports System.Web.Services Imports System.Configuration

You need to decorate the Public Class statement with the WebService attribute and set its properties, using the rather odd syntax required for VB.NET. The Description property value should contain a brief description of the service, and set the Namespace value to something unique to your site and service that differentiates it from all other Web services on the Internet:

<WebService(Description:= _ "DaveAndAl.com Public Traffic Data Service", _ Namespace:= _ "http://www.stonebroom.com/webservices/publiciislogs" _ )> _ Public Class PublicIISLogs <WebMethod(Description:= _ "Traffic summary for the previous 52 weeks")> _ Public Function TrafficSummary() As DataSet Dim oDS As New DataSet ... code to fill DataSet as required ... Return oDS End Function End Class

Author's Note: The Namespace property value does not have to be a URI—it can be anything unique, such as a GUID. Also note that the class name in the page directive must match the name of the public class you define within the file or assembly that implements the Web service.

The WebService attribute added to the public class defines the Web service itself. You mark any Public methods within it (any Sub or Function in VB.NET) that you want to expose through the Web service using the WebMethod attribute as shown in the previous listing. You can add Description and other properties to the WebMethod declaration to control caching, ASP.NET session support, buffering and transaction support. Search the .NET SDK for "WebMethodAttribute Members" for more details.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date