forked from esskar/Serialize.Linq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request esskar#130 from esskar/net50_support
Net50 support
- Loading branch information
Showing
44 changed files
with
236 additions
and
2,319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
...Serialize.Linq.Examples/Serialize.Linq.Examples.NetCoreApp1.1/NetCoreAppAssemblyLoader.cs
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
src/Serialize.Linq.Examples/Serialize.Linq.Examples.NetCoreApp1.1/Program.cs
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
...amples/Serialize.Linq.Examples.NetCoreApp1.1/Serialize.Linq.Examples.NetCoreApp1.1.csproj
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
src/Serialize.Linq.Examples/Serialize.Linq.Examples.NetCoreApp2/Program.cs
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
...q.Examples/Serialize.Linq.Examples.NetCoreApp2/Serialize.Linq.Examples.NetCoreApp2.csproj
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Serialize.Linq.Examples/Serialize.Linq.Examples.RestClientNet50/LoggingHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Serialize.Linq.Examples.RestClientNet50 | ||
{ | ||
public class LoggingHandler : DelegatingHandler | ||
{ | ||
public LoggingHandler(HttpMessageHandler innerHandler) | ||
: base(innerHandler) | ||
{ | ||
} | ||
|
||
public bool IsLoggingEnabled { get; set; } | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
if (IsLoggingEnabled) | ||
{ | ||
Console.WriteLine("Request:"); | ||
Console.WriteLine(request.ToString()); | ||
if (request.Content != null) | ||
{ | ||
Console.WriteLine(await request.Content.ReadAsStringAsync(cancellationToken)); | ||
} | ||
|
||
Console.WriteLine(); | ||
} | ||
|
||
var response = await base.SendAsync(request, cancellationToken); | ||
|
||
if (IsLoggingEnabled) | ||
{ | ||
Console.WriteLine("Response:"); | ||
Console.WriteLine(response.ToString()); | ||
Console.WriteLine(await response.Content.ReadAsStringAsync(cancellationToken)); | ||
|
||
Console.WriteLine(); | ||
} | ||
|
||
return response; | ||
} | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
src/Serialize.Linq.Examples/Serialize.Linq.Examples.RestClientNet50/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using System.Net.Http; | ||
using System.Net.Http.Formatting; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using Serialize.Linq.Examples.RestContracts.Entities; | ||
using Serialize.Linq.Extensions; | ||
|
||
namespace Serialize.Linq.Examples.RestClientNet50 | ||
{ | ||
internal class Program | ||
{ | ||
private static readonly List<MediaTypeFormatter> _formatters; | ||
private static readonly MediaTypeWithQualityHeaderValue _mediaTypeJson; | ||
private static readonly LoggingHandler _loggingHandler; | ||
private static readonly HttpClient _httpClient; | ||
|
||
static Program() | ||
{ | ||
_formatters = new List<MediaTypeFormatter> { new JsonMediaTypeFormatter | ||
{ | ||
SerializerSettings = new JsonSerializerSettings | ||
{ | ||
TypeNameHandling = TypeNameHandling.Objects | ||
} | ||
}}; | ||
_mediaTypeJson = new MediaTypeWithQualityHeaderValue("application/json"); | ||
|
||
_loggingHandler = new LoggingHandler(new HttpClientHandler()); | ||
|
||
_httpClient = new HttpClient(_loggingHandler) { BaseAddress = new Uri("http://localhost:60376/") }; | ||
_httpClient.DefaultRequestHeaders.Accept.Clear(); | ||
_httpClient.DefaultRequestHeaders.Accept.Add(_mediaTypeJson); | ||
|
||
} | ||
|
||
private static void Main() | ||
{ | ||
RunAsync().Wait(); | ||
} | ||
|
||
private static async Task RunAsync() | ||
{ | ||
var cancellationToken = CancellationToken.None; | ||
|
||
_loggingHandler.IsLoggingEnabled = true; | ||
try | ||
{ | ||
await RunAllPersonsAsync(cancellationToken); | ||
await RunAllPersonsFromJapan(cancellationToken); | ||
await RunAllPersonsOfAge100(cancellationToken); | ||
await RunAllMalePersons(cancellationToken); | ||
await RunAllLivingPersons(cancellationToken); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e.Message); | ||
} | ||
|
||
} | ||
|
||
private static async Task RunAllPersonsAsync(CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine("All persons"); | ||
|
||
var persons = await GetAllPersons(cancellationToken); | ||
ShowPersons(persons); | ||
} | ||
|
||
private static async Task RunAllPersonsFromJapan(CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine("All persons from Japan"); | ||
Expression<Func<Person, bool>> expression = p => p.Residence == "Japan"; | ||
|
||
var persons = await QueryPersons(expression, cancellationToken); | ||
ShowPersons(persons); | ||
} | ||
|
||
private static async Task RunAllPersonsOfAge100(CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine("All persons of Age >= 100"); | ||
Expression<Func<Person, bool>> expression = p => p.Age >= 100; | ||
|
||
var persons = await QueryPersons(expression, cancellationToken); | ||
ShowPersons(persons); | ||
} | ||
|
||
private static async Task RunAllMalePersons(CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine("All male persons"); | ||
Expression<Func<Person, bool>> expression = p => p.Gender == Gender.Male; | ||
|
||
var persons = await QueryPersons(expression, cancellationToken); | ||
ShowPersons(persons); | ||
} | ||
|
||
private static async Task RunAllLivingPersons(CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine("All living persons"); | ||
Expression<Func<Person, bool>> expression = p => p.DeathDate == null; | ||
|
||
var persons = await QueryPersons(expression, cancellationToken); | ||
ShowPersons(persons); | ||
} | ||
|
||
private static async Task<IEnumerable<Person>> GetAllPersons(CancellationToken cancellationToken) | ||
{ | ||
var response = await _httpClient.GetAsync("api/Person", cancellationToken); | ||
response.EnsureSuccessStatusCode(); | ||
return await response.Content.ReadAsAsync<IEnumerable<Person>>(_formatters); | ||
} | ||
|
||
private static async Task<IEnumerable<Person>> QueryPersons(Expression<Func<Person, bool>> query, CancellationToken cancellationToken) | ||
{ | ||
var queryNode = query.ToExpressionNode(); | ||
var response = await _httpClient.PostAsync("api/Person", queryNode, _formatters[0], _mediaTypeJson, cancellationToken); | ||
response.EnsureSuccessStatusCode(); | ||
return await response.Content.ReadAsAsync<IEnumerable<Person>>(_formatters); | ||
} | ||
|
||
private static void ShowPersons(IEnumerable<Person> persons) | ||
{ | ||
foreach (var person in persons) | ||
Console.WriteLine("{0}) {1} {2}, {5}, age {3}, form {4}", person.Id, person.FirstName, person.LastName, person.Age, person.Residence, person.Gender); | ||
Console.WriteLine(); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...es/Serialize.Linq.Examples.RestClientNet50/Serialize.Linq.Examples.RestClientNet50.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Serialize.Linq\Serialize.Linq.csproj" /> | ||
<ProjectReference Include="..\Serialize.Linq.Examples.RestContracts\Serialize.Linq.Examples.RestContracts.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.