In-Depth

Consuming Remote Web Services with ASP.NET AJAX

Notice that ScriptManager can't generate a client-side proxy object that can be used to call the remote service? Dan shows you the workaround.

It's amazing how much data is accessible through Web services these days. Everything from weather, to stock quotes, to language translations are available for consumption. Take a moment to scan through the services shown on XMethods.com to see the wide variety of services that are out there.

If you want to consume remote Web services in your ASP.NET AJAX applications you'll quickly find that the ScriptManager can't generate a client-side proxy object that can be used to call the remote service. This isn't because the ScriptManager is lacking any functionality, it's because the XmlHttpRequest object built-into all major browsers only allows calls back to the same domain from which the page originated (and because ASP.NET AJAX requires JSON messages rather than SOAP messages in order to work cross-browser). This restriction is added for good reason in an attempt to circumvent various forms of cross-site scripting attacks. So what do you do if you need to consume a remote Web Service in your ASP.NET AJAX pages?

Although you can't call a remote Web service directly, you can have the page call a local service on your server which in turn calls the remote service to access data. Going this route slows things down a little since there's a middle-man involved, but it also allows JSON messages to be passed between your page and your service regardless of what remote Web services are called.

To call a remote Web service you'll first need to create a server-side service proxy object using the standard Add Web reference dialog in VS.NET 2005. Alternatively you could use the wsdl.exe command-line tool as well. Once the proxy object is generated, you need to add a new Web service file into your project that your ASP.NET AJAX pages can call. Since you'll be calling the service with ASP.NET AJAX you'll want to mark it with the ScriptService attribute. You'll find additional details about using the ScriptService attribute and other AJAX-related attributes such as ScriptMethod in my previous articles.

Once you've added a Web service into your Web site, add a WebMethod into it that invokes the proxy object and calls the remote Web Service to retrieve data. Your local service is simply acting as a wrapper around the remote service. Listing 1 shows an example of a service that retrieves weather from a remote Java service located at http://live.capeclear.com/ccx/GlobalWeather?wsdl. The local Web Service is decorated with the ASP.NET AJAX ScriptService attribute and acts as a wrapper around the remote GlobalWeather Web Service. This remote service shown in Listing 1 returns weather report information for different airport codes.

Now that you have a local Web Service, reference it using the ScriptManager's Services property as shown next so that a client-side proxy is automatically generated and embedded in the ASP.NET AJAX-enabled page:

<asp:ScriptManager 
  ID="ScriptManager1" 
  runat="server">
  <Services>
    <asp:ServiceReference 
      Path="~/WeatherService.asmx" 
    />
  </Services>
</asp:ScriptManager>

[Click on image for larger view.]

One the page loads the ScriptManager will automatically create a client-side proxy class that can be used to call the local Web service to retrieve weather information. An example of invoking the client-side proxy and writing out weather data to the page is shown in Listing 2. An example of the output that is generated is shown in Figure 1.

Creating local Web services to call remote Web services may seem like a lot of extra work at first glance. However, doing so ensures that the data returned from the remote Web service can be integrated nicely into ASP.NET AJAX applications through JSON messaging. You can find another example of utilizing this technique to display Amazon.com album information in a Silverlight application on my XML for ASP.NET Developers Web site.

About the Author

Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube