Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3603 #4163

Merged
merged 1 commit into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions docs/csharp/tutorials/console-webapiclient.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,31 @@ public static void Main(string[] args)
}
```

Now, you have a program that does nothing, but does it asynchronously. Let's go back to the
`ProcessRepositories` method and fill in a first version of it:
Now, you have a program that does nothing, but does it asynchronously. Let's improve it.

First you need an object that is capable to retrieve data from the web; you can use
a <xref:System.Net.Http.HttpClient> to do that. This object handles the request and the responses. Instantiate a single instance of that type in the `Program` class inside the Program.cs file.

```csharp
namespace WebAPIClient
{
class Program
{
private static readonly HttpClient client = new HttpClient();

static void Main(string[] args)
{
//...
}
}
}
```

Let's go back to the `ProcessRepositories` method and fill in a first version of it:

```csharp
private static async Task ProcessRepositories()
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
Expand All @@ -168,8 +186,7 @@ using System.Net.Http.Headers;
```

This first version makes a web request to read the list of all repositories under the dotnet
foundation organization. (The gitHub ID for the .NET Foundation is 'dotnet'). First, you create
a new <xref:System.Net.Http.HttpClient>. This object handles the request and the responses. The next few lines set up
foundation organization. (The gitHub ID for the .NET Foundation is 'dotnet'). The first few lines set up
the <xref:System.Net.Http.HttpClient> for this request. First, it is configured to accept the GitHub JSON responses.
This format is simply JSON. The next line adds a User Agent header to all requests from this
object. These two headers are checked by the GitHub server code, and are necessary to retrieve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace WebAPIClient
{
class Program
{
private static readonly HttpClient client = new HttpClient();

static void Main(string[] args)
{
var repositories = ProcessRepositories().Result;
Expand All @@ -27,18 +29,17 @@ static void Main(string[] args)

private static async Task<List<Repository>> ProcessRepositories()
{
var client = new HttpClient();
var serializer = new DataContractJsonSerializer(typeof(List<Repository>));

client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

var stringTask = client.GetStringAsync("https://api.github.com/orgs/dotnet/repos");
var streamTask = client.GetStreamAsync("https://api.github.com/orgs/dotnet/repos");
var streamTask = client.GetStreamAsync("https://api.github.com/orgs/dotnet/repos");
var repositories = serializer.ReadObject(await streamTask) as List<Repository>;
return repositories;
}
}
}
}