Pierre Greborio.NET

Talking about .NET world

Another way for implementing a web service

Christian's articles and blogs are one of the most interesting information point that focus on how to design, develop and test web services. He shows how it must (should) be done and propose some interesting tools to make them easier.

But; I would share another way to implement a web service maintaining the SOA principle. So, let's start with a basic example: we have to save a contact information (simply first and last name) into our backend system. Since we have just to save the contact, this is a one-way message pattern. We then have to create the schema (XSD) of the request message. Since the XML .NET serialization maps types to XML schema (and viceversa) we can create directly a class library containing the request type:

[XmlType(Namespace="http://www.peway.com/contact/v1/messages")]
public class SaveContactRequest
{
  public string FirstName;
  public string LastName;
}

We have defined the first part of the contract ! Well, now the operation. We can start creating the interface containing the behavior of the service (this isn't mandatory but correct from an OO point of view):

public interface IContactService
{
  void SaveContact(SaveContactRequest contact);
}

Finally we have to implement the interface:

[WebService(Namespace="http://www.peway.com/contact/v1/service")]
public class ContactService : IContactService 
{
  [WebMethod()]
  [SoapDocumentMethod(OneWay=true, ParameterStyle=SoapParameterStyle.Bare)]
  public void SaveContact(SaveContactRequest contact)
  {
    // Save the person here....
  }
}

We have the service class too ! That is really perfect, it should be nice to decorate the interface instead of its implementation, but we can't since the WebService attributa isn't applicable to an interface (I hope that this kind of limitation will have a solution soon). The last thing we have to do is to copy the class library into the bin folder of the web site and add the following asmx document into the web site folder:

<%@ WebService Class="MyLib.ContactService, MyLib" %>

That's it !! The web service is ready to run.

Posted: Dec 14 2004, 09:07 PM by PierreG | with 2 comment(s)
Filed under:

Comments

Christian Weyer said:

Well, this is old-school Web services - besides the interface approach. Which is an important point, BTW.
ASMXv2 will heal us:
http://weblogs.asp.net/cweyer/archive/2004/09/14/229286.aspx
# December 15, 2004 12:47 AM

Pierre Greborio said:

Old-school doesn't means bad-school ;-) I think that it is a good alternative (better in ASP.NET 2.0) to WSDL first. Having a complete tool (yours and WhiteHorse) supporting the WSDL generation make it equivalent in a web service design/production phase.
# December 15, 2004 1:57 AM