From b5833e523092d49df922554fda7bba783f26d702 Mon Sep 17 00:00:00 2001 From: JKorf Date: Thu, 16 Aug 2018 10:40:35 +0200 Subject: [PATCH] Fixed some resharper warnings --- .../ExchangeClientTests.cs | 1 + .../TestImplementation.cs | 1 + .../Attributes/JsonOptionalPropertyAttribute.cs | 3 --- .../Authentication/ApiCredentials.cs | 5 +++-- CryptoExchange.Net/ExchangeClient.cs | 12 ++++++------ CryptoExchange.Net/ExtensionMethods.cs | 2 +- CryptoExchange.Net/Implementation/BaseSocket.cs | 11 ++++++----- .../Implementation/TestWebsocket.cs | 5 ++--- .../Logging/ThreadSafeFileWriter.cs | 16 +++++++++------- CryptoExchange.Net/Objects/ApiProxy.cs | 2 +- CryptoExchange.Net/Objects/CallResult.cs | 2 +- CryptoExchange.Net/Objects/Error.cs | 2 +- CryptoExchange.Net/Objects/ExchangeOptions.cs | 3 +-- 13 files changed, 33 insertions(+), 32 deletions(-) diff --git a/CryptoExchange.Net.UnitTests/ExchangeClientTests.cs b/CryptoExchange.Net.UnitTests/ExchangeClientTests.cs index 89bb704..9a04522 100644 --- a/CryptoExchange.Net.UnitTests/ExchangeClientTests.cs +++ b/CryptoExchange.Net.UnitTests/ExchangeClientTests.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using CryptoExchange.Net.Authentication; using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Logging; +using CryptoExchange.Net.Objects; using CryptoExchange.Net.RateLimiter; using Moq; using Newtonsoft.Json; diff --git a/CryptoExchange.Net.UnitTests/TestImplementation.cs b/CryptoExchange.Net.UnitTests/TestImplementation.cs index 83da800..e2dd2b1 100644 --- a/CryptoExchange.Net.UnitTests/TestImplementation.cs +++ b/CryptoExchange.Net.UnitTests/TestImplementation.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using CryptoExchange.Net.Authentication; using CryptoExchange.Net.Interfaces; +using CryptoExchange.Net.Objects; namespace CryptoExchange.Net.UnitTests { diff --git a/CryptoExchange.Net/Attributes/JsonOptionalPropertyAttribute.cs b/CryptoExchange.Net/Attributes/JsonOptionalPropertyAttribute.cs index eb298f7..9c33a6a 100644 --- a/CryptoExchange.Net/Attributes/JsonOptionalPropertyAttribute.cs +++ b/CryptoExchange.Net/Attributes/JsonOptionalPropertyAttribute.cs @@ -1,11 +1,8 @@ using System; -using System.Collections.Generic; -using System.Text; namespace CryptoExchange.Net.Attributes { public class JsonOptionalPropertyAttribute : Attribute { - public JsonOptionalPropertyAttribute() { } } } diff --git a/CryptoExchange.Net/Authentication/ApiCredentials.cs b/CryptoExchange.Net/Authentication/ApiCredentials.cs index a34f5af..836a5c5 100644 --- a/CryptoExchange.Net/Authentication/ApiCredentials.cs +++ b/CryptoExchange.Net/Authentication/ApiCredentials.cs @@ -57,9 +57,10 @@ namespace CryptoExchange.Net.Authentication } /// - /// Create Api credentials providing a private key for authenication + /// Create Api credentials providing a api key and secret for authenciation /// - /// The private key used for signing + /// The api key used for identification + /// The api secret used for signing public ApiCredentials(string key, string secret) { if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(secret)) diff --git a/CryptoExchange.Net/ExchangeClient.cs b/CryptoExchange.Net/ExchangeClient.cs index 480d07b..bd4cd16 100644 --- a/CryptoExchange.Net/ExchangeClient.cs +++ b/CryptoExchange.Net/ExchangeClient.cs @@ -31,7 +31,7 @@ namespace CryptoExchange.Net protected AuthenticationProvider authProvider; private List rateLimiters; - private static JsonSerializer defaultSerializer = JsonSerializer.Create(new JsonSerializerSettings() + private static readonly JsonSerializer defaultSerializer = JsonSerializer.Create(new JsonSerializerSettings() { DateTimeZoneHandling = DateTimeZoneHandling.Utc }); @@ -104,7 +104,7 @@ namespace CryptoExchange.Net { var ping = new Ping(); var uri = new Uri(baseAddress); - PingReply reply = null; + PingReply reply; try { reply = await ping.SendPingAsync(uri.Host); @@ -115,8 +115,7 @@ namespace CryptoExchange.Net { if (e.InnerException is SocketException) return new CallResult(0, new CantConnectError() { Message = "Ping failed: " + ((SocketException)e.InnerException).SocketErrorCode }); - else - return new CallResult(0, new CantConnectError() { Message = "Ping failed: " + e.InnerException.Message }); + return new CallResult(0, new CantConnectError() { Message = "Ping failed: " + e.InnerException.Message }); } return new CallResult(0, new CantConnectError() { Message = "Ping failed: " + e.Message }); } @@ -150,7 +149,8 @@ namespace CryptoExchange.Net log.Write(LogVerbosity.Debug, $"Request {uri.AbsolutePath} failed because of rate limit"); return new CallResult(null, limitResult.Error); } - else if (limitResult.Data > 0) + + if (limitResult.Data > 0) log.Write(LogVerbosity.Debug, $"Request {uri.AbsolutePath} was limited by {limitResult.Data}ms by {limiter.GetType().Name}"); } @@ -359,7 +359,7 @@ namespace CryptoExchange.Net foreach (var prop in properties) { - var propInfo = props.FirstOrDefault(p => p.Name == prop || + var propInfo = props.First(p => p.Name == prop || ((JsonPropertyAttribute)p.GetCustomAttributes(typeof(JsonPropertyAttribute), false).FirstOrDefault())?.PropertyName == prop); var optional = propInfo.GetCustomAttributes(typeof(JsonOptionalPropertyAttribute), false).FirstOrDefault(); if (optional != null) diff --git a/CryptoExchange.Net/ExtensionMethods.cs b/CryptoExchange.Net/ExtensionMethods.cs index 6756b03..673def1 100644 --- a/CryptoExchange.Net/ExtensionMethods.cs +++ b/CryptoExchange.Net/ExtensionMethods.cs @@ -33,7 +33,7 @@ namespace CryptoExchange.Net { lock (source) { - string result = null; + string result; int length = source.Length; IntPtr pointer = IntPtr.Zero; char[] chars = new char[length]; diff --git a/CryptoExchange.Net/Implementation/BaseSocket.cs b/CryptoExchange.Net/Implementation/BaseSocket.cs index 7b9c5c7..342f86f 100644 --- a/CryptoExchange.Net/Implementation/BaseSocket.cs +++ b/CryptoExchange.Net/Implementation/BaseSocket.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using System.Net; using System.Security.Authentication; @@ -47,9 +46,11 @@ namespace CryptoExchange.Net.Implementation public BaseSocket(Log log, string url, IDictionary cookies, IDictionary headers) { this.log = log; - socket = new WebSocket(url, cookies: cookies.ToList(), customHeaderItems: headers.ToList()); - socket.EnableAutoSendPing = true; - socket.AutoSendPingInterval = 10; + socket = new WebSocket(url, cookies: cookies.ToList(), customHeaderItems: headers.ToList()) + { + EnableAutoSendPing = true, + AutoSendPingInterval = 10 + }; socket.Opened += (o, s) => Handle(openhandlers); socket.Closed += (o, s) => Handle(closehandlers); socket.Error += (o, s) => Handle(errorhandlers, s.Exception); @@ -108,7 +109,7 @@ namespace CryptoExchange.Net.Implementation var handler = new EventHandler((o, a) => evnt.Set()); socket.Closed += handler; socket.Close(); - bool triggered = evnt.WaitOne(3000); + evnt.WaitOne(3000); socket.Closed -= handler; log.Write(LogVerbosity.Debug, "Websocket closed"); } diff --git a/CryptoExchange.Net/Implementation/TestWebsocket.cs b/CryptoExchange.Net/Implementation/TestWebsocket.cs index 4cfe037..843abed 100644 --- a/CryptoExchange.Net/Implementation/TestWebsocket.cs +++ b/CryptoExchange.Net/Implementation/TestWebsocket.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Security.Authentication; -using System.Threading; using System.Threading.Tasks; using CryptoExchange.Net.Interfaces; @@ -39,7 +38,7 @@ namespace CryptoExchange.Net.Implementation { if (!HasConnection) { - OnError(new Exception("No connection")); + OnError?.Invoke(new Exception("No connection")); return Task.FromResult(false); } @@ -54,7 +53,7 @@ namespace CryptoExchange.Net.Implementation { if (!HasConnection) { - OnError(new Exception("No connection")); + OnError?.Invoke(new Exception("No connection")); Close(); return; } diff --git a/CryptoExchange.Net/Logging/ThreadSafeFileWriter.cs b/CryptoExchange.Net/Logging/ThreadSafeFileWriter.cs index 21f1184..f5c03d3 100644 --- a/CryptoExchange.Net/Logging/ThreadSafeFileWriter.cs +++ b/CryptoExchange.Net/Logging/ThreadSafeFileWriter.cs @@ -6,18 +6,17 @@ namespace CryptoExchange.Net.Logging { public class ThreadSafeFileWriter: TextWriter { - private static object openedFilesLock = new object(); - private static List openedFiles = new List(); + private static readonly object openedFilesLock = new object(); + private static readonly List openedFiles = new List(); private StreamWriter logWriter; - private object writeLock; + private readonly object writeLock; public override Encoding Encoding => Encoding.ASCII; public ThreadSafeFileWriter(string path) { - logWriter = new StreamWriter(File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)); - logWriter.AutoFlush = true; + logWriter = new StreamWriter(File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) {AutoFlush = true}; writeLock = new object(); lock(openedFilesLock) @@ -37,8 +36,11 @@ namespace CryptoExchange.Net.Logging protected override void Dispose(bool disposing) { - logWriter.Close(); - logWriter = null; + lock (writeLock) + { + logWriter.Close(); + logWriter = null; + } } } } diff --git a/CryptoExchange.Net/Objects/ApiProxy.cs b/CryptoExchange.Net/Objects/ApiProxy.cs index 9152d64..21c7cb6 100644 --- a/CryptoExchange.Net/Objects/ApiProxy.cs +++ b/CryptoExchange.Net/Objects/ApiProxy.cs @@ -1,6 +1,6 @@ using System; -namespace CryptoExchange.Net +namespace CryptoExchange.Net.Objects { public class ApiProxy { diff --git a/CryptoExchange.Net/Objects/CallResult.cs b/CryptoExchange.Net/Objects/CallResult.cs index 029c68a..af1d214 100644 --- a/CryptoExchange.Net/Objects/CallResult.cs +++ b/CryptoExchange.Net/Objects/CallResult.cs @@ -1,4 +1,4 @@ -namespace CryptoExchange.Net +namespace CryptoExchange.Net.Objects { public class CallResult { diff --git a/CryptoExchange.Net/Objects/Error.cs b/CryptoExchange.Net/Objects/Error.cs index 2d5be84..7a59bb1 100644 --- a/CryptoExchange.Net/Objects/Error.cs +++ b/CryptoExchange.Net/Objects/Error.cs @@ -1,4 +1,4 @@ -namespace CryptoExchange.Net +namespace CryptoExchange.Net.Objects { public abstract class Error { diff --git a/CryptoExchange.Net/Objects/ExchangeOptions.cs b/CryptoExchange.Net/Objects/ExchangeOptions.cs index 67d3159..d3e5670 100644 --- a/CryptoExchange.Net/Objects/ExchangeOptions.cs +++ b/CryptoExchange.Net/Objects/ExchangeOptions.cs @@ -2,10 +2,9 @@ using System.IO; using CryptoExchange.Net.Authentication; using CryptoExchange.Net.Logging; -using CryptoExchange.Net.Objects; using CryptoExchange.Net.RateLimiter; -namespace CryptoExchange.Net +namespace CryptoExchange.Net.Objects { /// /// Options