My first Web Services in .NET with C #

In this opportunity I will show you a simple way to make a web services using C #.

To begin we must create a new project of type ASP.NET Web Services Application. We can find this project in the Visual C # -> Web menu, however it is important to previously select the .NET Framework 3.5 (after creating it we can change the framework).

ws1

After typing the name and selecting the route of our project, press the OK button.

Visual Studio by default will create a web services called Services1.asmx.cs

ws2

Most of the programmers when they are starting work directly on this class, which is why we will find many web services called Services1. I recommend removing this class and creating a new one with a name more consistent with the ones we are going to make.

To create a new Web Services we just have to do the following:

  • Remove Services1 (Optional)
ws3
ws4
  • Agregar nuevo Web Services haciendo clic derecho sobre el proyecto -> add -> New Item…
ws5
  • Seleccionar Web -> Web Services  y poner un nombre referente a la acción que realizará, para este ejemplo hare una suma que solicite dos parámetros y devuelva el resultado, es por esta razón que el web services se llamara wsCalculos. Al terminar la selección presionar Add.
ws6
  • Select Web -> Web Services and put a name referring to the action you will perform, for this example I will make a sum that requests two parameters and returns the result, that is why the web services will be called wsCalculos. When finishing the selection press Add.
using System.Web.Services;
namespace MiPrimerWs
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
  public class wsCalculos : System.Web.Services.WebService
  {
     [WebMethod]
     public double Sumar(int value1, int value2)
     {
        return value1 + value2;
     }
  }
}
  • With these simple steps we have everything ready to publish our web services and start using it. On this occasion we will publish it locally. For this we only have to click on the project and press View in Browser (when doing this visual recompiles our project and publishes it locally).
ws7

We wait a few seconds for our browser to open. We select the class that we just created wsCalculos.asmx

ws8

We select the Sumar method (if you want to omit or change the format of this screen you must change the namespace [WebService (Namespace = «http://tempuri.org/»)] by putting any other url).

ws9

Once you select our method, the following screen will appear where you must enter the parameters that you request, as you can see they are the same names of our method. To finish just click on Summon.

ws10

A new page will appear with the result.

ws11

I hope it is useful to you. If you have any questions, do not hesitate to leave us your comments.

regards

También te podría gustar...

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *