BTI420 notes – Fri Apr 7 and Mon Apr 10

Bonus coverage – load data from a web service. Work period – work on your assignment.

 

Code examples

WebServiceExample

LoadDataFromWebService

 

Bonus coverage – Load data from a web service

Some students have asked about loading data (e.g. JSON) from a web service. Learn how in this section.

 

Fetching JSON from a web service – design considerations

When designing a solution that will fetch JSON from a web service, you will realize that it is possible to perform the fetch by using JavaScript in the browser, or by using server-side code.

Which to use?

The answer is determined by the intended use of the JSON. If you want to create objects that will be used in the controller or manager, or saved in the data store, we recommend that you use server-side code. Otherwise, you can use JavaScript.

 

Fetch JSON using JavaScript

The Week 11 code example AjaxWithWebService implements this use case.

A new Week 12 code example LoadDataFromWebService also implements this use case, in its “ArtistsHTML5Page.html” page and linked script. Click the image to open it full-size in a new tab/window.

web-svc-call-artists-in-browser

 

Fetch JSON using C# (Manager class and controller classes)

As noted above, this use case is for situations where you need to work with the data in the controller or manager, or you need to save the data in the data store.

Open two code examples as you work through this section:

  • WebServiceExample
  • LoadDataFromWebService

The WebServiceExample publishes data from the Chinook music business database. Artists and employees. Start and run that app so that it is running on your computer’s per-instance on-demand web server.

Click the image to open it full-size in a new tab/window.

web-svc-home

 

Study the code. Its Manager class methods are similar to others you have written. Its Artists and Employees controllers are Web API controllers, which will return JSON (or XML) data.

The LoadDataFromWebService code example is a standard ASP.NET MVC web app, and has Manager class methods which call out to the web service. Every programming platform has an object that can make an HTTP request and handle its response. In your ASP.NET MVC apps, you can use the HttpClient class to do this work.

In the Manager, there is a custom “factory” method that creates an HttpClient object. Each data-fetching method uses the factory to create a custom object.

Notice also the use of the async-await pattern. When doing any task that has the potential of running for a long time, or not completing at all, you should use the async pattern. For this task, we do not know if – or when – a response will come back from the web service. When we use the async pattern, the task is done in the background, while the foreground task – whatever that is – remains running. There’s a simple recipe for using the async pattern, for most simple situations:

When calling an async method, prefix the method call with the await keyword.

When writing an async method,

  1. Wrap the method’s return type in a Task<T>
  2. Prefix this return type with the async keyword

Notice that the methods still return an object or collection (as the type in Task<T>) that’s based on a view model class. Yes, we still continue to use all that we have learned in the past.

Load the web app, and the list of artists. Data comes from the web service. Click the image to open it full-size in a new tab/window.

web-svc-call-artists

 

Click any artist to see its details. Data comes from the web service. Click the image to open it full-size in a new tab/window.

web-svc-call-artist-detail

 

Load the list of employees. Data comes from the web service. Click the image to open it full-size in a new tab/window.

web-svc-call-employees

 

Click any employee to see its details. Data comes from the web service. Click the image to open it full-size in a new tab/window.

web-svc-call-employee-detail

 

How to – enable a web app to call out to a web service

In the previous section, and in the LoadDataFromWebService code example, you learned how to call out to a web service from a web app. How can you implement this? Follow this sequence:

  1. Add a NuGet package
  2. Add (copy) the custom HttpClient “factory” method
  3. When needed, use the factory to create an instance of the customized HttpClient

 

Add a NuGet package – Use Solution Explorer to open the NuGet Package Manager. On the Browse tab, look for “webapi client”. Locate and install this package:

Microsoft.AspNet.WebApi.Client

This package adds support for formatting and content negotiation. On the response object’s Content property, a number of methods are added that help convert the incoming JSON into objects.

 

Add (copy) the custom HttpClient “factory” method – You have permission to copy the code for the custom HttpClient “factory” method from the code example, and paste it into your Manager class.

 

When needed, use the factory to create an instance of the customized HttpClient – Any time you must call out to a web service, use the pattern seen in the code example to do so.

 

 

Work on Assignment 11

Your professor will guide students as we get started with, or continue working on Assignment 11.

Before you leave the room at the end of the time slot, ensure that you give the Work Report to your professor, for the in-class grading part of the assignment.

 

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Advertisements
%d bloggers like this: