O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a .NET Web Service

Problem

You want to create a web service in ASP.NET.

Solution

In Visual Studio, go to New→Web Site→ASP.NET Web Service. Then enter the implementation code in the generated template.

Discussion

This quick recipe highlights the similarities between a Java EE 5 web service and one written in .NET using C#. Example 12-10 shows the boilerplate code Visual Studio generates when you create a new web service.

Example 12-10. C# web service template for ASP.NET

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
}

If you substitute in your mind the annotations you use in Java EE 5 with the square brackets used in C# (or the angle brackets used in VB.NET), it’s pretty easy to get around. Much of the Java EE 5 annotations are based on this way of creating web services in .NET, as this is basically the API Microsoft has provided since 2002. Instead of calling them “annotations” as you do in Java, in .NET the meta-code above classes and methods are called “attributes.”

You can see that the code itself indicates that it will conform to the Basic Profile version 1.1. Let’s add a few other attributes to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required