none
WCF + Consuming 3rd Party Web API? (REST/XML) RRS feed

  • Question

  • Hello,

    I'm trying to create a WCF contract that is able to consume XML from a weather feed of Yahoo's weather API.

    An example is: http://weather.yahooapis.com/forecastrss?z=24924 (not my zipcode)

    What I'm imagining is a simple WCF contract with one method called 'WeatherData GetData()', where it simply returns a DataContract (WeatherData), which maps its properties to the XML return from the webservice.

    I could honestly care less about this particular service. I'm really just trying to learn how to consume 3rd party APIs through my own WCF clients. If you have any examples of REST API's that I can look at, that'd be great too.

    Thanks!

    Edit:

    Here's what I have so far:

    client config:

    <configuration>
      <system.serviceModel>
        <client>
          <endpoint name="YahooWeather" address="http://weather.yahooapis.com/forecastrss?z=32765/" binding="webHttpBinding"
                     contract="RestServiceTest.Contracts.IRestService" behaviorConfiguration="WebBehavior"/>
        </client>
        <behaviors>
          <endpointBehaviors>
            <behavior name="WebBehavior">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>

    client consumer:

                using (ChannelFactory<IRestService> cf = new ChannelFactory<IRestService>("YahooWeather"))
                {
                    IRestService channel = cf.CreateChannel();
                    
                    var p1 = channel.GetData();
                    Console.WriteLine(p1);
                    Console.ReadLine();
                }

    contract:

        [ServiceContract]
        public interface IRestService
        {
            [OperationContract]
            [WebGet(UriTemplate = "/", ResponseFormat = WebMessageFormat.Xml)]
            string GetData();
        }

    When I run this I get an error stating:

    There was no endpoint listening at http://weather.yahooapis.com/forecastrss/?z=32765/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

    My contract is definitely not correct, and I have no DataContract to serialize the results to, however I feel like it should at least see that URL, and try to serialize it? Any ideas? Maybe something app.config related that I'm missing?



    • Edited by Holden15 Monday, March 25, 2013 2:12 PM
    Monday, March 25, 2013 6:58 AM

Answers

All replies

  • On 3/25/2013 2:58 AM, Holden15 wrote:

    And you know that the URL is a WCF Web service or a legacy Web service an ASMX Web service?

    http://weather.yahooapis.com/forecastrss?z=24924

    If it's a legacy Web service, than what you are doing is not going to work for you.  Because it doesn't involve WCF on the client and service sides, you would consume the Web service under the guidelines of consuming a Web service that is an ASMX legacy Web service.

    Monday, March 25, 2013 12:05 PM
  • I don't think that's true, darnold924.

    There are open source projects out there on the web right now (codeproject, etc) that can consume 3rd party APIs that are definitely not written in WCF (twitter API).

    http://petarvucetin.me/blog/2008/06/twitter-wcf-client/


    • Edited by Holden15 Monday, March 25, 2013 2:12 PM
    Monday, March 25, 2013 2:11 PM
  • On 3/25/2013 10:11 AM, Holden15 wrote:

    I don't think that's true, darnold924.

    There are open source projects out there on the web right now (codeproject, etc) that can consume 3rd party APIs that are definitely not written in WCF (twitter API).

    http://petarvucetin.me/blog/2008/06/twitter-wcf-client/

    I wouldn't bothered with this from a WCF standpoint trying to use a WCF client. I would simply make a classlib project that can consume that Web service API(s) and treat it as a legacy Web service, with project reference to project. That link above is more trouble than it's worth.

    Monday, March 25, 2013 3:58 PM
  • Well I'm asking in this thread for advice on setting up a consumer for a 3rd party XML API in this case (and/or REST API). I'm not asking for alternative solutions to the problem, because the problem is something I want to educate myself on.

    Monday, March 25, 2013 6:39 PM
  • Hi Holden15,

    I just check the yahoo service API endpoint you provided:

     http://weather.yahooapis.com/forecastrss?z=24924

    it is an RSS feed endpoint, and can be considered as a REST endpoint. Generally, it's recommended that we define strong-typed service Contract and client proxy for consuming SOAP XML based webservices (either built-in via WCF or other 3rd party framework). However, for REST service, it doesn't use SOAP, WSDL metadata, we don't have to (and personaly I don't recommend) use strong-typed WCF client proxy (with predefined serviceContract) to consume it.

    To consume REST service (return either XML or JSON format data), you can just go through the following steps:

    1. Get the sample request/response pairs of the target service so that you can get how you should generate request message and process response data.

    2. Use HttpWebRequest class to access the target service and retrieve the response data.


    #Make Yahoo! Web Service REST Calls With C#
    http://developer.yahoo.com/dotnet/howto-rest_cs.html

    #Using REST in C#
    http://rest.elkstein.org/2008/02/using-rest-in-c-sharp.html

    3. For XML data (like the RSS feed in your case), you can use .NET LINQ to XML classes to parse it:

    #LINQ to XML
    http://msdn.microsoft.com/en-us/library/bb387098.aspx

    #Querying XML in C# with LINQ to XML
    http://www.microsoft.com/uk/msdn/nuggets/nugget/204/querying-xml-in-c-with-linq-to-xml.aspx

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 1, 2013 6:02 AM
    Moderator