System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it. (localhost:11435) means nothing is listening on the host and port your .NET client was given. Point the client at the port Ollama really uses, http://localhost:11434 by default, and make sure the server is running.

The port in brackets at the end of the message is the one your code tried. Compare it with 11434, then run ollama list: if it prints your models, the server is up and the address in your code is the problem.

The error

Unhandled exception. System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it. (localhost:11435)
 ---> System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it.
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)

Why it happens

"Actively refused" is Windows socket error 10061: the TCP connection reached the machine, and the machine answered that no program accepts connections on that port. In this test the client points at port 11435 on purpose, where nothing listens, while Ollama itself was running on 11434. The same message appears when the address is right but Ollama is not running at all, for example after the tray app was quit or before the service started.

OllamaSharp does not wrap this failure: the exception comes straight from HttpClient, so the stack trace shows only System.Net frames at the top and nothing mentions Ollama. That is why the port in brackets is the most useful part of the message.

The fix

IChatClient ollama = new OllamaApiClient(new Uri(baseUrl), model);
var reply = await ollama.GetResponseAsync("In one short sentence, what is Blazor?");
Console.WriteLine(reply.Text);
PS> ollama list
NAME                       ID              SIZE      MODIFIED
qwen2.5:3b                 357c53fb659c    1.9 GB    Less than a second ago

PS> dotnet run --no-build -- qwen2.5:3b http://localhost:11434 chat
Blazor is a framework for building web applications using .NET that renders client-side using JavaScript and HTML.

The test program takes the model, base URL and mode from its arguments, so the fix was the URL alone. Keep the address in configuration rather than in code, so a machine where Ollama runs elsewhere (another port, a WSL distribution, a server) needs a settings change and not a rebuild. If ollama list itself fails, start Ollama first; the could not connect post below covers that on Linux and WSL.

How it was reproduced

A console app from dotnet new console -n FixLab -f net10.0 on .NET SDK 10.0.401, with Microsoft.Extensions.AI 10.10.0 and OllamaSharp 5.5.0, called GetResponseAsync through OllamaApiClient. Ollama 0.40.1 ran on Windows 11 on its default port; the run against 11435 failed as shown (the stack trace is cut after its first frame) and the run against 11434 answered. The ollama list output is shortened to one of its five rows.

Frequently asked

What port does Ollama listen on?
By default Ollama listens on http://localhost:11434. A .NET client must use that address unless you changed it with the OLLAMA_HOST setting.
Why does my .NET app say the target machine actively refused it when calling Ollama?
Nothing accepts connections on the host and port in your base URL. Either the port is wrong or Ollama is not running. Check the port in brackets at the end of the message and run ollama list.
How do I check that the Ollama server is running?
Run ollama list in a terminal. If it prints your models, the server is up on its configured address. If it fails, start the Ollama app or service first.

More decoded errors in the Fixes category. When the server is the part that is down on Linux or WSL, read could not connect to ollama server.