Skip to content

Commit 24f4241

Browse files
committed
Add comment about incorrect using HttpClient
1 parent 708bbcf commit 24f4241

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

src/CurlToCSharp.IntegrationTests/CurlToCSharp.IntegrationTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.9.0" />
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
1212
<PackageReference Include="xunit" Version="2.4.1" />
1313
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
1414
<PrivateAssets>all</PrivateAssets>

src/CurlToCSharp.UnitTests/CurlToCSharp.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
1111
<PackageReference Include="xunit" Version="2.4.1" />
1212
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
1313
<PrivateAssets>all</PrivateAssets>

src/CurlToCSharp/Constants/Chars.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace CurlToCSharp.Constants
1+
namespace CurlToCSharp.Constants
22
{
33
public static class Chars
44
{
@@ -9,5 +9,7 @@ public static class Chars
99
public const char SingleQuote = '\'';
1010

1111
public const char Space = ' ';
12+
13+
public const string NewLineString = "\n";
1214
}
1315
}

src/CurlToCSharp/Extensions/RoslynExtensions.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Linq;
33

4+
using CurlToCSharp.Constants;
5+
46
using Microsoft.CodeAnalysis;
57
using Microsoft.CodeAnalysis.CSharp;
68
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -9,8 +11,6 @@ namespace CurlToCSharp.Extensions
911
{
1012
public static class RoslynExtensions
1113
{
12-
private const string NewLineString = "\n";
13-
1414
public static InvocationExpressionSyntax CreateInvocationExpression(
1515
string leftPart,
1616
string rightPart,
@@ -132,12 +132,17 @@ public static AssignmentExpressionSyntax CreateMemberAssignmentExpression(
132132

133133
public static TSyntax PrependComment<TSyntax>(this TSyntax node, string comment) where TSyntax : SyntaxNode
134134
{
135-
return node.WithLeadingTrivia(SyntaxFactory.Comment(NewLineString + comment));
135+
return node.WithLeadingTrivia(SyntaxFactory.Comment(comment));
136136
}
137137

138138
public static TSyntax AppendWhiteSpace<TSyntax>(this TSyntax node) where TSyntax : SyntaxNode
139139
{
140-
return node.WithTrailingTrivia(SyntaxFactory.Comment(NewLineString));
140+
return node.WithTrailingTrivia(SyntaxFactory.Comment(Chars.NewLineString));
141+
}
142+
143+
public static TSyntax PrependWhiteSpace<TSyntax>(this TSyntax node) where TSyntax : SyntaxNode
144+
{
145+
return node.WithLeadingTrivia(SyntaxFactory.Comment(Chars.NewLineString));
141146
}
142147

143148
public static void TryAppendWhiteSpaceAtEnd<TSyntax>(this ICollection<TSyntax> statements) where TSyntax : SyntaxNode

src/CurlToCSharp/Services/ConverterService.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
55
using System.Net.Http;
66

7+
using CurlToCSharp.Constants;
78
using CurlToCSharp.Extensions;
89
using CurlToCSharp.Models;
910

@@ -506,10 +507,15 @@ private ExpressionStatementSyntax CreateTryAddHeaderStatement(ArgumentSyntax key
506507
/// </remarks>
507508
private UsingStatementSyntax CreateHttpClientUsing(CurlOptions curlOptions)
508509
{
510+
509511
var argumentSyntax = ShouldGenerateHandler(curlOptions)
510512
? new[] { SyntaxFactory.Argument(SyntaxFactory.IdentifierName(HandlerVariableName)) }
511513
: new ArgumentSyntax[0];
512-
return RoslynExtensions.CreateUsingStatement(HttpClientVariableName, nameof(HttpClient), argumentSyntax);
514+
var usingStatement = RoslynExtensions.CreateUsingStatement(HttpClientVariableName, nameof(HttpClient), argumentSyntax);
515+
516+
return usingStatement
517+
.PrependComment("// In production code, don't destroy the HttpClient through using, but better reuse an existing instance"
518+
+ Chars.NewLineString + "// https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/");
513519
}
514520

515521
/// <summary>
@@ -666,10 +672,10 @@ private IEnumerable<MemberDeclarationSyntax> ConfigureHandlerStatements(CurlOpti
666672
"AutomaticDecompression",
667673
SyntaxFactory.PrefixUnaryExpression(SyntaxKind.BitwiseNotExpression, RoslynExtensions.CreateMemberAccessExpression("DecompressionMethods", "None")));
668674

669-
memberAssignmentExpression = memberAssignmentExpression.PrependComment("// If you are using .NET Core 3.0+ you can replace `~DecompressionMethods.None` to `DecompressionMethods.All`");
675+
memberAssignmentExpression = memberAssignmentExpression
676+
.PrependComment(Chars.NewLineString + "// If you are using .NET Core 3.0+ you can replace `~DecompressionMethods.None` to `DecompressionMethods.All`");
670677

671-
statementSyntaxs.AddLast(
672-
SyntaxFactory.GlobalStatement(SyntaxFactory.ExpressionStatement(memberAssignmentExpression)));
678+
statementSyntaxs.AddLast(SyntaxFactory.GlobalStatement(SyntaxFactory.ExpressionStatement(memberAssignmentExpression)));
673679
}
674680

675681
if (curlOptions.HasCertificate && IsSupportedCertificate(curlOptions.CertificateType))

0 commit comments

Comments
 (0)