11.8. Binding to External Services

Of course, when you are binding to data, you usually want to retrieve it from an external source. The DataView control allows you to easily accomplish this and bind to many different types of services, for example:

  • ASMX web service

  • WCF web services

  • WCF Data Services

  • ASP.NET MVC controller actions

  • JSONP service

  • Basically anything that returns JSON-formatted data

You will now use client templates to bind to ASMX and WCF web services.

11.8.1. Binding to a Web Service (.asmx)

First bind to the web service:

  1. Add a new class called Person to the project, and enter the following class definition:

    public class Person
    {
       public string Name { get; set; }
       public string Age { get; set; }
    }

  2. Add a new .asmx web service to the project called GetData.asmx.

  3. Add the following using directive to GetData.asmx.cs:

    using System.Web.Script.Services;

  4. Uncomment the following attribute in the GetData class:

    [System.Web.Script.Services.ScriptService]

  5. Now create a new method to return a list of people. Note the call to System.Threading.Sleep();. This is to slow down the service so you can see the results appear (you might want to remove the sys-template style to see the effect that latency can have):

    [WebMethod]
            [System.Web.Script.Services.ScriptMethod]
            public List<Person> GetPeople()
            {
                System.Threading.Thread.Sleep(2000);
    
                List<Person> People = new List<Person>();
    
                People.Add(new Person { Name = "Alex Mackey", Age = "28" });
                People.Add(new Person { Name = "Matt Lacey", Age = "31" });
                People.Add(new Person { Name = "Mark Clugston", Age = "28" });

    People.Add(new Person { Name = "Craig Murphy", Age = "33" });
                People.Add(new Person { Name = "Chris Hay", Age = "32" });
                People.Add(new Person { Name = "Andy Gibson", Age = "21" });
    
                return People;
            }

  6. Copy the file dataviewProgrammatic.htm, and rename the new copy to dataviewAsmx.htm.

  7. Replace the existing <script> block with the following code:

    <script type="text/javascript">
    
    Sys.require([Sys.components.dataView, Sys.scripts.WebServices], function () {
    
        Sys.create.dataView('#peopleView',
        {
            dataProvider: "GetData.asmx",
            fetchOperation: "GetPeople",
            autoFetch: "true"
        });
    
    });
    </script>

  8. Right-click dataviewAsmx.htm, and select View in Browser. After about two seconds, you should see the results of the web service bound to the template.

11.8.2. Binding to a WCF Service

Binding to a WCF service is accomplished in a very similar way. In this example, you will pass in a name parameter as well as retrieve a single person:

  1. Add a new Ajax-enabled WCF service (similar to a standard WCF service but with some additional attributes to enable JavaScript methods calls) to the project called GetDataFromWCF.svc.

  2. Now add the following method to the new web service:

    [OperationContract]
    public Person GetPerson(string Name)
    {
       //System.Threading.Thread.Sleep(2000);
    
       List<Person> People = new List<Person>();
    
       People.Add(new Person { Name = "Alex Mackey", Age = "28" });
       People.Add(new Person { Name = "Matt Lacey", Age = "31" });
       People.Add(new Person { Name = "Barry Dorrans", Age = "78" });
       People.Add(new Person { Name = "Craig Murphy", Age = "33" });
       People.Add(new Person { Name = "Chris Hay", Age = "32" });
       People.Add(new Person { Name = "Andy Gibson", Age = "21" });
    
       return People.FirstOrDefault(p => p.Name == Name);
    }

  3. Replace the current <script> block in webserviceWCF.htm with the following code:

    <script type="text/javascript">
    
    //We need Sys.scripts.WebServices in order to interact with the WCF service
    Sys.require([Sys.components.dataView, Sys.scripts.WebServices], function () {
    
        Sys.create.dataView('#peopleView',
        {
            dataProvider: "GetDataFromWCF.svc",
            fetchOperation: "GetPerson",
            fetchParameters: { Name: "Alex Mackey" },
            autoFetch: "true"
        });
    
    });
    </script>

11.8.3. JSONP

The latest release of the AJAX libraries contains support for working with JSONP services. JSONP (or JSON with padding) is a bit of a hack that allows you to make Ajax calls to external domains (not allowed with standard XHTTP requests and utilized by Flickr and Bing). JSONP works by exploiting that you can reference a script file on a different server. This script then calls a function in the calling page to return the data.

DataView contains support for binding to JSONP services such as those offered by Flickr and Bing. The Microsoft AJAX site has an example of how to call a JSONP service at www.asp.net/ajaxlibrary/HOW%20TO%20Use%20JSONP%20to%20Request%20Data%20from%20Remote%20Websites.ashx.

For more information on JSONP, please refer to the following links:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.178.169