C# Unit test: Wiremock Server runs but does not deliver response #600

Closed
opened 2025-12-29 15:27:59 +01:00 by adam · 2 comments
Owner

Originally created by @elijah-ezaga on GitHub (May 17, 2024).

Originally assigned to: @StefH on GitHub.

Hi Wiremock.Net Team. I am new to using Wiremock.NET (v1.5.53, the latest) and trying to get it to work with a simple setup but its server does not deliver any response. For context, I have a simple C# Unit test file in a project (.NET Framework 461) with the following contents:

[TestFixture]
public class FooTest
{
	private WireMockServer wireMockServer;

	[SetUp]
	public void StartServer()
	{
		this.wireMockServer = WireMockServer.Start(new WireMock.Settings.WireMockServerSettings
		    {
			    Urls = new string[] { "http://localhost:8080" },
			    StartAdminInterface = true
		    }
		);
	}

	[TearDown]
	public void StopServer()
	{
		this.wireMockServer.Stop();
	}

	[Test]
	public async Task TestSomeResponse()
	{
                this.wireMockServer.Given(RequestCreator.Create().WithPath("/api").UsingGet())
		   .RespondWith(ResponseCreator.Create().WithStatusCode(200).WithBody("foo"));
		HttpResponseMessage response = await new HttpClient().GetAsync($"{this.wireMockServer.Urls[0]}/api"); // Line with failure
		Assert.That(response.StatusCode, Is.EqualTo(200));
	}
}

When I run the test, I get the error:

Message: 
System.Threading.Tasks.TaskCanceledException : A task was canceled.

  Stack Trace: 
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
TaskAwaiter`1.GetResult()
<TestSomeResponse>d__7.MoveNext() line 27
--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
TaskAwaiter`1.GetResult()
GenericAdapter`1.BlockUntilCompleted()
NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaiter)
AsyncToSyncAdapter.Await(Func`1 invoke)
TestMethodCommand.RunTestMethod(TestExecutionContext context)
TestMethodCommand.Execute(TestExecutionContext context)
<>c__DisplayClass1_0.<Execute>b__0()
DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)

Also If I add a line like await Task.Delay(TimeSpan.FromMinutes(5)) to pause execution and I make requests from Postman for admin URLs like __admin/mappings, it waits endlessly until the time exceeds and the test runs out and it doesn't get a response. However, I can confirm from a Windows command prompt, running netstat -ab, that the port is opened and the test process is listening for requests.

Any clues what might be wrong ?

Originally created by @elijah-ezaga on GitHub (May 17, 2024). Originally assigned to: @StefH on GitHub. Hi Wiremock.Net Team. I am new to using Wiremock.NET (v1.5.53, the latest) and trying to get it to work with a simple setup but its server does not deliver any response. For context, I have a simple C# Unit test file in a project (.NET Framework 461) with the following contents: ``` c# [TestFixture] public class FooTest { private WireMockServer wireMockServer; [SetUp] public void StartServer() { this.wireMockServer = WireMockServer.Start(new WireMock.Settings.WireMockServerSettings { Urls = new string[] { "http://localhost:8080" }, StartAdminInterface = true } ); } [TearDown] public void StopServer() { this.wireMockServer.Stop(); } [Test] public async Task TestSomeResponse() { this.wireMockServer.Given(RequestCreator.Create().WithPath("/api").UsingGet()) .RespondWith(ResponseCreator.Create().WithStatusCode(200).WithBody("foo")); HttpResponseMessage response = await new HttpClient().GetAsync($"{this.wireMockServer.Urls[0]}/api"); // Line with failure Assert.That(response.StatusCode, Is.EqualTo(200)); } } ``` When I run the test, I get the error: ``` Message: System.Threading.Tasks.TaskCanceledException : A task was canceled. Stack Trace: TaskAwaiter.ThrowForNonSuccess(Task task) TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) TaskAwaiter`1.GetResult() <TestSomeResponse>d__7.MoveNext() line 27 --- End of stack trace from previous location where exception was thrown --- TaskAwaiter.ThrowForNonSuccess(Task task) TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) TaskAwaiter`1.GetResult() GenericAdapter`1.BlockUntilCompleted() NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaiter) AsyncToSyncAdapter.Await(Func`1 invoke) TestMethodCommand.RunTestMethod(TestExecutionContext context) TestMethodCommand.Execute(TestExecutionContext context) <>c__DisplayClass1_0.<Execute>b__0() DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action) ``` Also If I add a line like `await Task.Delay(TimeSpan.FromMinutes(5))` to pause execution and I make requests from Postman for admin URLs like `__admin/mappings`, it waits endlessly until the time exceeds and the test runs out and it doesn't get a response. However, I can confirm from a Windows command prompt, running `netstat -ab`, that the port is opened and the test process is listening for requests. Any clues what might be wrong ?
adam added the question label 2025-12-29 15:27:59 +01:00
adam closed this issue 2025-12-29 15:27:59 +01:00
Author
Owner

@StefH commented on GitHub (May 18, 2024):

Can you please give the complete test project?

@StefH commented on GitHub (May 18, 2024): Can you please give the complete test project?
Author
Owner

@elijah-ezaga commented on GitHub (May 21, 2024):

Hi Stef. Thanks for offering to help.

While I was preparing the project to attach here, I found the issue and was able to fix it. It was connected with libraries in the project depending on 2 different versions of System.Memory leading to

System.IO.FileNotFoundException: Could not load file or assembly 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies.

I got it fixed by using binding redirect in app.config as the project type was a non-cross-platform .NET Framework test project type.

Later I re-created the project to be a cross-platform NUnit test project and there was no longer need for the binding redirect.

@elijah-ezaga commented on GitHub (May 21, 2024): Hi Stef. Thanks for offering to help. While I was preparing the project to attach here, I found the issue and was able to fix it. It was connected with libraries in the project depending on 2 different versions of `System.Memory` leading to > System.IO.FileNotFoundException: Could not load file or assembly 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. I got it fixed by using [binding redirect](https://stackoverflow.com/questions/55509638/could-not-load-file-or-assembly-system-memory-version-4-0-1-in-visual-studio) in `app.config` as the project type was a non-cross-platform .NET Framework test project type. Later I re-created the project to be a cross-platform NUnit test project and there was no longer need for the binding redirect.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#600