diff --git a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisHubLifetimeManagerBenchmark.cs b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisHubLifetimeManagerBenchmark.cs index 40426e995d..cb21cce57d 100644 --- a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisHubLifetimeManagerBenchmark.cs +++ b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisHubLifetimeManagerBenchmark.cs @@ -22,10 +22,10 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks private RedisHubLifetimeManager _manager2; private TestClient[] _clients; private object[] _args; - private List _excludedIds = new List(); - private List _sendIds = new List(); - private List _groups = new List(); - private List _users = new List(); + private readonly List _excludedIds = new List(); + private readonly List _sendIds = new List(); + private readonly List _groups = new List(); + private readonly List _users = new List(); private const int ClientCount = 20; diff --git a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisProtocolBenchmark.cs b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisProtocolBenchmark.cs index c856613edb..5eec1d66bf 100644 --- a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisProtocolBenchmark.cs +++ b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/RedisProtocolBenchmark.cs @@ -119,7 +119,7 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks private class DummyProtocol: IHubProtocol { - private static byte[] _fixedOutput = new byte[] { 0x68, 0x68, 0x6C, 0x6C, 0x6F }; + private static readonly byte[] _fixedOutput = new byte[] { 0x68, 0x68, 0x6C, 0x6C, 0x6F }; public string Name { get; } diff --git a/samples/ClientSample/Tcp/TcpConnection.cs b/samples/ClientSample/Tcp/TcpConnection.cs index fffeef17e6..d2656c1f80 100644 --- a/samples/ClientSample/Tcp/TcpConnection.cs +++ b/samples/ClientSample/Tcp/TcpConnection.cs @@ -16,8 +16,8 @@ namespace ClientSample private volatile bool _aborted; private readonly EndPoint _endPoint; private IDuplexPipe _application; - private SocketSender _sender; - private SocketReceiver _receiver; + private readonly SocketSender _sender; + private readonly SocketReceiver _receiver; public TcpConnection(EndPoint endPoint) { diff --git a/samples/SocialWeather/FormatterResolver.cs b/samples/SocialWeather/FormatterResolver.cs index 08cc1f6860..2a8912aa8f 100644 --- a/samples/SocialWeather/FormatterResolver.cs +++ b/samples/SocialWeather/FormatterResolver.cs @@ -9,9 +9,9 @@ namespace SocialWeather { public class FormatterResolver { - private IServiceProvider _serviceProvider; + private readonly IServiceProvider _serviceProvider; - private Dictionary> _formatters + private readonly Dictionary> _formatters = new Dictionary>(); public FormatterResolver(IServiceProvider serviceProvider) diff --git a/samples/SocialWeather/Json/JSonStreamFormatter.cs b/samples/SocialWeather/Json/JSonStreamFormatter.cs index dbd590eca7..c1e8f8f5d6 100644 --- a/samples/SocialWeather/Json/JSonStreamFormatter.cs +++ b/samples/SocialWeather/Json/JSonStreamFormatter.cs @@ -9,7 +9,7 @@ namespace SocialWeather.Json { public class JsonStreamFormatter : IStreamFormatter { - private JsonSerializer _serializer = new JsonSerializer(); + private readonly JsonSerializer _serializer = new JsonSerializer(); public async Task ReadAsync(Stream stream) { diff --git a/src/Microsoft.AspNetCore.Http.Connections.Client/Internal/ServerSentEventsMessageParser.cs b/src/Microsoft.AspNetCore.Http.Connections.Client/Internal/ServerSentEventsMessageParser.cs index 64fbba5914..ebc603c7d4 100644 --- a/src/Microsoft.AspNetCore.Http.Connections.Client/Internal/ServerSentEventsMessageParser.cs +++ b/src/Microsoft.AspNetCore.Http.Connections.Client/Internal/ServerSentEventsMessageParser.cs @@ -15,12 +15,12 @@ namespace Microsoft.AspNetCore.Http.Connections.Client.Internal private const byte ByteLF = (byte)'\n'; private const byte ByteColon = (byte)':'; - private static byte[] _dataPrefix = Encoding.UTF8.GetBytes("data: "); - private static byte[] _sseLineEnding = Encoding.UTF8.GetBytes("\r\n"); - private static byte[] _newLine = Encoding.UTF8.GetBytes(Environment.NewLine); + private static readonly byte[] _dataPrefix = Encoding.UTF8.GetBytes("data: "); + private static readonly byte[] _sseLineEnding = Encoding.UTF8.GetBytes("\r\n"); + private static readonly byte[] _newLine = Encoding.UTF8.GetBytes(Environment.NewLine); private InternalParseState _internalParserState = InternalParseState.ReadMessagePayload; - private List _data = new List(); + private readonly List _data = new List(); public ParseResult ParseMessage(ReadOnlySequence buffer, out SequencePosition consumed, out SequencePosition examined, out byte[] message) { diff --git a/src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.cs b/src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.cs index 2b42146ac4..de9c37256e 100644 --- a/src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.cs +++ b/src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.cs @@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Http.Connections private Timer _timer; private readonly ILogger _logger; private readonly ILogger _connectionLogger; - private object _executionLock = new object(); + private readonly object _executionLock = new object(); private bool _disposed; public HttpConnectionManager(ILoggerFactory loggerFactory, IApplicationLifetime appLifetime) diff --git a/src/Microsoft.AspNetCore.SignalR.Core/HubCallerClients.cs b/src/Microsoft.AspNetCore.SignalR.Core/HubCallerClients.cs index 7c6f109732..3fafdabbab 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/HubCallerClients.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/HubCallerClients.cs @@ -7,9 +7,9 @@ namespace Microsoft.AspNetCore.SignalR { public class HubCallerClients : IHubCallerClients { - private string _connectionId; - private IHubClients _hubClients; - private string[] _currentConnectionId; + private readonly string _connectionId; + private readonly IHubClients _hubClients; + private readonly string[] _currentConnectionId; public HubCallerClients(IHubClients hubClients, string connectionId) { diff --git a/src/Microsoft.AspNetCore.SignalR.Core/TypedClientBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Core/TypedClientBuilder.cs index 83bc3e45ac..99b7f6aad5 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/TypedClientBuilder.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/TypedClientBuilder.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.SignalR private const string ClientModuleName = "Microsoft.AspNetCore.SignalR.TypedClientBuilder"; // There is one static instance of _builder per T - private static Lazy> _builder = new Lazy>(() => GenerateClientBuilder()); + private static readonly Lazy> _builder = new Lazy>(() => GenerateClientBuilder()); public static T Build(IClientProxy proxy) { diff --git a/src/Microsoft.AspNetCore.SignalR.Core/TypedHubClients.cs b/src/Microsoft.AspNetCore.SignalR.Core/TypedHubClients.cs index 0ac92057f6..d984dbd99e 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/TypedHubClients.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/TypedHubClients.cs @@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.SignalR { internal class TypedHubClients : IHubCallerClients { - private IHubCallerClients _hubClients; + private readonly IHubCallerClients _hubClients; public TypedHubClients(IHubCallerClients dynamicContext) { diff --git a/src/Microsoft.AspNetCore.SignalR.Redis/RedisHubLifetimeManager.cs b/src/Microsoft.AspNetCore.SignalR.Redis/RedisHubLifetimeManager.cs index aeae6d1df2..a09deaffbb 100644 --- a/src/Microsoft.AspNetCore.SignalR.Redis/RedisHubLifetimeManager.cs +++ b/src/Microsoft.AspNetCore.SignalR.Redis/RedisHubLifetimeManager.cs @@ -579,8 +579,8 @@ namespace Microsoft.AspNetCore.SignalR.Redis private class GroupData { - public SemaphoreSlim Lock = new SemaphoreSlim(1, 1); - public HubConnectionStore Connections = new HubConnectionStore(); + public readonly SemaphoreSlim Lock = new SemaphoreSlim(1, 1); + public readonly HubConnectionStore Connections = new HubConnectionStore(); } private interface IRedisFeature diff --git a/test/Microsoft.AspNetCore.Http.Connections.Tests/MapConnectionHandlerTests.cs b/test/Microsoft.AspNetCore.Http.Connections.Tests/MapConnectionHandlerTests.cs index 57adeb80ef..0d24cf0e61 100644 --- a/test/Microsoft.AspNetCore.Http.Connections.Tests/MapConnectionHandlerTests.cs +++ b/test/Microsoft.AspNetCore.Http.Connections.Tests/MapConnectionHandlerTests.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Http.Connections.Tests { public class MapConnectionHandlerTests { - private ITestOutputHelper _output; + private readonly ITestOutputHelper _output; public MapConnectionHandlerTests(ITestOutputHelper output) { diff --git a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/MessagePackHubProtocolTests.cs b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/MessagePackHubProtocolTests.cs index 10604984ec..e6bdb6090d 100644 --- a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/MessagePackHubProtocolTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/MessagePackHubProtocolTests.cs @@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol { "ValueWithNewLines", "Also\nWorks\r\nFine" }, }; - private static MessagePackObject TestHeadersSerialized = Map( + private static readonly MessagePackObject TestHeadersSerialized = Map( ("Foo", "Bar"), ("KeyWith\nNew\r\nLines", "Still Works"), ("ValueWithNewLines", "Also\nWorks\r\nFine")); @@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol private static readonly MessagePackHubProtocol _hubProtocol = new MessagePackHubProtocol(); - private static MessagePackObject CustomObjectSerialized = Map( + private static readonly MessagePackObject CustomObjectSerialized = Map( ("ByteArrProp", new MessagePackObject(new byte[] { 1, 2, 3 }, isBinary: true)), ("DateTimeProp", new MessagePackObject(Timestamp.FromDateTime(new DateTime(2017, 4, 11)))), ("DoubleProp", 6.2831853071), diff --git a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Docker.cs b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Docker.cs index f48f30f3ba..fcc7ba066d 100644 --- a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Docker.cs +++ b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/Docker.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis.Tests private static readonly string _exeSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : string.Empty; private static readonly string _dockerContainerName = "redisTestContainer"; - private static Lazy _instance = new Lazy(Create); + private static readonly Lazy _instance = new Lazy(Create); public static Docker Default => _instance.Value; diff --git a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/ServerFixture.cs b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/ServerFixture.cs index 805bedffaa..51aa1a1502 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/ServerFixture.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/ServerFixture.cs @@ -148,7 +148,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests // TestSink doesn't seem to be thread-safe :(. private class LogSinkProvider : ILoggerProvider { - private ConcurrentQueue _logs = new ConcurrentQueue(); + private readonly ConcurrentQueue _logs = new ConcurrentQueue(); public ILogger CreateLogger(string categoryName) { @@ -179,8 +179,8 @@ namespace Microsoft.AspNetCore.SignalR.Tests private class LogSinkLogger : ILogger { - private string _categoryName; - private LogSinkProvider _logSinkProvider; + private readonly string _categoryName; + private readonly LogSinkProvider _logSinkProvider; public LogSinkLogger(string categoryName, LogSinkProvider logSinkProvider) { diff --git a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TestConnectionMultiplexer.cs b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TestConnectionMultiplexer.cs index 412ccf0cd4..2852f9c64c 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TestConnectionMultiplexer.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TestConnectionMultiplexer.cs @@ -70,7 +70,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests remove { } } - private ISubscriber _subscriber; + private readonly ISubscriber _subscriber; public TestConnectionMultiplexer(TestRedisServer server) { @@ -210,7 +210,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests public class TestRedisServer { - private ConcurrentDictionary>> _subscriptions = + private readonly ConcurrentDictionary>> _subscriptions = new ConcurrentDictionary>>(); public long Publish(RedisChannel channel, RedisValue message, CommandFlags flags = CommandFlags.None) diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/CancellationDisposable.cs b/test/Microsoft.AspNetCore.SignalR.Tests/CancellationDisposable.cs index 50a02883ca..1400026549 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/CancellationDisposable.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/CancellationDisposable.cs @@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests { internal class CancellationDisposable : IDisposable { - private CancellationTokenSource _cts; + private readonly CancellationTokenSource _cts; public CancellationDisposable(CancellationTokenSource cts) { diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs index 38dd0ee325..01c1d5f2b7 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs @@ -473,7 +473,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests private string _prevConnectionId = null; private IDuplexPipe _application; private IDuplexPipe _transport; - private int availableTransports = 3; + private readonly int availableTransports = 3; public PipeReader Input => _transport.Input; diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTestUtils/Hubs.cs b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTestUtils/Hubs.cs index 8782293e8c..3d3e4135df 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTestUtils/Hubs.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTestUtils/Hubs.cs @@ -498,7 +498,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests public class ConnectionLifetimeHub : Hub { - private ConnectionLifetimeState _state; + private readonly ConnectionLifetimeState _state; public ConnectionLifetimeHub(ConnectionLifetimeState state) {