I am using Nethereum to call a function called owner()
in a BSC contract, which should return the contract owner's address, however when I use the CallAsync()
method, I get an exception "No connection could be made because the target machine actively refused it"
My code (simplified):
using (var s = webRequest.GetResponse().GetResponseStream())
{
using (var sr = new StreamReader(s))
{
string contractABIstr = sr.ReadToEnd();
JObject contractABI = JObject.Parse(contractABIstr);
string contractResults = (string)contractABI.SelectToken("result");
var web3 = new Web3();
var contract = web3.Eth.GetContract(contractResults, address);
Nethereum.Contracts.Function = contract.GetFunction("owner");
string owner = "";
Task<string> task = function.CallAsync<string>();
owner = await task;
}
}
When calling the line owner = await task
, I get an exception with message "Error occurred when trying to send rpc requests(s)", which has an inner exception with the message "An error occurred while sending the request.", which has its own inner exception with the message "Unable to connect to the remote server", and this one has its own inner exception saying "No connection could be made because the target machine actively refused it [IP address]"
Does anyone know what I'm doing wrong here? (I've simplified the code above but I do check that the function exists)
My problem turned out to be that I had initialised the Web3() with no parameters, so it defaults to using localhost as the RPC client. I fixed it by initialising it with the RPC client url that I needed, in this case:
string rpcClient = "https://bsc-dataseed.binance.org/";
Web3 web3 = new Web3(rpcClient);