Sunday, January 15, 2012

how to read from an .asmx web service and how to build your own .asmx web service


I made a simple calculator in a new ASP.NET Web Forms project. There were two textboxes for users to enter numbers and a button one could click to display the sum of the two numbers entered. Here is the web form:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"

   AutoEventWireup="true" CodeBehind="Default.aspx.cs"

   Inherits="CalculatorThatUsesWebService._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

   <h2>The Calculator!</h2>

   <asp:TextBox ID="FirstTextBox" Width="30" runat="server"></asp:TextBox>

   <asp:Label ID="FirstLabel" Width="15" runat="server">+</asp:Label>

   <asp:TextBox ID="LastTextBox" Width="30" runat="server"></asp:TextBox>

   <asp:Button ID="EqualsButton" runat="server" Text="="

         onclick="EqualsButton_Click" />

   <asp:Label ID="LastLabel" runat="server"></asp:Label>

</asp:Content>

 
 

Now here is the code behind's C#. Note that I hand the values to something called "Reference" to do the calculation and then get back the calculation from Reference.

using System;

namespace CalculatorThatUsesWebService

{

   public partial class _Default : System.Web.UI.Page

   {

      protected void Page_Load(object sender, EventArgs e)

      {

      }

      

      protected void EqualsButton_Click(object sender, EventArgs e)

      {

         Reference.CalculatorWebService calculator =

               new Reference.CalculatorWebService();

         int firstInteger = Convert.ToInt32(FirstTextBox.Text);

         int lastInteger = Convert.ToInt32(LastTextBox.Text);

         int magicNumber = calculator.AddTwoIntegers(firstInteger, lastInteger);

         LastLabel.Text = magicNumber.ToString();

      }

   }

}

 
 

Reference is a pointer to an .asmx web service hosted outside of this application at http://www.damisam.com/CalculatorWebService.asmx. Here is how I added it to my application in Visual Studio 2010:

  1. From the Solution Explorer, I right-clicked on the one project in my solution and selected: Add Service Reference...
  2. The "Add Service Reference" dialog appeared and I clicked on the "Advanced..." button.
  3. I ended up at the "Service Reference Settings" dialog box and from here I clicked "Add Web Reference..."
  4. The third dialog box I then saw was "Add Web Reference" and I herein put http://www.damisam.com/CalculatorWebService.asmx in the URL field.
  5. I clicked the green right arrow at the right of the URL field.
  6. I was informed that CalculatorWebService was found.
  7. There was a button for me to click to create a reference that said "Add Reference" on it, but before I clicked the button I changed the suggested name from "com.damisam.www" to just be "Reference."
Addendum 8/22/2015: I am commenting out http://a.yfrog.com/img878/1734/sbf7.jpg which yfrog has seen fit to replace with some sort of iTunes advertisement. I wish I had not started up my blog hosting images with these clowns.

 
 

The content above naturally begs the question: "How does one author a web service?" It is pretty easy. Spin up a new web application that you will put online. If you right-click on project in the Solution Explorer and pick Add > New Item... you should be able to find a possibility that is vaguely called "Web Service" at the bottom of the list of items in the "Web" subset of the Visual C# options. Web Services can take many shapes, but we are using an .asmx web service in this case.

Addendum 8/22/2015: I am commenting out http://a.yfrog.com/img878/9611/uyp3.jpg which yfrog has seen fit to replace with some sort of iTunes advertisement. I wish I had not started up my blog hosting images with these clowns.

 
 

The .asmx I made just looks like this one line of code when published to be put online at http://www.damisam.com/CalculatorWebService.asmx:

<%@ WebService Language="C#" CodeBehind="CalculatorWebService.asmx.cs"

      Class="WebServiceApplication.CalculatorWebService" %>

 
 

...but it also has a code behind that will get compiled:

using System.Web.Services;

namespace WebServiceApplication

{

   [WebService(Namespace = "http://tempuri.org/")]

   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

   [System.ComponentModel.ToolboxItem(false)]

   public class CalculatorWebService : System.Web.Services.WebService

   {

      [WebMethod]

      public string HelloWorld()

      {

         return "Hello World";

      }

      

      [WebMethod]

      public int AddTwoIntegers(int initialInteger, int integerToAppend)

      {

         return initialInteger + integerToAppend;

      }

   }

}

 
 

I suppose it is important to note that one must publish an .asmx web service for another to use it. Once made, an .asmx it can't just sit on your laptop. It needs to end up on a web site somewhere so that someone may latch onto it by way of a url such as http://www.damisam.com/CalculatorWebService.asmx.

That is really all there is to it. The .asmx approach is really an old school one. I have familiarized myself with .asmx anew today as I am going to help a friend with this manner of web services in a few minutes. I was able to just dig back into my old notes from Visual Studio 2005 era projects. I'm glad I've kept such good notes over time.

Addendum 8/22/2015: I am commenting out http://a.yfrog.com/img876/8194/fxx1.jpg which yfrog has seen fit to replace with some sort of iTunes advertisement. I wish I had not started up my blog hosting images with these clowns.

No comments:

Post a Comment