WireMock.Net fails during startup with a System.Net.Sockets.SocketException #324

Closed
opened 2025-12-29 15:20:47 +01:00 by adam · 8 comments
Owner

Originally created by @SimonA84 on GitHub (Dec 21, 2020).

I use the nuget package of WireMock.Net for unit tests with xunit. I want to use a port like 19999 to be outside of the range of well known ports.

How to Reproduce it:

  • Create a WireMockServerSettings object with the specific port
  • Start the server with the settings object
  • Add some "endpoints"

var settings = new WireMockServerSettings{Port = 19999}; var server = WireMockServer _server = WireMockServer.Start(settings);

What happens:
The server throws in the line with WireMockServer.Start an Wiremock.Exceptions.WireMockException with "Service start failed with error: At least one error occoured"
The inner exceptions are

  • System.AccreateException: At least one error occoured
  • System.Net.Sockets.SocketException: The Access to a socket was forbidden because the permissions are not correct
    (Sorry if you don't have the exact same message. My system is set to german)

What I tried:

  • I tried to run the test as an Administrator and as a normal windows user >> In both cases the same error
  • I checked if the port is used by a different application >> The Port is free
  • I changed my firewall settings >> Still the same error
  • I used an URL instead of the port (http://localhost:19999) >> The same error

What I want:
I want to understand why this happens and how to solve and prevent this problem.

Originally created by @SimonA84 on GitHub (Dec 21, 2020). I use the nuget package of WireMock.Net for unit tests with xunit. I want to use a port like 19999 to be outside of the range of well known ports. How to Reproduce it: - Create a WireMockServerSettings object with the specific port - Start the server with the settings object - Add some "endpoints" `var settings = new WireMockServerSettings{Port = 19999}; var server = WireMockServer _server = WireMockServer.Start(settings);` What happens: The server throws in the line with WireMockServer.Start an Wiremock.Exceptions.WireMockException with "Service start failed with error: At least one error occoured" The inner exceptions are - System.AccreateException: At least one error occoured - System.Net.Sockets.SocketException: The Access to a socket was forbidden because the permissions are not correct (Sorry if you don't have the exact same message. My system is set to german) What I tried: - I tried to run the test as an Administrator and as a normal windows user >> In both cases the same error - I checked if the port is used by a different application >> The Port is free - I changed my firewall settings >> Still the same error - I used an URL instead of the port (http://localhost:19999) >> The same error What I want: I want to understand why this happens and how to solve and prevent this problem.
adam added the question label 2025-12-29 15:20:47 +01:00
adam closed this issue 2025-12-29 15:20:47 +01:00
Author
Owner

@StefH commented on GitHub (Dec 21, 2020):

Add some "endpoints" --> you mean add some mappings ?

@StefH commented on GitHub (Dec 21, 2020): `Add some "endpoints" ` --> you mean add some mappings ?
Author
Owner

@StefH commented on GitHub (Dec 21, 2020):

  • What OS do you use ?
  • Do you start WireMock.Net consoleapp as admin ?
    -Are you able to start the example console apps in this solution ?
@StefH commented on GitHub (Dec 21, 2020): - What OS do you use ? - Do you start WireMock.Net consoleapp as admin ? -Are you able to start the example console apps in this solution ?
Author
Owner

@SimonA84 commented on GitHub (Dec 21, 2020):

Yes. I mean with endpoints some mappings. The extended URL is by my collegues an endpoint.
I'm using a Win10 x64 Enterprise.
I don't start WireMock.net as a dedicated app. I start it in the tests that I'm running. Like in the following snippet.

string mockurl = "http://localhost:19999";
[Fact, Trait("Category", "WireMock")]
public void GetPluginVersionTest()
{
	var settings = new WireMockServerSettings()
	{
		Urls = new[] { mockurl }
	};

	_mockServer = WireMockServer.Start(settings);

	_mockServer
		.Given(
			Request
				.Create()
				.WithPath("/cadresources/buildVersion")
				.UsingGet()
		)
		.RespondWith(
			Response
				.Create()
				.WithStatusCode(HttpStatusCode.OK)
				.WithHeader("Content-Type", "application/octet-stream")
				.WithBodyFromFile(getWireMockFilePath("buildVersion"))
		);
	string version = Utils.GetPluginVersion(mockurl);
	Assert.NotNull(version);

	Assert.True(version.Split('-').Length == 2 || version.Split('-').Length == 3);
}

As I told at the beginning. I use it in xunit tests.

@SimonA84 commented on GitHub (Dec 21, 2020): Yes. I mean with endpoints some mappings. The extended URL is by my collegues an endpoint. I'm using a Win10 x64 Enterprise. I don't start WireMock.net as a dedicated app. I start it in the tests that I'm running. Like in the following snippet. ``` c# string mockurl = "http://localhost:19999"; [Fact, Trait("Category", "WireMock")] public void GetPluginVersionTest() { var settings = new WireMockServerSettings() { Urls = new[] { mockurl } }; _mockServer = WireMockServer.Start(settings); _mockServer .Given( Request .Create() .WithPath("/cadresources/buildVersion") .UsingGet() ) .RespondWith( Response .Create() .WithStatusCode(HttpStatusCode.OK) .WithHeader("Content-Type", "application/octet-stream") .WithBodyFromFile(getWireMockFilePath("buildVersion")) ); string version = Utils.GetPluginVersion(mockurl); Assert.NotNull(version); Assert.True(version.Split('-').Length == 2 || version.Split('-').Length == 3); } ``` As I told at the beginning. I use it in xunit tests.
Author
Owner

@StefH commented on GitHub (Dec 21, 2020):

Hello @SimonA84,
It is advised to use random ports in unit tests.
https://github.com/WireMock-Net/WireMock.Net/wiki/Using-WireMock-in-UnitTests

@StefH commented on GitHub (Dec 21, 2020): Hello @SimonA84, It is advised to use random ports in unit tests. https://github.com/WireMock-Net/WireMock.Net/wiki/Using-WireMock-in-UnitTests
Author
Owner

@SimonA84 commented on GitHub (Dec 21, 2020):

I changed it to use different ports. It works, but I prefere using a fix port. An external server does not change the port

@SimonA84 commented on GitHub (Dec 21, 2020): I changed it to use different ports. It works, but I prefere using a fix port. An external server does not change the port
Author
Owner

@StefH commented on GitHub (Dec 21, 2020):

In that case, a possible solution would be to start the server once in the constructor/setup from the unit-test.

Can you try that?

@StefH commented on GitHub (Dec 21, 2020): In that case, a possible solution would be to start the server once in the constructor/setup from the unit-test. Can you try that?
Author
Owner

@SimonA84 commented on GitHub (Dec 22, 2020):

I got a working solution with one single port and it "recreates" the server in the constructor for each test as before (with stoping it afterwards).
But what I don't understand is why this port works and the other one has the permission problem
When I take a look at the list of standardized ports in wikipedia (I know the list is not well maintained), then I see that both ports (working: 5001 and not working: 9999) are used by some things which I don't have on my system. Netstat tells me that both ports aren't in use when I don't run my tests with WireMock.

@SimonA84 commented on GitHub (Dec 22, 2020): I got a working solution with one single port and it "recreates" the server in the constructor for each test as before (with stoping it afterwards). But what I don't understand is why this port works and the other one has the permission problem When I take a look at the list of standardized ports in wikipedia (I know the list is not well maintained), then I see that both ports (working: 5001 and not working: 9999) are used by some things which I don't have on my system. Netstat tells me that both ports aren't in use when I don't run my tests with WireMock.
Author
Owner

@SimonA84 commented on GitHub (Dec 28, 2020):

Thanks for your support.

@SimonA84 commented on GitHub (Dec 28, 2020): Thanks for your support.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#324