Testnet BinanceSocketClient [CantConnectError] Can't connect to the server

I have this .NET Code to test subscribing to Kline Updates data using Binance.Net v10.20.0:

private static async Task SubscribeToKlineUpdates(BinanceSocketClient socketClient)
{
	var sub = await socketClient.SpotApi.ExchangeData.SubscribeToKlineUpdatesAsync(
		symbol: "BTCUSDT",
		interval: KlineInterval.OneMinute,
		HandleKlineUpdate);

	if (!sub.Success)
	{
		Console.WriteLine($"Subscription failed: {sub.Error}");
		Console.WriteLine($"Message: {sub.Error?.Message}");
		return;
	}

	Console.WriteLine("Subscribed to BTCUSDT 1m klines. Press any key to unsubscribe and exit.");
	Console.ReadKey();

	await socketClient.UnsubscribeAsync(sub.Data);
	Console.WriteLine("Unsubscribed.");
}

The socket client is initialised like so:

	BinanceSocketClient.SetDefaultOptions(options =>
	{
		options.Environment = BinanceEnvironment.Testnet;
		options.ApiCredentials = new ApiCredentials(ApiKey, ApiSecret);
	});

The ApiKey and ApiSecret work perfectly fine when initialising a BinanceRestClient instance and I can retrieve data from the Binance API with the REST client no problems.

This was working fine for me a few days ago but now all of sudden I get the following error message when trying to subscribe to (stream) Kline Updates data:

[CantConnectError] Can’t connect to the server

There is no other error data, simply this error message! So I have no idea why this is occurring. Anyone had this issue before and a way to resolve it? Please note that I haven’t executed this code for a number of days, so I shouldn’t have exceeded any limits as far as I know.

Hi @Mick,

Have you also tried with the mainnet URL to verify that this is not a testnet issue?

Hi @dimitrisn,
And for anyone else who comes across this thread / error.

I sent a DM to Binance Support on X and they recommended using the official Binance.Spot Nuget Package for Visual Studio, currently at the time of writing:

Solved the issue and I’ll be sticking with the official Nuget Package from now, I have a bit of code to refactor!

1 Like