From e71ed56d0c0fb99a9197d77749c5246edbaa8420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B8=D0=BC=D0=BE=D1=84=D0=B5=D0=B9=20=D0=93=D1=80?= =?UTF-8?q?=D0=B8=D0=BD=D1=86=D0=B5=D0=B2=D0=B8=D1=87?= Date: Fri, 28 Sep 2018 06:15:02 +0000 Subject: [PATCH 1/2] IServiceProvider ObjectDisposedException fix. Error occures when app is restarts. BinanceHttpClient.Initializer.IsValueCreate true, so we dont recreate BinanceHttpClient, but is needed, because the old IServiceProvider (which uses in ApiRateLimiterProvider, RateLimiterProvider) already disposed --- .../Extensions/ServiceCollectionExtensions.cs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Binance/Extensions/ServiceCollectionExtensions.cs b/src/Binance/Extensions/ServiceCollectionExtensions.cs index 87d695b0..0ff16358 100644 --- a/src/Binance/Extensions/ServiceCollectionExtensions.cs +++ b/src/Binance/Extensions/ServiceCollectionExtensions.cs @@ -21,16 +21,12 @@ public static IServiceCollection AddBinance(this IServiceCollection services, bo services.AddSingleton(); services.AddSingleton(s => { - if (!BinanceHttpClient.Initializer.IsValueCreated) - { - // Replace initializer. - BinanceHttpClient.Initializer = new Lazy(() => - new BinanceHttpClient( - s.GetService(), - s.GetService(), - s.GetService>(), - s.GetService>()), true); - } + BinanceHttpClient.Initializer = new Lazy(() => + new BinanceHttpClient( + s.GetService(), + s.GetService(), + s.GetService>(), + s.GetService>()), true); return BinanceHttpClient.Instance; }); From 954c0e830c5dfdc75bd88bb53d2f0e5e57c22405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B8=D0=BC=D0=BE=D1=84=D0=B5=D0=B9=20=D0=93=D1=80?= =?UTF-8?q?=D0=B8=D0=BD=D1=86=D0=B5=D0=B2=D0=B8=D1=87?= Date: Fri, 28 Sep 2018 07:16:19 +0000 Subject: [PATCH 2/2] test added --- .../Api/BinanceHttpClientTest.cs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/Binance.Tests/Api/BinanceHttpClientTest.cs diff --git a/test/Binance.Tests/Api/BinanceHttpClientTest.cs b/test/Binance.Tests/Api/BinanceHttpClientTest.cs new file mode 100644 index 00000000..acf638f9 --- /dev/null +++ b/test/Binance.Tests/Api/BinanceHttpClientTest.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Binance.Tests.Api +{ + public class BinanceHttpClientTest + { + [Fact] + public void ServiceProviderDisposeNoThrows() + { + var serviceProvider = new ServiceCollection() + .AddBinance().BuildServiceProvider(); + + var httpClient = serviceProvider.GetService(); + + //Restart simulation + serviceProvider.Dispose(); + + var objectDisposedException = false; + + serviceProvider = new ServiceCollection() + .AddBinance().BuildServiceProvider(); + + var newHttpClient = serviceProvider.GetService(); + + try + { + newHttpClient.RateLimiter.Configure(TimeSpan.FromSeconds(5), 5); + } + catch (ObjectDisposedException) + { + objectDisposedException = true; + } + + Assert.False(objectDisposedException); + Assert.False(newHttpClient.Equals(httpClient)); + } + } +}