Browse DevX
Sign up for e-mail newsletters from DevX


Free Your C# Apps from .NET Platforms : Page 2

Visual MainWin offers unprecedented platform flexibility, allowing you to develop applications in C# and deploy and run them on J2EE. Learn how to take advantage of this freedom by building a C# Web service that you can run on platforms besides .NET and IIS.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Building and Consuming a Simple Web Service
To add a Web method that returns a value, add the following code to the Web service from the previous section:

[WebMethod] public string echoHelloWorld(string strMyName) { StringBuilder strReturn = new StringBuilder("Hello World to you, "); strReturn.Append(strMyName); return strReturn.ToString();}

Running the application gives you a similar result to what you saw earlier. Append the string '?WSDL' to the url as specified, and see the WSDL page for this Web service. [i.e. http://localhost:8080/DevxMainWin1/Service1.asmx?WSDL]

Figure 3. This is the Web form design.

To consume this Web service, you will now build a Web application. To do this, make sure that the application isn't running and then right-click the project in the Solution Explorer and select 'Add Web Form.' The 'Add New Item' dialog box will appear with 'Web Form' selected as default. Change the default name to 'ConsumeDevx.aspx' and hit OK to create the Web Form.

Create a Web reference to the Web service by right-clicking DevxMainWin on the Solution Explorer and selecting 'Add Web Reference.' In the 'Add Web Reference' dialog that pops up, enter the WSDL url (from above), and select 'Add reference.'

Add labels and text boxes to the Web form as in Figure 3.

Double click the 'Go' button to add an event handler for clicking the button. Add the code from Listing 2 to this. This is assuming that the 'top' text box in Figure 3 is called TextBox1 (the default), and the 'bottom' one is called TextBox2.

private void Button1_Click(object sender, System.EventArgs e) { localhost.Service1 myWS = new localhost.Service1(); string strMyName = TextBox1.Text; string strReturn = myWS.echoHelloWorld(strMyName); TextBox2.Text = strReturn;}

Figure 4. Here's a deployed ASPX, running on Tomcat, that consumes the Web Service.

Before you run the application, make sure that the aspx is the 'default' page for the application by right-clicking it and selecting 'Set as Start Page.'

When you run the application, it is compiled and deployed onto Tomcat for you by the Visual MainWin plug ins to Visual Studio. The results are shown in Figure 4.

Now is that cool or what? You have just built a C# Web forms application and a C# Web services application, tied them together with Visual Studio Web services proxies, and compiled and deployed them—all on an application server without tweaking a single app server configuration file!

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date