Request for an API for WaitOnExit in StandAlone scenario[Solved] #342

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

Originally created by @ZenwalkerD on GitHub (Mar 20, 2021).

Originally assigned to: @StefH on GitHub.

Is your feature request related to a problem? Please describe.
In my integration tests, i am using Standalone style of using Wiremock. And i am following below example https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs as a template to start with.
I do not want to make it a Windows Service because in my pipeline; i do not have Admin rights to install it as service. Neither i can make it a Web app because IIS is not available in pipeline.
However, in my case, since no manual intervention can be done; so i am just sleeping infinitely rather than taking an Console key entry to close the app/process.

Describe the solution you'd like
An API similar to Process.WaitForExit(), can be provided. That way i need not need to kill the process when the process is sleeping indefinitely (standalone).
I can issue WiremockServer.Stop(); then WaitForExit() will allow the control flow to end just like how Process.WaitForExit behaves.

Describe alternatives you've considered
Killing the process.

Is your feature request supported by WireMock (java version)? Please provide details.
Not sure. I do not use java version.

Additional context
None

Originally created by @ZenwalkerD on GitHub (Mar 20, 2021). Originally assigned to: @StefH on GitHub. **Is your feature request related to a problem? Please describe.** In my integration tests, i am using Standalone style of using Wiremock. And i am following below example https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs as a template to start with. I do not want to make it a Windows Service because in my pipeline; i do not have Admin rights to install it as service. Neither i can make it a Web app because IIS is not available in pipeline. However, in my case, since no manual intervention can be done; so i am just sleeping infinitely rather than taking an Console key entry to close the app/process. **Describe the solution you'd like** An API similar to Process.WaitForExit(), can be provided. That way i need not need to kill the process when the process is sleeping indefinitely (standalone). I can issue WiremockServer.Stop(); then WaitForExit() will allow the control flow to end just like how Process.WaitForExit behaves. **Describe alternatives you've considered** Killing the process. **Is your feature request supported by [WireMock (java version)](https://www.wiremock.org)? Please provide details.** Not sure. I do not use java version. **Additional context** None
adam added the question label 2025-12-29 15:21:22 +01:00
adam closed this issue 2025-12-29 15:21:22 +01:00
Author
Owner

@StefH commented on GitHub (Mar 20, 2021):

Do you mean this scenario?

1.

You start the WireMock.Net using a console app, just like WireMock.Net.StandAlone.NETCoreApp

2.

You removed the code which handles Ctrl+C

3.

But what next ? How can you stop the console-app ?
Do you want to post a Stop-command to the Admin interface to stop WireMock.Net?

or

Do you just want to use WaitForExit(timeout) -> this is already possible ?

@StefH commented on GitHub (Mar 20, 2021): Do you mean this scenario? ### 1. You start the WireMock.Net using a console app, just like WireMock.Net.StandAlone.NETCoreApp ### 2. You removed the code which handles `Ctrl+C` ### 3. But what next ? How can you stop the console-app ? Do you want to post a Stop-command to the Admin interface to stop WireMock.Net? or Do you just want to use `WaitForExit(timeout)` -> this is already possible ?
Author
Owner

@ZenwalkerD commented on GitHub (Mar 20, 2021):

Let me explain with the code below...
I created a windows form App (without form) with Main Function as shown:

//MockServer.exe
private static void Main(string[] args)
        {
            server = WireMockServer.Start(new WireMockServerSettings()
            {
                Port = 9500,
                Urls = new[] { "http://+:9500/" },
                StartAdminInterface = true,
                AllowCSharpCodeMatcher = true
            });
 Thread.Sleep(Timeout.Infinite);
        }

Now i created another class in another project to start above app as shown below:

internal static class MockServerProcessHandler
    {
        public static void StartMockServer(StubType stubType)
        {
            //process = Process.Start("FunctionPoints.External.MockServer.exe", stubType.ToString());
            ProcessStartInfo process = new ProcessStartInfo(@"MockServer.exe");
            process.UseShellExecute = false;
            process.Arguments = stubType.ToString();
            process.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
            Process p = new Process();
            p.StartInfo = process;
            p.Start();
            //p.WaitForExit();
        }
        public static void StopMockServer()
        {
            while (Process.GetProcessesByName("FunctionPoints.External.MockServer").Length > 0)
            {
                Process[] p = Process.GetProcessesByName("FunctionPoints.External.MockServer");
                p[0].Kill(true);
                Thread.Sleep(1000);
            }
        }
    }

As u can see above from class MockServerProcessHandler. I start the exe process which actually starts the mockserver.

p.s: Sorry, github is screwing up the formatting of code pasted. I tried to fix, it aint indenting

@ZenwalkerD commented on GitHub (Mar 20, 2021): Let me explain with the code below... I created a windows form App (without form) with Main Function as shown: ``` c# //MockServer.exe private static void Main(string[] args) { server = WireMockServer.Start(new WireMockServerSettings() { Port = 9500, Urls = new[] { "http://+:9500/" }, StartAdminInterface = true, AllowCSharpCodeMatcher = true }); Thread.Sleep(Timeout.Infinite); } ``` Now i created another class in another project to start above app as shown below: ``` c# internal static class MockServerProcessHandler { public static void StartMockServer(StubType stubType) { //process = Process.Start("FunctionPoints.External.MockServer.exe", stubType.ToString()); ProcessStartInfo process = new ProcessStartInfo(@"MockServer.exe"); process.UseShellExecute = false; process.Arguments = stubType.ToString(); process.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory; Process p = new Process(); p.StartInfo = process; p.Start(); //p.WaitForExit(); } public static void StopMockServer() { while (Process.GetProcessesByName("FunctionPoints.External.MockServer").Length > 0) { Process[] p = Process.GetProcessesByName("FunctionPoints.External.MockServer"); p[0].Kill(true); Thread.Sleep(1000); } } } ``` As u can see above from class MockServerProcessHandler. I start the exe process which actually starts the mockserver. p.s: Sorry, github is screwing up the formatting of code pasted. I tried to fix, it aint indenting
Author
Owner

@StefH commented on GitHub (Mar 20, 2021):

I'm trying to understand your request....

Quick question : why don't you use WireMock.Net in a unit-testing scenario (https://github.com/WireMock-Net/WireMock.Net/wiki/Using-WireMock-in-UnitTests) ?

@StefH commented on GitHub (Mar 20, 2021): I'm trying to understand your request.... Quick question : why don't you use WireMock.Net in a unit-testing scenario (https://github.com/WireMock-Net/WireMock.Net/wiki/Using-WireMock-in-UnitTests) ?
Author
Owner

@ZenwalkerD commented on GitHub (Mar 22, 2021):

Ok let me explain my scenario here.
I am doing Integration Testing.
I have a DotNET Core WebAPI application.

I run my WebAPI on localhost. The integration test cases call my APIs exposed in WebAPI app using HttpClient (localhost/api/xxxx).
The WebAPI internally calls external API services (assume weather API).
So in my integration tests shown code in my previous post; the mockserver is used to mock the external APIs which is called by the WebAPI and not from Integration Tests.

Hence, in my integration tests i start mockserver as an independent process on port 9500; so that my webAPI app can invoke API requests on that port on localhost instead of actual external API.

Note: I tried the Unit testing style (link given by you) it wont work. Server starts, but from postman i can not use the api stubbed by wiremock.

@ZenwalkerD commented on GitHub (Mar 22, 2021): Ok let me explain my scenario here. I am doing Integration Testing. I have a DotNET Core WebAPI application. I run my WebAPI on localhost. The integration test cases call my APIs exposed in WebAPI app using HttpClient (localhost/api/xxxx). The WebAPI internally calls external API services (assume weather API). So in my integration tests shown code in my previous post; the mockserver is used to mock the external APIs which is called by the WebAPI and not from Integration Tests. Hence, in my integration tests i start mockserver as an independent process on port 9500; so that my webAPI app can invoke API requests on that port on localhost instead of actual external API. Note: I tried the Unit testing style (link given by you) it wont work. Server starts, but from postman i can not use the api stubbed by wiremock.
Author
Owner

@StefH commented on GitHub (Mar 22, 2021):

Another possibility is that you just keep the always WireMock.Net server running and just reset all mappings (via the admin interface) before you start your test.

Or you can also run the WireMock.Net server as a docker image and just kill/stop the container after the test?

@StefH commented on GitHub (Mar 22, 2021): Another possibility is that you just keep the always WireMock.Net server running and just reset all mappings (via the admin interface) before you start your test. Or you can also run the WireMock.Net server as a docker image and just kill/stop the container after the test?
Author
Owner

@ZenwalkerD commented on GitHub (Mar 22, 2021):

I can not do the reset map and again map as i am suppose to run the wiremock as an independent process.
So once the process starts, i wont be able to reset before every tests otherwise i will have to implement IPC. Which is a huge task.

Btw, not allowed to use Docket in pipeline. Hence using as a process.

But i wish to know how to close the process without killing it. I do not want to use Thread.Sleep after i run mockserver start API.
Because i am running Thread.Sleep; i am forced to kill the process.

@ZenwalkerD commented on GitHub (Mar 22, 2021): I can not do the reset map and again map as i am suppose to run the wiremock as an independent process. So once the process starts, i wont be able to reset before every tests otherwise i will have to implement IPC. Which is a huge task. Btw, not allowed to use Docket in pipeline. Hence using as a process. But i wish to know how to close the process without killing it. I do not want to use Thread.Sleep after i run mockserver start API. Because i am running Thread.Sleep; i am forced to kill the process.
Author
Owner

@StefH commented on GitHub (Mar 22, 2021):

1.

IPC is not needed.
When the WireMock.Net server starts, you can call the admin API (via post/get rest commands) to add or reset the mappings. (https://github.com/WireMock-Net/WireMock.Net/wiki/Admin-API-Reference#__adminmappings) that's the whole idea, you can start the server once, and before each test, just add the mappings you need.

2.

But i wish to know how to close the process without killing it. I do not want to use Thread.Sleep after i run mockserver start API.
Because i am running Thread.Sleep; i am forced to kill the process.

What do you mean by close the process? Why can't you just kill it?
Like:

  • Step A: Start WireMock.Net server process
  • Step B: Do your integration tests
  • Step C: Kill WireMock.Net server process
@StefH commented on GitHub (Mar 22, 2021): ### 1. IPC is not needed. When the WireMock.Net server starts, you can call the admin API (via post/get rest commands) to add or reset the mappings. (https://github.com/WireMock-Net/WireMock.Net/wiki/Admin-API-Reference#__adminmappings) that's the whole idea, you can start the server once, and before each test, just add the mappings you need. ### 2. > But i wish to know how to close the process without killing it. I do not want to use Thread.Sleep after i run mockserver start API. > Because i am running Thread.Sleep; i am forced to kill the process. What do you mean by close the process? Why can't you just kill it? Like: - Step A: Start WireMock.Net server process - Step B: Do your integration tests - Step C: Kill WireMock.Net server process
Author
Owner

@ZenwalkerD commented on GitHub (Mar 22, 2021):

Killing is bad ;)
Any way, i came up with a simple way to safely close the application rather than killing. Its working without much hassle. ;)
I am using ManualResetEvent. In Main func, i make ManualResetEvent to wait.
Then in one of the API request; i just notify it to close. ;)

Console App:

c#
class Program{
private static WireMockServer server;

        private static ManualResetEvent _mainThread = new ManualResetEvent(false);
private static void Main(string[] args)
{
 server = WireMockServer.Start(new WireMockServerSettings()
            {
                Urls = new[] { "http://localhost:9500/"},
                StartAdminInterface = true,
                AllowCSharpCodeMatcher = true
            });

            **server.Given(Request.Create().WithPath("/exit")).RespondWith(Response.Create().WithBody((data) =>
            {
                _mainThread.Set();
                System.Environment.Exit(0);
                return "";
            }));
 _mainThread.WaitOne();
            if (server != null)
            {
                server.Stop();
                server.Dispose();
            }
}

Process starter:

c#
public static void StartMockServer(StubType stubType)
        {
            if (Process.GetProcessesByName("MockServer").Length > 0)
                StopMockServer();

            ProcessStartInfo process = new ProcessStartInfo(@"MockServer.exe");
            process.UseShellExecute = false;
            process.Arguments = stubType.ToString();
            process.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
            Process p = new Process();
            p.StartInfo = process;
            p.Start();           
        }
 public static void StopMockServer()
        {
            try
            {
                ApiHelper.ExecuteGetRequest("http://localhost:9500/exit");
            }
            catch
            {
            }
        }

Thanks for your time. :)

@ZenwalkerD commented on GitHub (Mar 22, 2021): Killing is bad ;) Any way, i came up with a simple way to safely close the application rather than killing. Its working without much hassle. ;) I am using ManualResetEvent. In Main func, i make ManualResetEvent to wait. Then in one of the API request; i just notify it to close. ;) Console App: ``` c# class Program{ private static WireMockServer server; private static ManualResetEvent _mainThread = new ManualResetEvent(false); private static void Main(string[] args) { server = WireMockServer.Start(new WireMockServerSettings() { Urls = new[] { "http://localhost:9500/"}, StartAdminInterface = true, AllowCSharpCodeMatcher = true }); **server.Given(Request.Create().WithPath("/exit")).RespondWith(Response.Create().WithBody((data) => { _mainThread.Set(); System.Environment.Exit(0); return ""; })); _mainThread.WaitOne(); if (server != null) { server.Stop(); server.Dispose(); } } ``` Process starter: ``` c# public static void StartMockServer(StubType stubType) { if (Process.GetProcessesByName("MockServer").Length > 0) StopMockServer(); ProcessStartInfo process = new ProcessStartInfo(@"MockServer.exe"); process.UseShellExecute = false; process.Arguments = stubType.ToString(); process.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory; Process p = new Process(); p.StartInfo = process; p.Start(); } public static void StopMockServer() { try { ApiHelper.ExecuteGetRequest("http://localhost:9500/exit"); } catch { } } ``` Thanks for your time. :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#342