diff --git a/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs b/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs index e060dc35..479d19d5 100644 --- a/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs +++ b/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs @@ -1,79 +1,109 @@ -using System; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Threading; -using log4net; -using log4net.Config; -using log4net.Repository; -using WireMock.RequestBuilders; -using WireMock.ResponseBuilders; -using WireMock.Server; -using WireMock.Settings; +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading; +using log4net; +using log4net.Config; +using log4net.Repository; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using WireMock.RequestBuilders; +using WireMock.ResponseBuilders; +using WireMock.Server; +using WireMock.Settings; using WireMock.Util; - -namespace WireMock.Net.StandAlone.NETCoreApp -{ - static class Program - { - private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); - // private static readonly ILog Log = LogManager.GetLogger(typeof(Program)); - - private static int sleepTime = 30000; - private static WireMockServer _server; - - static void Main(string[] args) - { - XmlConfigurator.Configure(LogRepository, new FileInfo("log4net.config")); - + +namespace WireMock.Net.StandAlone.NETCoreApp +{ + static class Program + { + private static readonly ILoggerRepository LogRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); + // private static readonly ILog Log = LogManager.GetLogger(typeof(Program)); + + private static int sleepTime = 30000; + private static WireMockServer _server; + + static void Main(string[] args) + { + XmlConfigurator.Configure(LogRepository, new FileInfo("log4net.config")); + if (!WireMockServerSettingsParser.TryParseArguments(args, out var settings, new WireMockLog4NetLogger())) { return; } - settings.Logger.Debug("WireMock.Net server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'"))); - - _server = WireMockServer.Start(settings); - + settings.Logger.Debug("WireMock.Net server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'"))); + + /* https://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-core */ + /* Enable Cors */ + var policyName = "MyPolicy"; + settings.AdditionalServiceRegistration = services => + { + services.AddCors(corsOptions => + corsOptions.AddPolicy(policyName, + corsPolicyBuilder => + { + corsPolicyBuilder + .AllowAnyHeader() + .AllowAnyMethod() + .AllowAnyOrigin(); + })); + + settings.Logger.Debug("Enable Cors"); + }; + + /* Use Cors */ + settings.PreWireMockMiddlewareInit = app => + { + var appBuilder = (IApplicationBuilder)app; + appBuilder.UseCors(policyName); + + settings.Logger.Debug("Use Cors"); + }; + + _server = WireMockServer.Start(settings); + _server.Given(Request.Create().WithPath("/api/sap") .UsingPost() - .WithBody((IBodyData xmlData) => { + .WithBody((IBodyData xmlData) => + { //xmlData is always null return true; })) .RespondWith(Response.Create().WithStatusCode(System.Net.HttpStatusCode.OK)); - _server - .Given(Request.Create() - .UsingAnyMethod()) - .RespondWith(Response.Create() - .WithTransformer() - .WithBody("{{Random Type=\"Integer\" Min=100 Max=999999}} {{DateTime.Now}} {{DateTime.Now \"yyyy-MMM\"}} {{String.Format (DateTime.Now) \"MMM-dd\"}}")); - - Console.WriteLine($"{DateTime.UtcNow} Press Ctrl+C to shut down"); - - Console.CancelKeyPress += (s, e) => - { - Stop("CancelKeyPress"); - }; - - System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += ctx => - { - Stop("AssemblyLoadContext.Default.Unloading"); - }; - - while (true) - { - Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server running : {_server.IsStarted}"); - Thread.Sleep(sleepTime); - } - } - - private static void Stop(string why) - { - Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopping because '{why}'"); - _server.Stop(); - Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopped"); - } - } -} \ No newline at end of file + _server + .Given(Request.Create() + .UsingAnyMethod()) + .RespondWith(Response.Create() + .WithTransformer() + .WithBody("{{Random Type=\"Integer\" Min=100 Max=999999}} {{DateTime.Now}} {{DateTime.Now \"yyyy-MMM\"}} {{String.Format (DateTime.Now) \"MMM-dd\"}}")); + + Console.WriteLine($"{DateTime.UtcNow} Press Ctrl+C to shut down"); + + Console.CancelKeyPress += (s, e) => + { + Stop("CancelKeyPress"); + }; + + System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += ctx => + { + Stop("AssemblyLoadContext.Default.Unloading"); + }; + + while (true) + { + Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server running : {_server.IsStarted}"); + Thread.Sleep(sleepTime); + } + } + + private static void Stop(string why) + { + Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopping because '{why}'"); + _server.Stop(); + Console.WriteLine($"{DateTime.UtcNow} WireMock.Net server stopped"); + } + } +} diff --git a/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs b/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs index 3d29486f..3992010f 100644 --- a/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs +++ b/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs @@ -1,6 +1,7 @@ #if USE_ASPNETCORE using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -8,10 +9,10 @@ using JetBrains.Annotations; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; +using Stef.Validation; using WireMock.Logging; using WireMock.Owin.Mappers; using WireMock.Util; -using Stef.Validation; namespace WireMock.Owin { @@ -54,7 +55,7 @@ namespace WireMock.Owin // a filesystem handler). if (string.IsNullOrEmpty(AppContext.BaseDirectory)) { - builder.UseContentRoot(System.IO.Directory.GetCurrentDirectory()); + builder.UseContentRoot(Directory.GetCurrentDirectory()); } _host = builder diff --git a/src/WireMock.Net/Owin/OwinSelfHost.cs b/src/WireMock.Net/Owin/OwinSelfHost.cs index 2d8df431..d0740a9f 100644 --- a/src/WireMock.Net/Owin/OwinSelfHost.cs +++ b/src/WireMock.Net/Owin/OwinSelfHost.cs @@ -46,10 +46,7 @@ namespace WireMock.Owin [PublicAPI] public Task StartAsync() { - return Task.Run(() => - { - StartServers(); - }, _cts.Token); + return Task.Run(StartServers, _cts.Token); } [PublicAPI]