Library tutorials & articles
Data Integrity in Web Services
- Introduction
- Creating the web service
- The Test Client
- Conclusion
Creating the web service
The web service below is a simple web service that simply passes the struct PersonData back and forth assigning it to a static member. A useful extention of this example is to build up some object-relational mapping between our Person object and a database, but that is beyond the scope of this article.
The areas of note in this object are the PersonData struct and the Person object. Please note that YearsExperience does indeed have an accessor method. Also note that the Person object does a complete range checking on the struct as it is passed in before allowing an assignment.
To start this project go to File->New Project and then choose a ASP.NET Web Service from the selection menu. The complete code listing is below:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace RemoteObjectPasser
{
public class PersonService : System.Web.Services.WebService
{
static Person person;
public PersonService()
{
InitializeComponent();
if(person==null)
{
person = new Person();
PersonData serviceData = new PersonData();
serviceData.FirstName="David";
serviceData.LastName="Talbot";
serviceData.YearsExperience=5;
person.CurrentData=serviceData;
}
}
private void InitializeComponent() {}
protected override void Dispose( bool disposing ) { }
[WebMethod]
public PersonData GetPersonData()
{ return person.CurrentData; }
[WebMethod]
public void SetPersonData(PersonData pd)
{ this.person.CurrentData = pd; }
}//END OF PersonService Object
public struct PersonData
{
private int yearsExperience;
public int YearsExperience
{
get { return yearsExperience; }
set
{
if(value<2) { throw new Exception("You're unemployable!"); }
yearsExperience = value;
}
}
public String FirstName;
public String LastName;
}
public class Person
{
private PersonData personData;
public PersonData CurrentData
{
get { return personData; }
set
{
if(value.FirstName.Length > 20)
{ throw new Exception("FirstName must be less than 20 characters"); }
if(value.LastName.Length > 20)
{ throw new Exception("LastName must be less than 20 characters"); }
if(value.YearsExperience < 2)
{ throw new Exception("People with less than 2 years exp are unemployable in IT."); }
personData=value;
}
}
//Other useful methods to operate on a person
}//END OF PERSON OBJECT
}//END of RemoteObject Passer Namespace
|
Related articles
Related discussion
-
MOS Protocol - Anyone used it?
by alexnavarro38 (5 replies)
-
Read HSQLDB data into a webpage
by joe90 (3 replies)
-
VB.NET Simple Client / Server Communication
by r0bbyw (3 replies)
-
Dot NET questions.
by amohanece (0 replies)
-
ListView-How to use it?
by imiazlina (8 replies)
Related podcasts
-
Remoting Pt. 2
Podcast (MP3): Download Hosts: Markus Michael Guests: Recording venue: This is the second part of the remoting infrastructures discussion started in Episode 9. We take a look at how remoting infrastructures such as CORBA, .NET Remoting or Web Service...
Events coming up
-
Nov
18
15 Minutes of Fame
Dresher, United States
This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.
This thread is for discussions of Data Integrity in Web Services.