Consuming a hosted Web Service using Visual Studio, specifically VWD 2010

Consuming a hosted Web Service using Visual Studio, specifically VWD 2010

  • Comments 8

In this blog we will examine the process to “Consume” a web service located on the web.

Starting the Web Site construction

To begin, create the default web site in Visual Web Developer Express 2010, the process is basically the same as with any of the Visual Studio web tools. The installation is faster, about 10 minutes from start of download to completion. To get started select the ASP.NET Web Site, and I will be using the Visual C# language, mainly because of its built in support of refactoring. For the next few figures, I will be comparing Visual Web Developer express 2010 (VWD 2010) to Visual Web Developer express 2008 (VWD 2008)

Figure 1: Visual Web Developer Express 2010 New Web Site Dialog Box

image

Figure 2: Visual Web Developer Express 2008 New Web Site Dialog Box

clip_image004

Figure 3: VWD 2010, Solution Explorer has more files in it then VWD 2008, many for security purposes

clip_image006

Figure 4: VWD 2008 doesn't have the security items or the site master

clip_image008

From now on in this blog entry I will be using VWD 2010 express and refer to it as VWD, you will be able to use VWD 2008 Express, the pictures will be slightly different.

Building your Web Service Consuming application

You will need to add a “Standard” button to the Default.aspx and a Label to use the following code, here is what the code will look like when you are done, but don’t enter the code yet:

protected void Button1_Click(object sender, EventArgs e)

{

us.z25.Service ws = new us.z25.Service();

Label1.Text = ws.HelloWorld();

}

Getting started with the User Interface or UI:

To create the Button1_Click event, that you see in the introduction to this section, you will need to:

· Add a button to Default.aspx and double click on it in the Visual Studio or Visual Web Dev form (the VSD 2008 Express default is different than the VSD 2010 default web page).

· Add a label, you can change the text, but that isn’t required.

· You DO NOT need to actually type in the code for the event, you will need to type a little code to consume the web service.

Figure 5: How your form might look in VWD 2010 Express

clip_image010

Add Web Reference

Figure 6: Right click the project at the top of the Solution Explorer and then select Add Web Reference

clip_image012

Here is the Add Web Reference dialog box, for the URL, make sure to use your own URL after 1/1/2010, you can check to see if I have anything at z25, you will need to navigate to the site to see what I have. If you get an error, it could mean that I have taken service.asmx off the site or moved it.

Make user to note the Web Reference name out of the textbox just above the Add Reference button.

Figure 7: Add Web Reference, type in the URL plus the service.asmx. Note that a change needs to be made to our hello world application

clip_image014

 

Writing the code to consume a web service

First off let’s talk about the properties of the button and label, for this example, don’t change any of the properties or IDs. Make sure that they look like the following:

clip_image016

 

This is fairly simple coding exercise. Double click the button on Default.aspx, then using the Web Reference name, in this case: us.z25.Service (because Service was the name of the Web Method in the Web service created by VWD by default)

Figure 8: When you double click the button you will see this screen, or a similar screen.

clip_image018

Figure 9: Now construct the Service Class exposed by the Web Service

clip_image020

Figure 10: Now add the Service class, do it on both sides

clip_image022

clip_image024

Figure 11: For reference purposes I have included the default web service from the previous blog entry here, note that there is a public class Service that exposes the method HelloWorld:

clip_image026
 
Run your application

Press the F5 key or ctrl+F5, after a few seconds you will see the following in VWD 2010

clip_image028

Click the button on your new webpage. After a few seconds the web service at http://z25.us/service.asmx will return the phrase: Hello World and look like the following:

clip_image030

 

Conclusion:

Great work!  You have successfully consumed a simple web service!

Leave a Comment
  • Please add 8 and 4 and type the answer here:
  • Post
  • Great post. Thank you for this.

  • Cool post. Thanks, very helpful :)

  • What F***8 Cr**** images

  • RObert P, thanks for the feedback on those first three images, I will see if I can improve them.

    Awesome feedback, got my attention.  

    Now a little feedback for you: No need for the asterik cursing, personally I view all comments and take a look at what I think you are trying to say.  

    For many bloggers this type of language is ignored; makes the blogger upset; etc. In balance an equal number bloggers like any feedback.

    In the future, if you could focus your comments a little on the problem that would make the discourse on the web more polite in general.  

    However, feel free to post as you like here at devschool.  Communications is a part of learning.

    Instead of: "What F***8 Cr*** images"

    You might consider writing: "Images in this blog are blurred, it makes your blog look stupid."  

    See the latter makes your point, adds the important insult we all like to throw out (I have done my share) and allows me to quickly get to the point that you want to make.

    F***8 is short for one of the "seven" words and lends an unneccessary weight to the feedback.  For example you might want to save that for one of the various forums that are talk about the POS Ghostbusters 3 video game or the latest government/college/corporate screw-up.

    Cr*** images: Now this was appropriate weight, and I think you could use the full word without violating the "seven" word rule.

  • Great stuff, thanks.  Would you be able to extend or convert this example, so that a batch job could use this code instead of invoking via ASP.NET?

  • lo que pasa es que tengo una aplicación en visual studio 2010 y la compilo desde visual y funciona bien (la aplicación es un servicio web), y apenas la publico el iis 8 (windows 8 ) el servicio no me deja entrar ala base de datos que esta creada en sqlserver 2008 r2, no se si es que local mente el iis tenga una configuración especifica sobre la entrada a consumir el servicio

  • Pido perdón, pero no tengo una respuesta para usted en esto. He cambiado de enfoque.

  • Pido perdón, pero no tengo una respuesta para usted en esto. He cambiado de enfoque.

Page 1 of 1 (8 items)
Generic Content

Legal Note:

  1. The author, Sam Stokes, is an employee of Microsoft
  2. There is no guarantee that anything I write is correct, I do try to make sure that what I write is correct.
  3. Use anything written in this blog at your own risk
  4. Test what I write about before using the information
  • Sometimes posts will disappear because I realized that they really don't fit with the total chaos of what I write about
  • Links are tested and available on date of publication, what others do with their links are out of my control

Restrictions:

  • Whatever I write on this blog is unedited by management or anyone, which should be obvious from the randomness of the blog.  I simply write about things I like.
  • Microsoft does not restrict me in anyway, so I generallyblather about whatever I want to.
  • For personal reasons I will not write about:
    • Company confidential stuff, mainly because Microsoft is more transparent than any company I have ever worked for.
    • Other people unless they give me permission to write about them