Exforsys

Ads


Home arrow Reviews arrow SOA Web Services

SOA Web Services - Developing the .NET Web Service

Author: Packt Publishing     Published on: 1st Aug 2008

Developing the .NET Web Service

Creating a .NET web service using Visual Studio IDE is as simple as creating a Java web service using NetBeans IDE. I used Visual Studio 2005 for creating the web service and the test client application. The IDE provides a template for creating an ASP.NET Web Service.

Ads

Follow the default project options while creating the web service. I used the NetService as the name for my project and selected C# as the development language. The wizard generates a default class for the web service with a default service method. I modified this service method to send a greeting message to the caller. The modified source file is shown in the following listing.

Sample Code
  1. using System
  2. using System.Web
  3. using System.Web.Services
  4. using System.Web.Services.Protocols
  5. [WebService(Namespace = "http://tempuri.org/")]
  6. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  7. public class Service : System.Web.Services.WebService
  8. {
  9.  public Service () {
  10.  //Uncomment the following line if using designed components
  11. //InitializeComponent()
  12. }
  13.  [WebMethod]
  14. public string SayHello() {
  15. return "\nHello from .NET service"
  16. }
  17. }
Copyright exforsys.com


As can be seen from the listing, the Service class inherits from the System.Web. Services.WebServiceclass. The class is attributed with two attributes WebService and WebServiceBinding. The WebServiceattribute specifies the namespace for the defined web service. The WebServiceBinding attribute defines the conformance target.

In our example, the conformance target is Basic Profile 1.1 as specified by the constant from the WsiProfiles class. Within the class definition, each desired method that is to be invoked as a web service method should be annotated using the WebMethod keyword. In our example, the SayHello method is declared as a web method that can be invoked using SOAP.

Deploying the .NET Web Service

Once you write the code for the web service, it can be deployed using the wizard provided in the VS.NET IDE. You may now look up the generated WSDL by opening the following URL in your browser:

http://localhost:20278/NetService/Service.asmx?WSDL

Note that you will need to set up the appropriate port number in the above URL. The generated WSDL is shown in following listing.

Sample Code
  1. <wsdl:definitions targetnamespace="http://tempuri.org/">
  2. <wsdl:types>
  3. <s:schema targetnamespace="http://
  4. tempuri.org/" elementformdefault="qualified">
  5.  
  6.  <s:element name="SayHello">
  7. <s:complextype></s:complextype>
  8. </s:element>
  9.  
  10.  <s:element name="SayHelloResponse">
  11. <s:complextype>
  12. <s:sequence>
  13.  
  14. <s:element name="SayHelloResult" type="s:
  15. string" minoccurs="0" maxoccurs="1"></s:element>
  16. </s:sequence>
  17. </s:complextype>
  18. </s:element>
  19. </s:schema>
  20. </wsdl:types>
  21. <wsdl:message name="SayHelloSoapIn">
  22. <wsdl:part name="parameters" element="tns:SayHello"></wsdl:part>
  23. </wsdl:message>
  24. <wsdl:message name="SayHelloSoapOut">
  25. <wsdl:part name="parameters" element="tns:SayHelloResponse"></wsdl:part>
  26. </wsdl:message>
  27. <wsdl:porttype name="ServiceSoap">
  28. <wsdl:operation name="SayHello">
  29. <wsdl:input message="tns:SayHelloSoapIn" />
  30. <wsdl:output message="tns:SayHelloSoapOut"></wsdl:output>
  31. </wsdl:operation>
  32. </wsdl:porttype>
  33. <wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
  34. <soap:binding transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
  35.  
  36.  <wsdl:operation name="SayHello">
  37. <soap:operation soapaction="http://tempuri.org/SayHello"></soap:operation>
  38.  
  39. <wsdl:input>
  40.  
  41. <soap:body use="literal"></soap:body>
  42. </wsdl:input>
  43.  
  44.  
  45.  <wsdl:output>
  46. <soap:body use="literal"></soap:body>
  47. </wsdl:output>
  48. </wsdl:operation>
  49. </wsdl:binding>
  50.  
  51.  
  52.  <wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">
  53. <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"></soap12:binding>
  54. <wsdl:operation name="SayHello">
  55. <soap12:operation soapaction="http://tempuri.org/SayHello"></soap12:operation>
  56.  
  57.  <wsdl:input>
  58. <soap12:body use="literal"></soap12:body>
  59. </wsdl:input>
  60.  
  61.  
  62.  <wsdl:output>
  63. <soap12:body use="literal"></soap12:body>
  64. </wsdl:output>
  65. </wsdl:operation>
  66. </wsdl:binding>
  67.  
  68.  
  69.  <wsdl:service name="Service">
  70. <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
  71.  
  72.  
  73. <soap:address location="http://localhost:20278/NetService/Service.
  74. asmx"></soap:address>
  75. </wsdl:port>
  76.  
  77.  
  78.  <wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12">
  79.  
  80. <soap12:address location="http://localhost:20278/NetService/Service.
  81. asmx"></soap12:address>
  82. </wsdl:port>
  83. </wsdl:service>
  84. </wsdl:definitions>
Copyright exforsys.com


Ads

If you compare this WSDL against the BP conformance requirements, you will find that this is indeed BP 1.1 conformant.



 
This tutorial is part of a SOA Web Services tutorial series. Read it from the beginning and learn yourself.

SOA Web Services

 

Comments