Skip to content

Commit 7b95a53

Browse files
authored
Add readonly to record structs (#1584)
1 parent 3add750 commit 7b95a53

File tree

7 files changed

+42
-18
lines changed

7 files changed

+42
-18
lines changed

src/Grpc.AspNetCore.Web/Grpc.AspNetCore.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<None Include="README.md" Pack="true" PackagePath="\" />
1919

2020
<Compile Include="..\Shared\CommonGrpcProtocolHelpers.cs" Link="Internal\CommonGrpcProtocolHelpers.cs" />
21+
<Compile Include="..\Shared\IsExternalInit.cs" Link="Internal\IsExternalInit.cs" />
2122
</ItemGroup>
2223

2324
<ItemGroup>

src/Grpc.AspNetCore.Web/Internal/GrpcWebMiddleware.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,20 @@ private static string ResolveContentType(string newContentType, string originalC
113113

114114
internal static ServerGrpcWebContext GetGrpcWebContext(HttpContext httpContext)
115115
{
116-
var serverContext = new ServerGrpcWebContext();
117-
118-
if (TryGetWebMode(httpContext.Request.ContentType, out var requestMode))
116+
if (!TryGetWebMode(httpContext.Request.ContentType, out var requestMode))
119117
{
120-
serverContext.Request = requestMode;
121-
122-
if (TryGetWebMode(httpContext.Request.Headers["Accept"], out var responseMode))
123-
{
124-
serverContext.Response = responseMode;
125-
}
126-
else
127-
{
128-
// If there isn't a request 'accept' header then default to mode to 'application/grpc`.
129-
serverContext.Response = ServerGrpcWebMode.GrpcWeb;
130-
}
118+
return default;
131119
}
132120

133-
return serverContext;
121+
if (TryGetWebMode(httpContext.Request.Headers["Accept"], out var responseMode))
122+
{
123+
return new ServerGrpcWebContext(requestMode, responseMode);
124+
}
125+
else
126+
{
127+
// If there isn't a request 'accept' header then default to mode to 'application/grpc`.
128+
return new ServerGrpcWebContext(requestMode, ServerGrpcWebMode.GrpcWeb);
129+
}
134130
}
135131

136132
private static bool TryGetWebMode(string? contentType, out ServerGrpcWebMode mode)

src/Grpc.AspNetCore.Web/Internal/ServerGrpcWebMode.cs

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

1919
namespace Grpc.AspNetCore.Web.Internal
2020
{
21-
internal record struct ServerGrpcWebContext(ServerGrpcWebMode Request, ServerGrpcWebMode Response);
21+
internal readonly record struct ServerGrpcWebContext(ServerGrpcWebMode Request, ServerGrpcWebMode Response);
2222

2323
internal enum ServerGrpcWebMode
2424
{

src/Grpc.Net.Client/Balancer/Internal/SocketConnectivitySubchannelTransport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace Grpc.Net.Client.Balancer.Internal
4949
/// </summary>
5050
internal class SocketConnectivitySubchannelTransport : ISubchannelTransport, IDisposable
5151
{
52-
internal record struct ActiveStream(BalancerAddress Address, Socket Socket, Stream? Stream);
52+
internal readonly record struct ActiveStream(BalancerAddress Address, Socket Socket, Stream? Stream);
5353

5454
private readonly ILogger _logger;
5555
private readonly Subchannel _subchannel;

src/Grpc.Net.ClientFactory/Grpc.Net.ClientFactory.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<ItemGroup>
1414
<None Include="README.md" Pack="true" PackagePath="\" />
1515

16+
<Compile Include="..\Shared\IsExternalInit.cs" Link="Internal\IsExternalInit.cs" />
1617
<Compile Include="..\Shared\HttpHandlerFactory.cs" Link="Internal\HttpHandlerFactory.cs" />
1718
<Compile Include="..\Shared\TelemetryHeaderHandler.cs" Link="Internal\TelemetryHeaderHandler.cs" />
1819
<Compile Include="..\Shared\CompatibilityHelpers.cs" Link="Internal\CompatibilityHelpers.cs" />

src/Grpc.Net.ClientFactory/Internal/GrpcCallInvokerFactory.cs

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

2727
namespace Grpc.Net.ClientFactory.Internal
2828
{
29-
internal record struct EntryKey(string Name, Type Type);
29+
internal readonly record struct EntryKey(string Name, Type Type);
3030

3131
internal class GrpcCallInvokerFactory
3232
{

src/Shared/IsExternalInit.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
namespace System.Runtime.CompilerServices
20+
{
21+
#if NETCOREAPP3_0 || NETSTANDARD2_0_OR_GREATER
22+
internal static class IsExternalInit
23+
{
24+
}
25+
#endif
26+
}

0 commit comments

Comments
 (0)