C# Starting Appium programmatically - target machine actively refused it

I want to start Appium server programmatically using C#. when I use Appium window to start Appium manually, It Starts successfully: Appium window

But when I run it automatically often I get an exception:

"An unhandled exception of type ‘OpenQA.Selenium.WebDriverException’ occurred in WebDriver.dll

Additional information: Unexpected error. System.Net.WebException: Unable to connect to the remote server —> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:4723"
This is the c# code for starting Appium Server:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "C:/Program Files (x86)/Appium/node.exe";
startInfo.Arguments = @"""C:/Program Files (x86)/Appium/node.exe lib/server/main.js"" --address 127.0.0.1 --port 4723 --session-override --platform-name Android --platform-version 23 --automation-name Appium --log-no-color";
process.StartInfo = startInfo;
process.Start();


capabilities = new DesiredCapabilities();
capabilities.SetCapability("deviceName", "Samsung S6");
capabilities.SetCapability("platformName", "Android");

capabilities.SetCapability("platformVersion", "5.0.2");
capabilities.SetCapability(CapabilityType.BrowserName, "Chrome");

driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities, TimeSpan.FromSeconds(30));

I read those questions but it didn’t helped me:

Appium iOS automation using C#/Visual Studio No connection could be made because the target machine actively refused it 127.0.0.1:3446

Why when I start is manually Appium start successfully, but when I Start it the same programmatically I get refused?

1 Like

Someone maybe can help here?

Still getting this error…

Hi all,
I was receiving the same error. Here is my solution and it works perfectly with Windows 10 and VS2015 Professional:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/k (\"C:\\Program Files (x86)\\Appium\\node.exe\" \"C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\appium.js\" --address 127.0.0.1 --port 4723 --app \"C:\\Users\\youraplicationapk.apk\" --platform-name Android --platform-version 23 --automation-name Appium)";
process.StartInfo = startInfo;
process.Start();
Thread.Sleep(10000);

Basically the problem was that my command was not write in cmd.exe. The clu is to add /k and then if you have command with 2 file path you shoudl put it into (). So in short cut:
startInfo.Arguments = "/k (all your arguments)";

Of course the server arguments up to you.

PS. Please be sure that you run VS as administrator and also if you don’t want to see cmd window just change .Maximized to .Hidden in 3 line.