Page 209 - DCAP312_WEB_TECHNOLOGIES_II
P. 209

Unit 10: Web Services



                     [WebService(Namespace= “http://tempuri.org/ “)]                              Notes
                     [WebServiceBinding(Conformist= WsiProfiles.BasicProfile1_1)]
                     // To allow this Web Service to be called from script, using
                     ASP.NET AJAX, uncomment the followingline.
                     //[System.Web.Script.Services.ScriptService]
                     public class Service :System.Web.Services.WebService
                     {
                     [WebMethod]
                      public int Multiplication(int a,int b)
                     {
                     return (a*b);
                      }
                     }
            Before Debugging the above Web Service see some important term:

            Using System.Web.Services
            This directive allows you to refer to objects in the System.Web. Services namespace without
            having to fully qualify the request. This statement is optional, but if it is not included, every
            reference to an object in this namespace must be fully qualified. An example is the next line,
            which is our class declaration. With the using statement, it looks as follows in C#:

            The [Web Method] attribute
            The Service class exposes a single method, the public method Multiplication, which takes two
            integer arguments and returns the multiplication of two number as integer. To expose a method
            as a part of a web service, you must decorate it with the Web Method attribute, which tells the
            compiler to treat it as such. Any method marked with the Web Method attribute must be defined
            as public. Class methods exposed as web services follow the same object-oriented rules as any
            other class, and therefore methods marked private, protected, or internal are not accessible and
            will return an error if you attempt to expose them using the Web Method attribute.
            In the Solution Explorer you will see

















            Service.asmx- which contains the following code:
                     <%@ WebService Language=“C# “ CodeBehind=“~/App_Code/Service.
                     cs “ Class= “Service” %>
            The page directive WebService is required and the class is the name of the .NET Class to expose
            the WebService, each method exposes as WebService Class Method need to have a declarative
            attribute statement.



                                             LOVELY PROFESSIONAL UNIVERSITY                                   203
   204   205   206   207   208   209   210   211   212   213   214