Skip to content

Commit

Permalink
Merge pull request esskar#130 from esskar/net50_support
Browse files Browse the repository at this point in the history
Net50 support
  • Loading branch information
esskar authored Dec 12, 2020
2 parents 7308b4d + 084b889 commit 65ab033
Show file tree
Hide file tree
Showing 44 changed files with 236 additions and 2,319 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ If you have a feature request, a bug or any other question, just create an [issu
For bugs: make sure you create a unit test, so it is easier for me to reproduce and fix it.

## Supported Platforms (or known to work with)
* .NET 4.0
* .NET 5.0
* .NET 4.5
* .Net Standard 1.3
* .NET 4.0
* .Net Standard 2.0
* .Net Standard 1.3
* UAP 10.0


[1]: http://nuget.org/packages/Serialize.Linq
[2]: https://github.com/esskar
[4]: https://github.com/esskar/Serialize.Linq/issues
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 1.0.{build}
configuration:
- Release
image: Visual Studio 2017
image: Visual Studio 2019
before_build:
- nuget restore src\Serialize.Linq.sln
build:
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Serialize.Linq\Serialize.Linq.csproj">
<Project>{94c52ad1-5d4a-4c9e-a1a3-68d3a36e6bf7}</Project>
<Project>{0004924b-c805-4d58-b448-9c71f45a62fa}</Project>
<Name>Serialize.Linq</Name>
</ProjectReference>
<ProjectReference Include="..\Serialize.Linq.Examples.RestContracts\Serialize.Linq.Examples.RestContracts.csproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Serialize.Linq.Examples.RestClient
{
class Program
internal class Program
{
private static readonly List<MediaTypeFormatter> _formatters;
private static readonly MediaTypeWithQualityHeaderValue _mediaTypeJson;
Expand All @@ -32,7 +32,7 @@ static Program()

_loggingHandler = new LoggingHandler(new HttpClientHandler());

_httpClient = new HttpClient(_loggingHandler) { BaseAddress = new Uri("http://localhost:51052/") };
_httpClient = new HttpClient(_loggingHandler) { BaseAddress = new Uri("http://localhost:60376/") };
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(_mediaTypeJson);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Serialize.Linq\Serialize.Linq.csproj">
<Project>{94c52ad1-5d4a-4c9e-a1a3-68d3a36e6bf7}</Project>
<Project>{0004924b-c805-4d58-b448-9c71f45a62fa}</Project>
<Name>Serialize.Linq</Name>
</ProjectReference>
<ProjectReference Include="..\Serialize.Linq.Examples.RestContracts\Serialize.Linq.Examples.RestContracts.csproj">
Expand Down
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;
}
}
}
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();
}
}
}
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>
Loading

0 comments on commit 65ab033

Please sign in to comment.