Skip to content

Commit 60f9e57

Browse files
RoblindeHenr1k80
andauthored
Contentful client tweaks (#351)
* Avoid waiting for complete response, if we are just checking if for success status code. Avoid evaluating OS & Version for every request. Avoid a possible MemoryStream leak. Other minor tweaks * Dispose responses & requests, pass cancellationToken & avoid an array allocation * Dispose cloned request, add sealed & readonly * Avoid deserializing into a string by deserializing directly from the response stream. Then deserialization can start immediately instead of waiting for the entire response. Reuse JsonSerializerSettings. Avoid async Task methods where it adds no value. * Use HTTP/2 * Allow HTTP response connection * Update package and dependency --------- Co-authored-by: Henrik Gedionsen <hg@impact.dk>
1 parent b3ec71c commit 60f9e57

11 files changed

+393
-632
lines changed

Contentful.AspNetCore/Contentful.AspNetCore.csproj

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Description>Official .NET SDK for the Contentful Content Delivery and Management API for ASP.NET core.</Description>
44
<PackageId>contentful.aspnetcore</PackageId>
55
<NeutralLanguage>en-US</NeutralLanguage>
6-
<VersionPrefix>8.1.0</VersionPrefix>
6+
<VersionPrefix>8.2.0</VersionPrefix>
77
<TargetFramework>netstandard2.0</TargetFramework>
88
<Authors>Contentful</Authors>
99
<Copyright>Contentful GmbH.</Copyright>
@@ -13,10 +13,10 @@
1313
<PackageProjectUrl>https://github.com/contentful/contentful.net</PackageProjectUrl>
1414
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1515
<RepositoryType>git</RepositoryType>
16-
<Version>8.1.0</Version>
17-
<AssemblyVersion>8.1.0.0</AssemblyVersion>
16+
<Version>8.2.0</Version>
17+
<AssemblyVersion>8.2.0.0</AssemblyVersion>
1818
<RepositoryUrl>https://github.com/contentful/contentful.net</RepositoryUrl>
19-
<FileVersion>8.1.0.0</FileVersion>
19+
<FileVersion>8.2.0.0</FileVersion>
2020
</PropertyGroup>
2121
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
2222
<DocumentationFile>bin\Release\netstandard1.5\Contentful.AspNetCore.xml</DocumentationFile>
@@ -25,7 +25,7 @@
2525
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
2626
</PropertyGroup>
2727
<ItemGroup>
28-
<PackageReference Include="contentful.csharp" Version="8.1.0" />
28+
<PackageReference Include="contentful.csharp" Version="8.2.0" />
2929
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
3030
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
3131
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.2.0" />

Contentful.AspNetCore/IServiceCollectionExtensions.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Net;
42
using Microsoft.Extensions.DependencyInjection;
53
using Microsoft.Extensions.DependencyInjection.Extensions;
64
using Contentful.Core.Configuration;
75
using Microsoft.Extensions.Configuration;
86
using System.Net.Http;
97
using Contentful.Core;
108
using Microsoft.Extensions.Options;
11-
using Microsoft.Extensions.Http;
129
using Contentful.Core.Models;
1310

1411
namespace Contentful.AspNetCore
@@ -31,7 +28,16 @@ public static IServiceCollection AddContentful(this IServiceCollection services,
3128
services.AddOptions();
3229
services.Configure<ContentfulOptions>(configuration.GetSection("ContentfulOptions"));
3330
services.TryAddSingleton<HttpClient>();
34-
services.AddHttpClient(HttpClientName);
31+
services.AddHttpClient(HttpClientName).ConfigurePrimaryHttpMessageHandler(sp =>
32+
{
33+
var options = sp.GetService<IOptions<ContentfulOptions>>().Value;
34+
var handler = new HttpClientHandler();
35+
if (options.AllowHttpResponseCompression && handler.SupportsAutomaticDecompression)
36+
{
37+
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
38+
}
39+
return handler;
40+
});
3541
services.TryAddTransient<IContentfulClient>((sp) => {
3642
var options = sp.GetService<IOptions<ContentfulOptions>>().Value;
3743
var factory = sp.GetService<IHttpClientFactory>();

Contentful.Core/Configuration/ContentfulOptions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,11 @@ public class ContentfulOptions
5656
/// Default is "https://cdn.contentful.com/spaces/".
5757
/// </summary>
5858
public string BaseUrl { get; set; } = "https://cdn.contentful.com/spaces/";
59+
60+
/// <summary>
61+
/// Whether to use HTTP compression for responses. Only disable if you are hosting in the same datacenter as contentful
62+
/// Default is true
63+
/// </summary>
64+
public bool AllowHttpResponseCompression { get; set; } = true;
5965
}
6066
}

Contentful.Core/Configuration/ExtensionJsonConverter.cs

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
using Newtonsoft.Json.Linq;
55
using Newtonsoft.Json.Serialization;
66
using System;
7-
using System.Collections.Generic;
87
using System.Linq;
9-
using System.Text;
108

119
namespace Contentful.Core.Configuration
1210
{
@@ -15,6 +13,17 @@ namespace Contentful.Core.Configuration
1513
/// </summary>
1614
public class ExtensionJsonConverter : JsonConverter
1715
{
16+
private static readonly JsonSerializerSettings JsonSerializerSettings = new()
17+
{
18+
ContractResolver = new CamelCasePropertyNamesContractResolver
19+
{
20+
NamingStrategy =
21+
{
22+
OverrideSpecifiedNames = false
23+
}
24+
},
25+
};
26+
1827
/// <summary>
1928
/// Determines whether this instance can convert the specified object type.
2029
/// </summary>
@@ -75,15 +84,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
7584
extension
7685
};
7786

78-
var resolver = new CamelCasePropertyNamesContractResolver();
79-
resolver.NamingStrategy.OverrideSpecifiedNames = false;
80-
81-
var settings = new JsonSerializerSettings
82-
{
83-
ContractResolver = resolver,
84-
};
85-
86-
var jObject = JObject.FromObject(extensionStructure, JsonSerializer.Create(settings));
87+
var jObject = JObject.FromObject(extensionStructure, JsonSerializer.Create(JsonSerializerSettings));
8788

8889
serializer.Serialize(writer, jObject);
8990
}

Contentful.Core/Contentful.Core.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<PackageId>contentful.csharp</PackageId>
55
<AssemblyTitle>contentful.net</AssemblyTitle>
66
<NeutralLanguage>en-US</NeutralLanguage>
7-
<VersionPrefix>8.1.0</VersionPrefix>
7+
<VersionPrefix>8.2.0</VersionPrefix>
88
<TargetFramework>netstandard2.0</TargetFramework>
99
<Authors>Contentful</Authors>
1010
<Copyright>Contentful GmbH.</Copyright>

0 commit comments

Comments
 (0)