├── sources
├── EfficientApiCalls.Api
│ ├── appsettings.Development.json
│ ├── appsettings.json
│ ├── EfficientApiCalls.Api.csproj
│ ├── Program.cs
│ ├── Controllers
│ │ └── ValuesController.cs
│ ├── Properties
│ │ └── launchSettings.json
│ ├── Startup.cs
│ ├── Model.cs
│ └── E1IlXtbj4.json
├── EfficientApiCalls
│ ├── EfficientApiCalls.csproj
│ ├── Model.cs
│ ├── BenchmarkHelper.cs
│ └── Program.cs
└── EfficientApiCalls.sln
├── README.md
├── .gitignore
└── LICENSE
/sources/EfficientApiCalls.Api/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "IncludeScopes": false,
4 | "LogLevel": {
5 | "Default": "Debug",
6 | "System": "Information",
7 | "Microsoft": "Information"
8 | }
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # How to make efficient api calls with C# and JSON.NET
2 |
3 | Source code illustrating how to make efficient HttpClient api calls in C#
4 |
5 | You can find more information about this benchmark here :
6 | [https://johnthiriet.com/efficient-api-calls/](https://johnthiriet.com/efficient-api-calls/)
7 |
--------------------------------------------------------------------------------
/sources/EfficientApiCalls.Api/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "IncludeScopes": false,
4 | "Debug": {
5 | "LogLevel": {
6 | "Default": "Warning"
7 | }
8 | },
9 | "Console": {
10 | "LogLevel": {
11 | "Default": "Warning"
12 | }
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/sources/EfficientApiCalls/EfficientApiCalls.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/sources/EfficientApiCalls.Api/EfficientApiCalls.Api.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.0
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Autosave files
2 | *~
3 |
4 | # build
5 | [Oo]bj/
6 | [Bb]in/
7 | packages/
8 | TestResults/
9 |
10 | # globs
11 | Makefile.in
12 | *.DS_Store
13 | *.sln.cache
14 | *.suo
15 | *.cache
16 | *.pidb
17 | *.userprefs
18 | *.usertasks
19 | config.log
20 | config.make
21 | config.status
22 | aclocal.m4
23 | install-sh
24 | autom4te.cache/
25 | *.user
26 | *.tar.gz
27 | tarballs/
28 | test-results/
29 | Thumbs.db
30 |
31 | # Mac bundle stuff
32 | *.dmg
33 | *.app
34 |
35 | # resharper
36 | *_Resharper.*
37 | *.Resharper
38 |
39 | # dotCover
40 | *.dotCover
41 |
42 | *.db
43 |
--------------------------------------------------------------------------------
/sources/EfficientApiCalls.Api/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 | using Microsoft.AspNetCore;
7 | using Microsoft.AspNetCore.Hosting;
8 | using Microsoft.Extensions.Configuration;
9 | using Microsoft.Extensions.Logging;
10 |
11 | namespace EfficientApiCalls.Api
12 | {
13 | public class Program
14 | {
15 | public static void Main(string[] args)
16 | {
17 | BuildWebHost(args).Run();
18 | }
19 |
20 | public static IWebHost BuildWebHost(string[] args) =>
21 | WebHost.CreateDefaultBuilder(args)
22 | .UseStartup()
23 | .Build();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/sources/EfficientApiCalls.Api/Controllers/ValuesController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Mvc;
6 |
7 | namespace EfficientApiCalls.Api.Controllers
8 | {
9 | [Route("api/[controller]")]
10 | public class ValuesController : Controller
11 | {
12 | // GET api/values
13 | public FileStreamResult Get()
14 | {
15 | var fileStream = System.IO.File.Open("E1IlXtbj4.json", System.IO.FileMode.Open);
16 | return File(fileStream, "application/json");
17 | }
18 |
19 | // POST api/values
20 | [HttpPost]
21 | public void Post([FromBody]Model value)
22 | {
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/sources/EfficientApiCalls.Api/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "iisSettings": {
3 | "windowsAuthentication": false,
4 | "anonymousAuthentication": true,
5 | "iisExpress": {
6 | "applicationUrl": "http://localhost:55556/",
7 | "sslPort": 0
8 | }
9 | },
10 | "profiles": {
11 | "IIS Express": {
12 | "commandName": "IISExpress",
13 | "launchBrowser": true,
14 | "launchUrl": "api/values",
15 | "environmentVariables": {
16 | "ASPNETCORE_ENVIRONMENT": "Development"
17 | }
18 | },
19 | "EfficientApiCalls.Api": {
20 | "commandName": "Project",
21 | "launchBrowser": true,
22 | "launchUrl": "api/values",
23 | "environmentVariables": {
24 | "ASPNETCORE_ENVIRONMENT": "Development"
25 | },
26 | "applicationUrl": "http://localhost:5000/"
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 John Thiriet
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/sources/EfficientApiCalls.Api/Startup.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Builder;
6 | using Microsoft.AspNetCore.Hosting;
7 | using Microsoft.Extensions.Configuration;
8 | using Microsoft.Extensions.DependencyInjection;
9 | using Microsoft.Extensions.Logging;
10 | using Microsoft.Extensions.Options;
11 |
12 | namespace EfficientApiCalls.Api
13 | {
14 | public class Startup
15 | {
16 | public Startup(IConfiguration configuration)
17 | {
18 | Configuration = configuration;
19 | }
20 |
21 | public IConfiguration Configuration { get; }
22 |
23 | // This method gets called by the runtime. Use this method to add services to the container.
24 | public void ConfigureServices(IServiceCollection services)
25 | {
26 | services.AddMvc();
27 | }
28 |
29 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
30 | public void Configure(IApplicationBuilder app, IHostingEnvironment env)
31 | {
32 | if (env.IsDevelopment())
33 | {
34 | app.UseDeveloperExceptionPage();
35 | }
36 |
37 | app.UseMvc();
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/sources/EfficientApiCalls.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2012
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EfficientApiCalls", "EfficientApiCalls\EfficientApiCalls.csproj", "{630EAB9D-4AD1-479B-9D7E-F837521C1652}"
5 | EndProject
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EfficientApiCalls.Api", "EfficientApiCalls.Api\EfficientApiCalls.Api.csproj", "{BD01DD35-A197-4034-8D73-C296EE934225}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {630EAB9D-4AD1-479B-9D7E-F837521C1652}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {630EAB9D-4AD1-479B-9D7E-F837521C1652}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {630EAB9D-4AD1-479B-9D7E-F837521C1652}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {630EAB9D-4AD1-479B-9D7E-F837521C1652}.Release|Any CPU.Build.0 = Release|Any CPU
18 | {BD01DD35-A197-4034-8D73-C296EE934225}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19 | {BD01DD35-A197-4034-8D73-C296EE934225}.Debug|Any CPU.Build.0 = Debug|Any CPU
20 | {BD01DD35-A197-4034-8D73-C296EE934225}.Release|Any CPU.ActiveCfg = Release|Any CPU
21 | {BD01DD35-A197-4034-8D73-C296EE934225}.Release|Any CPU.Build.0 = Release|Any CPU
22 | EndGlobalSection
23 | EndGlobal
24 |
--------------------------------------------------------------------------------
/sources/EfficientApiCalls/Model.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace EfficientApiCalls
4 | {
5 | public class Name
6 | {
7 | public string first { get; set; }
8 | public string last { get; set; }
9 | }
10 |
11 | public class Friend
12 | {
13 | public int id { get; set; }
14 | public string name { get; set; }
15 | }
16 |
17 | public class Model
18 | {
19 | public string _id { get; set; }
20 | public int index { get; set; }
21 | public string guid { get; set; }
22 | public bool isActive { get; set; }
23 | public string balance { get; set; }
24 | public string picture { get; set; }
25 | public int age { get; set; }
26 | public string eyeColor { get; set; }
27 | public Name name { get; set; }
28 | public string company { get; set; }
29 | public string email { get; set; }
30 | public string phone { get; set; }
31 | public string address { get; set; }
32 | public string about { get; set; }
33 | public string registered { get; set; }
34 | public string latitude { get; set; }
35 | public string longitude { get; set; }
36 | public List tags { get; set; }
37 | public List range { get; set; }
38 | public List friends { get; set; }
39 | public string greeting { get; set; }
40 | public string favoriteFruit { get; set; }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/sources/EfficientApiCalls.Api/Model.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace EfficientApiCalls
4 | {
5 | public class Name
6 | {
7 | public string first { get; set; }
8 | public string last { get; set; }
9 | }
10 |
11 | public class Friend
12 | {
13 | public int id { get; set; }
14 | public string name { get; set; }
15 | }
16 |
17 | public class Model
18 | {
19 | public string _id { get; set; }
20 | public int index { get; set; }
21 | public string guid { get; set; }
22 | public bool isActive { get; set; }
23 | public string balance { get; set; }
24 | public string picture { get; set; }
25 | public int age { get; set; }
26 | public string eyeColor { get; set; }
27 | public Name name { get; set; }
28 | public string company { get; set; }
29 | public string email { get; set; }
30 | public string phone { get; set; }
31 | public string address { get; set; }
32 | public string about { get; set; }
33 | public string registered { get; set; }
34 | public string latitude { get; set; }
35 | public string longitude { get; set; }
36 | public List tags { get; set; }
37 | public List range { get; set; }
38 | public List friends { get; set; }
39 | public string greeting { get; set; }
40 | public string favoriteFruit { get; set; }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/sources/EfficientApiCalls/BenchmarkHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.Threading.Tasks;
4 | using System.Linq;
5 | using System.Threading;
6 |
7 | namespace EfficientApiCalls
8 | {
9 | public static class BenchmarkHelper
10 | {
11 | public static async Task BenchAsync(Func