Search

Creating and Consuming a Web Service

1 views

One of the most powerful aspects of .NET is the ease with which one can create Web Services. A Web Service is an external interface provided by a Web site that can be called from other Web sites. For example, a financial company may make up to the minute stock quotes available via a Web Service for those who do their trading with that company. This information could be read from a Web page and displayed, or read from a stand-alone application on a customer's desktop computer. In this article we'll examine the two parts of a Web Service: how to create it and how to consume it. Specifically, we'll be creating a Web Service that exposes the FAQs from <%@ WebService Language="VB" Class="ASPFAQs" %> Imports System.Web.Services Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Public Class ASPFAQs &nbsp Private Function GetDataSet(strSQL as String) as DataSet &nbsp&nbsp '1. Create a connection &nbsp&nbsp Dim myConnection as New SqlConnection(ConnectionString) &nbsp&nbsp '2. Create the command object, passing in the SQL string &nbsp&nbsp Dim myCommand as New SqlCommand(strSQL, myConnection) &nbsp&nbsp myConnection.Open() &nbsp&nbsp '3. Create the DataAdapter &nbsp&nbsp Dim myDataAdapter as New SqlDataAdapter() &nbsp&nbsp myDataAdapter.SelectCommand = myCommand &nbsp&nbsp '4. Populate the DataSet and close the connection &nbsp&nbsp Dim myDataSet as New DataSet() &nbsp&nbsp myDataAdapter.Fill(myDataSet) &nbsp&nbsp myConnection.Close() &nbsp&nbsp 'Return the DataSet &nbsp&nbsp Return myDataSet &nbsp End Function <WebMethod()> Public Function GetCategories() as DataSet &nbsp&nbsp Return GetDataSet(SQL Query for Retrieving the FAQ Categories) &nbsp End Function &nbsp <WebMethod()> Public Function GetFAQsInCategory(catID as Integer) as DataSet &nbsp&nbsp Return GetDataSet(SQL Query for Retrieving the FAQs for Category catID) &nbsp End Function &nbsp <WebMethod()> Public Function GetFAQ(FAQID as Integer) as DataSet &nbsp&nbsp Return GetDataSet(SQL Query for Retrieving the FAQ FAQID) &nbsp End Function End Class Some things to note: the three Web Service-accessible methods are predicated with <WebMethod()>; at the top of the .asmx file is a @WebService directive that specifies the language and class in the file; the Web Service is named ASPFAQs, as shown by the class name. Once you have created this .asmx file and stored it on a Web-accessible directory, you can view the methods by visiting the page directly through your Web browser. For example, I named my Web Service file ASPFAQs.asmx and saved it in the /ws directory; so, by visiting Consuming a Web Service For a Web site to consume a Web Service, a rather complicated and terse communication must occur between the client Web site (henceforth referred to as the "consumer") and the Web site that is providing the Web server (henceforth the "producer"). Essentially, the consumer must decide what producer's method it wishes to call. If there are input parameters, these parameters must be converted into XML to be passed along. An HTTP request is then made from the consumer to the producer, specifying the method it wishes to call and passing along the parameters in either a SOAP request, through the QueryString, or in the POST headers. The producer receives the incoming request, unpackages the input parameters, and calls the appropriate method of the specified class. This method, when complete, returns some value, which is packaged up and sent back to the consumer in an HTTP response. The consumer receives this response, unpackages the return value, completing the Web Service call. Clearly we'd like to not have to worry about the HTTP message passing semantics at all when using Web Services. In order to remove this as a concern, we use what is called a Proxy class. Proxy classes serve as an intermediate step between the program (or Web page) on the consumer and the actual Web service on the producer. For each method in the producer's Web Service, there is a method in the Proxy class. It is the responsibility of the Proxy class to do all of the complicated message-passing tasks; essentially this complexity is hidden in the class, our Web page can simply call the methods of this class and not concern itself with the underlying semantics involved in calling a Web Service. What in the World did he just Say? By this point you're likely thoroughly confused, and that's understandable, this can be a confusing topic. The fundamental thing to understand is that the HTTP communications that must occur between a consumer and producer when calling a Web Service can be complicated and require much code. We'd prefer to have our Web pages that utilize Web Services be able to invoke the Web Service just as if it were a local component. In order to accomplish this, a Proxy class is used, whose public interface mirrors that of the Web Service. If you are still confused, consider viewing 'HTML content in .aspx page... <asp:datagrid id="dgCategoryFAQs" runat="server" /> '* ------------------------------------------------ *' 'code content in the code-behind page Private Sub Page_Load(sender as Object, e as EventArgs) &nbsp 'Create an instance of the Proxy class &nbsp Dim consumeWebService as com._4guysfromrolla.aspnet.ASPFAQs &nbsp Set consumeWebService = New com._4guysfromrolla.aspnet.ASPFAQs &nbsp 'Bind the results of GetFAQsInCategory to dgCategoryFAQs &nbsp dgCategoryFAQs.DataSource = consumeWebService.GetFAQsInCategory(22) &nbsp dgCategoryFAQs.DataBind() End Sub From simply examining the code you could not determine that the call to the com._4guysfromrolla.aspnet.ASPFAQs Proxy class is, in actuality, a remote Web service call. When the Proxy class's GetFAQsInCategory method is called, the complex communications discussed previously occur (the remote HTTP request/response dialogue). Creating a Proxy Class without Visual Studio .NET Creating a Proxy class through Visual Studio .NET is very easy; however, if you don't have Visual Studio .NET, you can still create Proxy classes, but you must do so through the command-line. For more information on this technique be sure to read the PowerPoint presentation: Calling a Web Service from an ASP.NET Web Page. Conclusion In this article we examined how to create a Web Service and then how to consume it from an ASP.NET Web page using Visual Studio .NET. Microsoft has really simplified the process of producing and consuming Web Services with .NET. Creating a Web Service is as simple as creating a .asmx file and writing the code for the Web Service methods - save for the <WebMethod()> macros, the code appears nearly identical to the code one would write for a local component. Consuming a Web Service is painfully simple too, thanks to the use of Proxy classes. On top of that, the creation of Proxy classes is utterly simple through tools such as Visual Studio .NET. *Originally published at Scott Mitchell, author of five ASP/ASP.NET books and founder of

Found an error or have a suggestion? Let us know and we'll review it.

Share this article

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!