The purpose of the NewsService class is to wrap the HTTP calls to the news REST API and make them easily accessible to our code in the form of regular .NET method calls. It also acts as an abstraction, making it easier to replace the source of the news if you would like to do so.
Creating the NewsService class is quite straightforward. Follow these steps:
- In the News project, install the Newtonsoft.Json NuGet package in the same way that we added the Autofac package earlier.
- In the Services folder, create a new class called NewsService.
- Edit the class so that it looks like the following code:
using System;
using System.Net;
using System.Threading.Tasks;
using News.Models;
using Newtonsoft.Json;
namespace News.Services
{
public class NewsService
{
public async Task<NewsResult> GetNews(NewsScope scope)
{
string url = GetUrl(scope);
var webclient = new WebClient();
var json = await
...