Example calling an external webservice

Discussion in 'ASP .Net' started by Marc, Nov 5, 2008.

  1. Marc

    Marc Guest

    I am trying to study ASP.NET by making some example programmes in Visual Web
    Developper 2005 Express Edition.

    I am trying to write a client that consumes a simple webservice like:

    http://www.webservicex.net/uklocation.asmx?WSDL

    I've added the webservice to my project, but how do I proceed? I have found
    several examples but nothing clear what I really want. For example I have
    found the below but that's with localhost. I do not have the webservice on
    local host. Nor do I understand where localhost should be defined anyway.

    protected void ConvertButton_Click(object sender, EventArgs e)
    {
    localhost.Convert wsConvert = new localhost.Convert();
    double temperature =
    System.Convert.ToDouble(TemperatureTextbox.Text);
    FahrenheitLabel.Text = "Fahrenheit To Celsius = " +
    wsConvert.FahrenheitToCelsius(temperature).ToString();
    CelsiusLabel.Text = "Celsius To Fahrenheit = " +
    wsConvert.CelsiusToFahrenheit(temperature).ToString();
    }

    Please give me some hints!

    Marc Wentink
     
    Marc, Nov 5, 2008
    #1
    1. Advertisements

  2. Try this walkthrough:
    http://msdn.microsoft.com/en-us/library/aa290754(VS.71).aspx

    You will have to set up the service to do it. Once you understand that, you
    can create other services or connect to the one you want to consume. I could
    show you with the service you have pointed out, but it is extremely slow.
     
    Cowboy \(Gregory A. Beamer\), Nov 5, 2008
    #2
    1. Advertisements

  3. I think you are not looking for the Client. Here is an example I have not
    tested, but it has the basic methodology wrapped in a class.

    Service = http://ws.cdyne.com/WeatherWS/Weather.asmx?wsdl

    Class =

    using ServiceConsumptionLibrary.WeatherService;

    namespace ServiceConsumptionLibrary
    {
    public class ServiceConsumer
    {
    public static WeatherService.ForecastReturn
    GetForecastForUsPostalCode(string postalCode)
    {
    var client = new WeatherHttpGetClient();
    var forecast = client.GetCityForecastByZIP(postalCode);

    return forecast;
    }
    }
    }

    I have not tested through this yet (I would create a unit test, personally),
    but the basic idea is sound and works like .NET 2.0+ works with all
    services. And, yes, it is a bit more complex than 1.x.
     
    Cowboy \(Gregory A. Beamer\), Nov 5, 2008
    #3
  4. Marc

    Marc Guest

    "Cowboy (Gregory A. Beamer)" <> schreef


    Ok, now for something silly, and I am sorry this must be a real newbee question, and concurrently I am searching to solve this myself, but:

    using ServiceConsumptionLibrary.WeatherService;
    Gives

    Error 1 The type or namespace name 'WeatherService' does not exist in the namespace 'ServiceConsumptionLibrary' (are you missing an assembly reference?) C:\Documents and Settings\Marc wentink\Mijn documenten\Visual Studio 2005\WebSites\H8Async\App_Code\ServiceConsumer.cs 10 33 C:\...\H8Async\

    Now normally I would add a link to an assembly, a dll, with WeatherService in it, but what do I do now? I presume I do not have a dll with WeatherService in it.
     
    Marc, Nov 5, 2008
    #4
  5. Marc

    Jeff Dillon Guest

    Add Web Reference in Solution Explorer

    "Cowboy (Gregory A. Beamer)" <> schreef


    Ok, now for something silly, and I am sorry this must be a real newbee question, and concurrently I am searching to solve this myself, but:

    using ServiceConsumptionLibrary.WeatherService;
    Gives

    Error 1 The type or namespace name 'WeatherService' does not exist in the namespace 'ServiceConsumptionLibrary' (are you missing an assembly reference?) C:\Documents and Settings\Marc wentink\Mijn documenten\Visual Studio 2005\WebSites\H8Async\App_Code\ServiceConsumer.cs 10 33 C:\...\H8Async\

    Now normally I would add a link to an assembly, a dll, with WeatherService in it, but what do I do now? I presume I do not have a dll with WeatherService in it.
     
    Jeff Dillon, Nov 5, 2008
    #5
  6. Marc

    Marc Guest

    No I did that.
     
    Marc, Nov 5, 2008
    #6
  7. Here are the steps, based on the code I have:

    First create a reference to the service
    Name it WeatherService
    Then add the code and run it

    I will have to run that code and make sure it is fine. I will not have a chance until this weekend to set up a sample application, with a simple web service (and instructions). I will slap it up on my blog and post a note here when I am done. That way you will have an "instruction manual" in the blog entry.



    "Cowboy (Gregory A. Beamer)" <> schreef


    Ok, now for something silly, and I am sorry this must be a real newbee question, and concurrently I am searching to solve this myself, but:

    using ServiceConsumptionLibrary.WeatherService;
    Gives

    Error 1 The type or namespace name 'WeatherService' does not exist in the namespace 'ServiceConsumptionLibrary' (are you missing an assembly reference?) C:\Documents and Settings\Marc wentink\Mijn documenten\Visual Studio 2005\WebSites\H8Async\App_Code\ServiceConsumer.cs 10 33 C:\...\H8Async\

    Now normally I would add a link to an assembly, a dll, with WeatherService in it, but what do I do now? I presume I do not have a dll with WeatherService in it.
     
    Cowboy \(Gregory A. Beamer\), Nov 6, 2008
    #7
  8. Marc

    Marc Guest

    "Cowboy (Gregory A. Beamer)" <> schreef

    "I will have to run that code and make sure it is fine. I will not have a
    chance until this weekend to set up a sample application, with a simple web
    service (and instructions). I will slap it up on my blog and post a note
    here when I am done. That way you will have an "instruction manual" in the
    blog entry."


    Thanks! You're doing a whole lot!
     
    Marc, Nov 7, 2008
    #8
  9. Marc

    Jeff Dillon Guest

    So what did you name it? You would refer to that name in your code
     
    Jeff Dillon, Nov 13, 2008
    #9
  10. Marc

    Marc Guest

    Ah.......so stupid me, I think....

    As far as I can see I have not named them in any moment yet. But I think the
    name would be: com.cdyne.ws. That one is automatically choosen for you. So I
    must use that name, com.cdyne.ws, where in the examples localhost is used? I
    just overlooked that EditBox. Well I am not really sure where and how to you
    that name, but that is the connection then!?

    FYI: this is what I did, step by step:

    I have used the service cowboy pointed out to me.
    I have selected Add Webreference in the Solution Explorer
    Inserted the URL from Cowboy:

    http://ws.cdyne.com/WeatherWS/Weather.asmx?wsdl

    In the "Add Web Reference Window" that pops up.
    Pressed "Go"
    The three supported Webservices are shown.
    At the right low there is an EditBox with the title "Web Reference Name".
    That's filled with com.cdyne.ws by default. But you can change the name (I
    see that now).
    Then I pressed 'Add Reference'.

    Now in the solution explorer an entry appears in App_WebReferences. A sort
    of folder tree with:

    com->cdyne->ws

    And in the latter ws folder there are two files:

    Weather.discomap
    Weather.wsdl


    Yours Sincerely grateful
    Marc Wentink
     
    Marc, Nov 14, 2008
    #10
  11. Marc

    Marc Guest

    "Marc" <> schreef

    And then calling the webservice is like this:

    protected void Button1_Click(object sender, EventArgs e)
    {
    com.cdyne.ws.Weather wt = new com.cdyne.ws.Weather();
    com.cdyne.ws.ForecastReturn FcR = wt.GetCityForecastByZIP("11202");
    Label1.Text = FcR.ResponseText;
    Label2.Text = "Tommorow Morning in NYC it's :
    "+FcR.ForecastResult[1].Temperatures.MorningLow;
    }
     
    Marc, Nov 14, 2008
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.