In an earlier article, I discussed the details involved in creating a web service using the Web Matrix. This tutorial is about creating consumers of those services.
There are a couple of alternatives that can be used to access the web services created using Web Matrix. Two of the methods depend on the usage of VS 2003 IDE. A third method is probably more useful for those who do not have access to VS 2003. In the first method, where you have access to VS2003, you would reference the WSDL obtained by referencing the Web Service ASMX file with the mini-web server connected and running. In the second, you would import the web service file created in Web Matrix to a VS 2003 IDE project and generate the necessary files so that the web service becomes a part of the VS Project.
Importing a Web Matrix Web service into VS 2003 Project: the Web Matrix Service File
The following web service file from web matrix, which happens to be the default project with the Web Method Add (), shown in Fig.1, was used. If a client provides two numbers, the service adds them up and returns the result to the client.
Fig.1
Importing into VS 2003 Project
This file is now imported into a VS 2003 project called FindJay, which has its own web service file, Service1.asmx. This file is not of interest to us for this part of the discussion. At the time of importing you will be notified that a class file does not exist, and one has to be created. If you agree to this the file will be imported. The file structure in the project appears as shown in Fig.2. Notice that the imported test.asmx file has neither a design view, nor a code view. It only has the imported text.
Fig.2
However, the test.asmx file is fully functional as seen in Fig.3. A WSDL reference to this file can be established by simply appending "? Wsdl" to the URL shown in the figure.
The TestWmatrixWsvc project is as shown, with the project node completely expanded, in Fig.4.
Fig.4
A web reference is added by right clicking the References node and choosing Add Web Reference. In the ensuing wizard, the web reference obtained from the previous section is inserted since this is available on the local host as shown in Fig.5 (the xml file is abbreviated). This brings the test.wsdl file shown in the solution explorer, and will be persisted in C:/Documents and Settings/computer user/VSWebCache/XPHTEK/TestWmatrixWsvc/Web References/localhost/test.wsdl. The soap: address is that of the test.asmx in the FindJay project.
Fig.5
The client may be tested now with the code in the Page_Load() event as shown in Fig.6. As the page is browsed, the sum of the hard coded values appears in the browser.
In this method, the reference to the mini-web server will be used to create a consumer of the web service created with Web Matrix using the VS 2003 IDE.
Create an XML web service project having the file name miniweb.asmx as shown. Since it is a web service file, enter a class name and a namespace, as shown in Fig.7. The service will be known by its class name.
Fig.7
The file will be added to the location shown in Fig.8. Since it is a demo for the client, just accept the default web service whose Web Method is Add () as shown. Notice that the class name is Cassini.
Fig.8
Run this application by clicking on the Start arrow (blue colored) in the top menu. This brings up the Start Web Application window where you will use the ASP.NET Web Matrix server, herein called the mini-web server, as shown in Fig.9.
Fig.9
When you click on the Start button in the above screen, the server starts and you will see a screen similar to Fig.10. You may test this window by clicking on the Add hyperlink. You can also get a Service Description (the other hyperlink, not visible in this picture). Notice the URL with http at port 8890. Since this is an asmx file you can easily generate a wsdl file in the VS Studio 2003 by appending "? Wsdl" to the URL address.
Fig.10
Now do not close the mini-web server. Create a Web application, TestMiniweb, which brings in a default web form1. Since this is a client consumer, provide a web reference by right clicking the node References and choose web reference. This opens up the wizard, which will locate the service. This can be browsed to find the mini-web service, or better still, paste the URL from the previous step, http://localhost:8890/miniweb.asmx?wsdl. This establishes the web reference.
Open the code view of web form1 to display as seen in Fig.11 shown here. Since a reference has been established for the web service, you should add the appropriate Imports statement as shown. This page will be instantiating the web service with the new keyword which provides the correct object reference. The code added to write the mysvc.Add (3, 27) will send the arguments a=3, b=27 to the web service. The return value after being evaluated will be written to the screen. You may test this to find that the number 30 is written to the browser.
Fig.11
If you stop your mini-web server and try to refresh the WebForm1.aspx, you will get an error. This service is only up as long as the mini-web server is working.
Create a web service, three.asmx, as in Fig.12, which adds 3 numbers and returns the sum. Make sure you take note of the Namespace and Class that you provide here.
Fig.12
The code for this web service is as shown here in Fig.13:
Fig.13
You may test this service by running the program which produces the following, as in Fig.14:
Fig.14
In order to consume this service, you need to create a proxy. Web Matrix has a tool, which can be accessed from the Tools ->Web Services Proxy Generator. When you click on this you will be generating the dynamic link libraries to be associated with the client code. Fill in the appropriate information as shown here in Fig.15:
Fig.15
Click on the Generate button to generate the files. Make sure the output directory is the same as that in which the asmx file resides. The MyProxyClass.dll will be placed in the bin sub folder. With this preparation you are now in a position to create the client
Add an ASP.NET page called mooru.aspx to a web project. Add a button which, when clicked, will access the web service. In the click event declare a mysvc as a new service with the syntax <namespace.class> (sugaku.numero). Implement code that will write the result to the screen as shown in Fig16. That is all there is to it.
Fig.16
When you browse the file mooru.aspx, the browser opens with the button in view. If you click on the button the display will be as seen in Fig.17 below.
Fig.17
Summary
Web Matrix has a neat interface for creating both a Web Service Provider as well as a consumer. It is also possible to refer to, or import, Web Matrix created web service files in VS 2003 projects to create consumers. The mini-web server is an excellent medium for trying out samples or prototypes. You can browse the files that are exposed by the mini-web server by accessing them in the browser. In the case of importing a file into a project, although the route to the client has been somewhat tortuous, the end result shows that all the interfaces have functioned consistently and correctly.