You learned how to create a sample ASP.NET Web application in "Building Web Applications on the .NET Platform." Creating Web services with the ASP.NET Framework is as simple as creating Web applications. Similar to creating Web applications, you need a text editor and the ASP.NET runtime to create Web services.
An ASP.NET Web service consists of an .asmx file that contains the code for the Web service. Let's look at a sample .asmx file for a Web service that returns the day of the week. The .asmx file contains a Web processing directive that specifies a class name for a Web service and the language used to create the Web service. You can create the .asmx file in a text editor, such as Notepad. To do so, add the following code to a Notepad file:
After you add the code to a Notepad file, save the file with the extension .asmx.
The preceding code creates a class, SampleService, in Visual Basic .NET. In addition, the code imports the System. Web. Services namespace to your application. Next, a Web method, QuoteForTheDay(), is declared inside the SampleService class. The Web method returns a string to the requesting application.
As you can see, the .asmx file contains the code for the Web service. Therefore, each time an application sends a request for the Web service, the code in the .asmx file is compiled. (Here, we are not taking into account the fact that pages are being cached.) To prevent this, you can shift the code that provides the functionality into a separate Visual Basic .NET or Visual C# file. You then can compile the code for the Web service and store it in an assembly. The client application can access the assembly through the .asmx file. However, to be able to access the assembly from a client application, you first need to create a reference to the file that contains the code for the Web service. To do this, you use the Codebehind attribute of the Web service directive, as shown in the following code snippet:
The previous code contains the Codebehind attribute that has a reference to the Service1.asmx.vb file. Service1.asmx.vb is a Visual Basic .NET file that contains the code to provide the functionality for the Web service. You can use the previous code to implement a Web service in Visual Studio .NET by using .NET languages, such as Visual C# and Visual Basic .NET.
Creating a Web Service Using Visual Studio .NET
Similar to ASP.NET Web applications, Visual Studio .NET also provides you with a template to create an ASP.NET Web service. The steps for creating an ASP.NET Web service are similar to those for creating an ASP.NET Web application. To create an ASP.NET Web service, perform the following actions:
You cannot specify a name for the Web service in the Name text box. To specify a name for the Web service, type the name in the Location text box.
After specifying the values in the New Project dialog box, the dialog box should look like Figure.
The New Projects Dialog Box
When you create a Web service by using the ASP.NET Web services template, Visual Studio .NET creates a simple Web service project that you can see in the Solution Explorer window. The design view for a Web service looks like Figure.
The IDE for a Web Service.
As you can see in the Solution Explorer window, Visual Studio .NET creates some default files for the Web service. Table explains the default files that Visual Studio .NET creates.
Default Files for a Web Service
Look at the code in the .asmx file that Visual Studio .NET creates for a Web service:
As you can see, the Web service directive contains a Codebehind attribute that has a reference to the Service1.asmx.vb file. The code for the Web service is present in this file. In addition, a class directive is included that defines a class, Service1, for the Web service. Visual Studio .NET allows you to view the code present in the Service1.asmx.vb file.To do this, perform the following steps:
The code for the Web service is as shown:
The preceding code contains the WebService attribute that declares http://tempuri.org as the default namespace for the Web service, as shown in the following code snippet:
By default, http://tempuri.org is declared as the default namespace. To specify another default namespace, you can change the value of the Namespace property of the WebService attribute. You can also change the Name and Description properties of the WebService attribute to specify a name and provide a description for the Web service, respectively. The code for the WebService attribute is as shown:
The code contains the declaration for a public class, Service1. This class is inherited from the System. Web. Services. WebService class. Then, the code contains the # Region preprocessor directive, which contains the boilerplate code for the Web service. Visual Studio .NET generates this code and provides the same functionality for all ASP.NET Web services. The developers rarely need to change this code.
The #Region preprocessor directives are used to mark a region of the code that you need to have executed as a block. To end the region marked with the #Region preprocessor directive, you use the #End Region preprocessor directive.
After the #Region-#End Region preprocessor directives is the code for the Web method, HelloWorld(), which is marked with comment entries. A Web service contains one or more Web methods that provide you with the functionality of the Web service. A Web method is similar to any other function but is preceded by the WebMethod attribute. The use of this attribute differentiates a Web method from all other methods and makes the Web method accessible to Web applications. The following list discusses some of the properties of the WebMethod attribute:
When you create the Web service in Visual Studio .NET, it creates a sample Web method, HelloWorld(), and marks the Web method as a comment. To implement the Web method, remove the comment entries and run the Web service. When you run the Web service, the HelloWorld() Web method returns a string, HelloWorld, as shown in Figure
The Web Service in Visual Studio .NET.
When you run the Web service, the Service1.asmx page is opened on the http://localhost/NewsShop/Service1.asmx URL. The Service1. asmx page provides you with the links for the functionality that the Web service exposes. Because the sample Web service contains only the HelloWorld() Web method, you will see only one link for the HelloWorld operation. In addition, the Service1.asmx page contains a link, Service Description. When you click on this link, the WSDL document for the Web service is displayed, as shown in Figure.
The WSDL Document for the Web Service.
The Service1.asmx page also recommends that you change the default namespace. The Web service that Visual Studio .NET creates uses http://tempuri.org as the default namespace. You saw how to change the default namespace for a Web service in the previous section.
When you click on the HelloWorld link, the Service1.asmx page for the HelloWorld operation opens. This page contains an Invoke button that you can use to invoke the HelloWorld Web service. In addition to the Invoke button, the page specifies the syntax for invoking the operation. As you know, you can call a Web service by sending a request in the form of a SOAP message. The Service1.asmx page shows the request for and the response of a Web service in the form of SOAP messages, HTTP GET, and HTTP POST protocols, as shown in Figure.
The Service1.asmx Page for the HelloWorld Operation.
Now we will discuss the request and response messages for the SOAP protocol. The following is the code for the request message:
The code contains the XML declaration statement followed by the soap:Envelope element that includes the namespaces to be used in the SOAP message. Then, the soap:Body element that contains a request for the HelloWorld operation is included. Similarly, the syntax of the response message is as shown:
The soap:Envelope element is the same for the request and response messages. However, the soap:Body element is different. The soap:Body element in the response message returns a string to the requesting application.
When you click on the Invoke button, the response of the Web service is sent in the form of an HTTP-GET message, as shown in the following code:
The response of the Web service is shown in Figure.
The Response of the Web Service.
Now, change the WebMethod attribute for the HelloWorld Web method as shown:
After making the changes as in the previous code snippet, run the Web service again. As you can see, the recommendation for changing the namespace is no longer present in the Service1.asmx page. In addition, the Web service now has a description. The Service1.asmx page is shown in Figure.
The Modified Web Service Description.
After you learn to create a sample Web service, you need to create a database for NewsShop that will store the data to be returned by the Web service.
You should provide a description for a Web service and the Web methods by using the Description property. Doing this helps other users who want to use the Web service.
XML Related Interview Questions
|Soap Tool Interview Questions||HTML Interview Questions|
|PHP Interview Questions||ASP.NET Interview Questions|
|PHP5 Interview Questions||Java Interview Questions|
|CSS Interview Questions||XSLT Interview Questions|
|Java XML Interview Questions||XMLHttpRequest (XHR) Interview Questions|
|ebXML Interview Questions||XML DOM Interview Questions|
|XML-RPC Interview Questions||XSD Interview Questions|
|Soap Web Services Interview Questions||XSL Interview Questions|
|Xml Publisher Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.