kirupa.com - Creating a Web Service, Page 5

       by kirupa  |  11 August 2006 In the previous page, you finished creating your web service. In this page, I will provide a quick recap of why what you did earlier works, and I will conclude with an explanation of what the code does. Now that you have your web service created, let's take a look at what happens when you load your web service in the browser. This is the URL of my web service, so feel free to click on it if you don't feel like using the web service you created: http://www.kirupafx.com/WebService/TopMovies.asmx. The page you see in your browser is the default page created for you automatically. It includes a lot of valuable information on how to use your web service, and you even get code snippets on how to implement your web service using different protocols. We created two methods for our web service - GetTop10 and GetMovieAtNumber. The web service information page loaded in your browser, lists those two methods as operations your web service provides. Clicking on either the GetTop10 or GetMovieAtNumber operation provides you with the SOAP 1.1 and SOAP 1.2 implementations. To dig deeper what causes your web service page to display this information? It is not simply because your code has it, it is because of the [WebMethod] tag directly above your public method: [ the WebMethod attribute tags ensure that your methods are seen by the web service ] The [WebMethod] tag makes sure that this particular method is exposed to the public via the web service interface. By omitting the [WebMethod] tag, you can ensure your method stays hidden from the public. Since you did copy and paste some code, before I conclude the tutorial, I want to make sure you know what each line of code does: private string[] movieList = { "The Godfather (1972)", "The Shawshank Redemption (1994)", "The Godfather: Part II (1974)", "The Lord of the Rings: The Return of the King (2003)", "Casablanca", "Schindler's List", "Shichinin no samurai (1954)", "Buono, il brutto, il cattivo, Il (1966)", "Pulp Fiction (1994)", "Star Wars: Episode V - The Empire Strikes Back (1980)"}; In the above statement, I declare an array of strings[] called movieList. In my declaration, I go ahead and populate the array with the top ten movies listed on the IMDb web site. [WebMethod] public string[] GetTop10() { return movieList; } The GetTop10 method does nothing but return our earlier movieList array. Notice that the return type of this method is string[] - which is of the same type as the value you are returning. [WebMethod] public string GetMovieAtNumber(int input) { return movieList[input]; } The GetMovieAtNumber returns a value of type string, but notice that it also takes an integer input. Your integer input acts as an index value, and that can be seen by looking at our return line: return movieList[input]; You access specific values in your array by placing the index number next to the brackets such as what you see above. Array index numbering starts at 0, so be sure to take that into account when referring to specific values. Because our movieList array contains objects of type string, the return value is also string. That works, for our method's return type is string also. Got a question or just want to chat? Comment below or drop by our forums (they are actually the same thing!) where a bunch of the friendliest people you'll ever run into will be happy to help you out! When Kirupa isn’t busy writing about himself in 3rd person, he is practicing social distancing…even on his Twitter, Facebook, and LinkedIn profiles. Hit Subscribe to get cool tips, tricks, selfies, and more personally hand-delivered to your inbox.  

This is a companion discussion topic for the original entry at https://www.kirupa.com/net/webService5.htm