protected override void ReleaseManagedResources() { if (m_OrderBooksCache != null) { try { foreach (DepthWebSocketCache cache in m_OrderBooksCache.Values) { cache.Unsubscribe(); } } catch { } } if (m_UserDataWebSocket != null) { try { m_UserDataWebSocket.UnsubscribeAllAsync().Wait(); m_UserDataWebSocket.Dispose(); } catch { } } if (m_User != null) { try { m_User.Dispose(); } catch { } } base.ReleaseManagedResources(); }
public static async Task Main(string[] args) { // Un-comment to run... //await AccountBalancesExample.ExampleMain(args); //await AccountBalancesExample.AdvancedExampleMain(args); //await MinimalWithDependencyInjection.ExampleMain(args); //await MinimalWithoutDependencyInjection.ExampleMain(args); //await OrderBookCacheAccountBalanceExample.AdvancedExampleMain(args); //await ReadMeExample.ExampleMain(args); var cts = new CancellationTokenSource(); try { // Load configuration. Configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", true, false) .AddUserSecrets <Program>() // for access to API key and secret. .Build(); // Configure services. ServiceProvider = new ServiceCollection() // ReSharper disable once ArgumentsStyleLiteral .AddBinance(useSingleCombinedStream: true) // add default Binance services. // Use alternative, low-level, web socket client implementation. //.AddTransient<IWebSocketClient, WebSocket4NetClient>() //.AddTransient<IWebSocketClient, WebSocketSharpClient>() .AddOptions() .Configure <BinanceApiOptions>(Configuration.GetSection("ApiOptions")) .AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace)) .BuildServiceProvider(); // Configure logging. ServiceProvider .GetService <ILoggerFactory>() .AddConsole(Configuration.GetSection("Logging:Console")) .AddFile(Configuration.GetSection("Logging:File")); // NOTE: Using ":" requires Microsoft.Extensions.Configuration.Binder. var apiKey = Configuration["BinanceApiKey"] // user secrets configuration. ?? Configuration.GetSection("User")["ApiKey"]; // appsettings.json configuration. var apiSecret = Configuration["BinanceApiSecret"] // user secrets configuration. ?? Configuration.GetSection("User")["ApiSecret"]; // appsettings.json configuration. if (string.IsNullOrWhiteSpace(apiKey) || string.IsNullOrWhiteSpace(apiSecret)) { PrintApiNotice(); } if (!string.IsNullOrEmpty(apiKey)) { User = ServiceProvider .GetService <IBinanceApiUserProvider>() .CreateUser(apiKey, apiSecret); } Api = ServiceProvider.GetService <IBinanceApi>(); ClientManager = ServiceProvider.GetService <IBinanceWebSocketClientManager>(); ClientManager.Error += (s, e) => { lock (ConsoleSync) { Console.WriteLine(); Console.WriteLine($"! Client Manager Error: \"{e.Exception.Message}\""); Console.WriteLine(); } }; UserDataManager = ServiceProvider.GetService <IUserDataWebSocketManager>(); // Instantiate all assembly command handlers. foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { if (typeof(IHandleCommand).IsAssignableFrom(type) && !type.IsAbstract) { CommandHandlers.Add((IHandleCommand)Activator.CreateInstance(type)); } } await SuperLoopAsync(cts.Token); } catch (Exception e) { lock (ConsoleSync) { Console.WriteLine($"! FAIL: \"{e.Message}\""); if (e.InnerException != null) { Console.WriteLine($" -> Exception: \"{e.InnerException.Message}\""); } } } finally { await DisableLiveTaskAsync(); cts.Cancel(); cts.Dispose(); User?.Dispose(); ClientManager?.Dispose(); lock (ConsoleSync) { Console.WriteLine(); Console.WriteLine(" ...press any key to close window."); Console.ReadKey(true); } } }
public static async Task Main(string[] args) { // Un-comment to run... //await AccountBalancesExample.ExampleMain(args); //await MinimalWithDependencyInjection.ExampleMain(args); //await MinimalWithoutDependencyInjection.ExampleMain(args); //await SerializationExample.ExampleMain(args); var cts = new CancellationTokenSource(); try { // Load configuration. Configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", true, false) .AddUserSecrets <Program>() .Build(); // Configure services. ServiceProvider = new ServiceCollection() .AddBinance() .AddOptions() .AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace)) .Configure <BinanceApiOptions>(Configuration.GetSection("ApiOptions")) .Configure <UserDataWebSocketClientOptions>(Configuration.GetSection("UserClientOptions")) .BuildServiceProvider(); // Configure logging. ServiceProvider .GetService <ILoggerFactory>() .AddConsole(Configuration.GetSection("Logging").GetSection("Console")) .AddFile(Configuration.GetSection("Logging").GetSection("File")); var apiKey = Configuration["BinanceApiKey"] // user secrets configuration. ?? Configuration.GetSection("User")["ApiKey"]; // appsettings.json configuration. var apiSecret = Configuration["BinanceApiSecret"] // user secrets configuration. ?? Configuration.GetSection("User")["ApiSecret"]; // appsettings.json configuration. if (string.IsNullOrWhiteSpace(apiKey) || string.IsNullOrWhiteSpace(apiSecret)) { PrintApiNotice(); } if (!string.IsNullOrEmpty(apiKey)) { User = ServiceProvider .GetService <IBinanceApiUserProvider>() .CreateUser(apiKey, apiSecret); } Api = ServiceProvider.GetService <IBinanceApi>(); // Instantiate all assembly command handlers. foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { if (typeof(IHandleCommand).IsAssignableFrom(type) && !type.IsAbstract) { CommandHandlers.Add((IHandleCommand)Activator.CreateInstance(type)); } } await SuperLoopAsync(cts.Token); } catch (Exception e) { lock (ConsoleSync) { Console.WriteLine($"! FAIL: \"{e.Message}\""); if (e.InnerException != null) { Console.WriteLine($" -> Exception: \"{e.InnerException.Message}\""); } } } finally { await DisableLiveTask(); cts.Cancel(); cts.Dispose(); User?.Dispose(); lock (ConsoleSync) { Console.WriteLine(); Console.WriteLine(" ...press any key to close window."); Console.ReadKey(true); } } }