mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 01:08:28 +02:00
Created WireMock running as a Windows Service (markdown)
72
WireMock-running-as-a-Windows-Service.md
Normal file
72
WireMock-running-as-a-Windows-Service.md
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
It's also possible to wrap WireMock in a Windows Service.
|
||||||
|
|
||||||
|
Create a **Program.cs** with the following content:
|
||||||
|
|
||||||
|
``` csharp
|
||||||
|
public static class Program
|
||||||
|
{
|
||||||
|
#region Nested classes to support running as service
|
||||||
|
public const string ServiceName = "Wiremock.Net.Service";
|
||||||
|
|
||||||
|
public class Service : ServiceBase
|
||||||
|
{
|
||||||
|
public Service()
|
||||||
|
{
|
||||||
|
ServiceName = Program.ServiceName;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnStart(string[] args)
|
||||||
|
{
|
||||||
|
Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnStop()
|
||||||
|
{
|
||||||
|
Program.Stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
private static FluentMockServer _server;
|
||||||
|
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
// running as service
|
||||||
|
if (!Environment.UserInteractive)
|
||||||
|
{
|
||||||
|
using (var service = new Service())
|
||||||
|
{
|
||||||
|
ServiceBase.Run(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// running as console app
|
||||||
|
Start();
|
||||||
|
|
||||||
|
Console.WriteLine("Press any key to stop...");
|
||||||
|
Console.ReadKey(true);
|
||||||
|
|
||||||
|
Stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Start()
|
||||||
|
{
|
||||||
|
_server = StandAloneApp.Start(new FluentMockServerSettings
|
||||||
|
{
|
||||||
|
Urls = new[] { "http://*:9091/" },
|
||||||
|
StartAdminInterface = true,
|
||||||
|
ReadStaticMappings = true,
|
||||||
|
Logger = new WireMockConsoleLogger()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Stop()
|
||||||
|
{
|
||||||
|
_server.Stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
When you start the exe file in Visual Studio or from the commandline, the application will behave same like a
|
||||||
Reference in New Issue
Block a user