private static void Main(string[] args) { const string url = "http://localhost:4545"; var exitEvent = new ManualResetEvent(false); // Hook up the Ctrl+C to exit the application System.Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; exitEvent.Set(); }; // Get NanoConfiguration used by the demo projects. Replace this with your own code. var config = NanoConfigurationHelper.GetNanoConfiguration(); // Specify your application name. A reasonable default is automatically used if not // supplied but it's definitely recommended to supply one. config.ApplicationName = "Nano.Demo.SelfHost.Console"; using (var server = HttpListenerNanoServer.Start(config, url)) { if (Debugger.IsAttached) { Process.Start(url + "/ApiExplorer/"); } System.Console.WriteLine("Nano Server is running on: " + url); System.Console.WriteLine("Press Ctrl+C to exit."); System.Console.WriteLine(config.ToString()); exitEvent.WaitOne(); } }
/// <summary> /// Creates an <see cref="HttpListenerNanoServer"/> with defaults to help write small unit tests. /// </summary> /// <returns></returns> public static HttpListenerNanoServer Start() { var nanoConfiguration = new NanoConfiguration(); const string url = "http://localhost:4545/"; return(HttpListenerNanoServer.Start(nanoConfiguration, url)); }
public void SimpleSpeedTest(int requestCount) { ServicePointManager.UseNagleAlgorithm = false; ServicePointManager.DefaultConnectionLimit = int.MaxValue; var nanoConfiguration = new NanoConfiguration(); //nanoConfiguration.AddDirectory( "/", "www" ); nanoConfiguration.AddFile("/", "www\\index.html"); using (HttpListenerNanoServer.Start(nanoConfiguration, "http://localhost:4545")) { var stopwatch = Stopwatch.StartNew(); Parallel.For(0, requestCount, i => { using (var client = new WebClient()) { byte[] responsebytes = client.DownloadData("http://localhost:4545/"); string responsebody = Encoding.UTF8.GetString(responsebytes); if (requestCount == 1) { Trace.WriteLine(responsebody); } } }); var elapsedTime = stopwatch.Elapsed; Trace.WriteLine(string.Format("{0} requests completed in {1}", requestCount, elapsedTime.GetFormattedTime())); var averageRequestTimeInMilliseconds = elapsedTime.TotalMilliseconds / requestCount; var averageRequestTimeSpan = TimeSpan.FromTicks((long)(TimeSpan.TicksPerMillisecond * averageRequestTimeInMilliseconds)); Trace.WriteLine(string.Format("Average request time: {0}", averageRequestTimeSpan.GetFormattedTime())); } }
public void Start(string urls) { var validatedUrls = ValidateUrls(urls); // Get NanoConfiguration used by the demo projects. Replace this with your own code. var config = NanoConfigurationHelper.GetNanoConfiguration(); // Specify your application name. A reasonable default is automatically used if not // supplied but it's definitely recommended to supply one. config.ApplicationName = "Nano.Demo.SelfHost.Topshelf"; // Start the server passing in the NanoConfiguration and a list of URLs to listen on. _server = HttpListenerNanoServer.Start(config, validatedUrls); // Optionally specify a virtual application path if your application URL is going to use one. // Example: http://localhost:4545/ExecutiveDashboard/ _server.HttpListenerConfiguration.ApplicationPath = ""; string url = _server.HttpListenerConfiguration.GetFirstUrlBeingListenedOn(); // If the debugger is attached go ahead and launch a browser window directly to the Api Explorer page. if (Debugger.IsAttached) { Process.Start(url + "ApiExplorer"); } Console.WriteLine("Nano Server is running on: " + url); Console.WriteLine(config.ToString()); }
public void Start() { using (HttpListenerNanoServer.Start(nano, baseUrl)) { Colorizer.WriteLine($"[DarkYellow!Web Server Listening on {baseUrl}]"); Process.Start(baseUrl); Startup.Exit.WaitOne(); } }
public static void Start(string url) { var nanoConfiguration = new NanoConfiguration(); nanoConfiguration.AddMethods <Beatles>(); using (HttpListenerNanoServer.Start(nanoConfiguration, url)) { } }
/// <summary> /// Creates an <see cref="HttpListenerNanoServer"/> with defaults to help write small unit tests. /// </summary> /// <returns></returns> public static HttpListenerNanoServer Start() { var nanoConfiguration = new NanoConfiguration(); (( JsonNetSerializer )nanoConfiguration.SerializationService).JsonSerializerSettings.Formatting = Formatting.None; const string url = "http://localhost:4545/"; var server = HttpListenerNanoServer.Start(nanoConfiguration, url); return(server); }
public void Test_Proxy_Generated_Code() { var config = new NanoConfiguration(); config.AddMethods <Customer>("/api/customer/"); using (HttpListenerNanoServer.Start(config, ApiProxy.BaseApiUrl)) { Trace.WriteLine("CreateCustomer"); var createCustomerResponse = ApiProxy.CreateCustomer("Clark", "Kent"); Trace.WriteLine(JsonConvert.SerializeObject(createCustomerResponse)); Assert.That(createCustomerResponse.FirstName == "Clark" && createCustomerResponse.LastName == "Kent"); Trace.WriteLine(""); Trace.WriteLine("GetPerson"); var getPersonResponse = ApiProxy.GetPerson(123); Trace.WriteLine(JsonConvert.SerializeObject(getPersonResponse)); Assert.That(getPersonResponse.FirstName == "Clark" && getPersonResponse.LastName == "Kent" && getPersonResponse.Addresses.Count == 2); Trace.WriteLine(""); Trace.WriteLine("GetCustomer"); dynamic getCustomerResponse = ApiProxy.GetCustomer(123); string getCustomerResponseJson = JsonConvert.SerializeObject(getCustomerResponse); Trace.WriteLine(getCustomerResponseJson); Assert.That(getCustomerResponse.FirstName == "Clark" && getCustomerResponse.LastName == "Kent"); Trace.WriteLine(""); Trace.WriteLine("GetContext"); dynamic getContextResponse = ApiProxy.GetContext(); string getContextResponseJson = JsonConvert.SerializeObject(getContextResponse); Trace.WriteLine(getContextResponseJson); Assert.That(getContextResponse.Url != null && getContextResponse.HttpMethod == "POST"); Trace.WriteLine(""); Trace.WriteLine("GetCorrelationId"); var correlationId = Guid.NewGuid().ToString(); string getCorrelationIdResponse = ApiProxy.GetCorrelationId(correlationId); Trace.WriteLine(getCorrelationIdResponse); Assert.That(correlationId == getCorrelationIdResponse); Trace.WriteLine(""); Trace.WriteLine("CreatePendingCustomer"); var createPendingCustomerResponse = ApiProxy.CreatePendingCustomer("Clark", "Kent"); Trace.WriteLine(JsonConvert.SerializeObject(createPendingCustomerResponse)); Assert.That(createPendingCustomerResponse.FirstName == "Clark" && createPendingCustomerResponse.LastName == "Kent"); Trace.WriteLine(""); Trace.WriteLine("CreateDynamicCustomer"); dynamic createDynamicCustomerResponse = ApiProxy.CreateDynamicCustomer(new { CustomerId = 1, FirstName = "Clark", LastName = "Kent" }); string createDynamicCustomerResponseJson = JsonConvert.SerializeObject(createDynamicCustomerResponse); Trace.WriteLine(createDynamicCustomerResponseJson); Assert.That(createDynamicCustomerResponse.FirstName == "Clark" && createDynamicCustomerResponse.LastName == "Kent"); Trace.WriteLine(""); } }
private static void Main() { OutputTitle(); InitLogger(); var exit = CreateExitEvent(); var baseUrl = $"http://{Config.Domain}:{Config.Port}"; var nano = new NanoConfiguration { ApplicationName = "MiniWebServer", EnableVerboseErrors = true }; // logging nano.GlobalEventHandler.PostInvokeHandlers.Add(context => { var level = context.Response.HttpStatusCode == 200 ? LogEventLevel.Information : LogEventLevel.Warning; var address = context.Request.Url.ToString().Replace(baseUrl, "/").Replace("//", "/"); var statusName = Enum.GetName(typeof(Constants.HttpStatusCode), context.Response.HttpStatusCode); Log.Write(level, "{address} => {HttpStatusCode} {statusName}", address, context.Response.HttpStatusCode, statusName); }); nano.GlobalEventHandler.UnhandledExceptionHandlers.Add((exception, context) => { var address = context.Request.Url.ToString().Replace(baseUrl, "/").Replace("//", "/"); Log.Error(exception, "{address} => Exception: {Message}", address, exception.Message); }); // pulse var startTime = DateTime.Now; nano.AddBackgroundTask("Uptime", (int)TimeSpan.FromMinutes(1).TotalMilliseconds, () => { var uptime = DateTime.Now - startTime; Log.Information("Uptime {uptime}", uptime); return(uptime); }); // hosting HttpHost.Init(nano, Config.WebRoot); ApiHost.Init(nano, Config.WebRoot); nano.DisableCorrelationId(); nano.EnableCors(); // start server using (var server = HttpListenerNanoServer.Start(nano, baseUrl)) { Log.Information("Listening on {url}", baseUrl); Log.Information("Press Ctrl+C to exit."); Process.Start(File.Exists($"{Environment.CurrentDirectory}\\index.html") ? baseUrl : $"{baseUrl}/ApiExplorer"); exit.WaitOne(); } }
public void Can_Handle_Void_Methods() { // Arrange var config = new NanoConfiguration(); config.AddMethods <Customer2>(); using (HttpListenerNanoServer.Start(config, ApiProxy.Configuration.BaseApiUrl)) { // Act and Assert ApiProxy.Customer2.DoNothing(); // Just ensure we don't blow up } }
public void Start(string urls) { var validatedUrls = ValidateUrls(urls); var config = new NanoConfiguration(); // When the Debugger is attached, map two folders up so that you can live edit files in Visual Studio without having to restart // your application to get the files copied to your bin directory. config.AddDirectory("/", Debugger.IsAttached ? "../../www" : "www", returnHttp404WhenFileWasNotFound: true); config.AddMethods <Customer>("/api/customer/"); config.AddFunc("/hi", context => "Hello World!"); config.GlobalEventHandler.UnhandledExceptionHandlers.Add((exception, context) => { try { if (!EventLog.SourceExists(_applicationName)) { EventLog.CreateEventSource(_applicationName, "Application"); } var msg = new StringBuilder() .AppendLine("Nano Error:") .AppendLine("-----------").AppendLine() .AppendLine("URL: " + context.Request.Url).AppendLine() .AppendLine("Message: " + exception.Message).AppendLine() .AppendLine("StackTrace:") .AppendLine(exception.StackTrace) .ToString(); EventLog.WriteEntry(_applicationName, msg, EventLogEntryType.Error); } catch (Exception) { // Gulp: Never throw an exception in the unhandled exception handler } }); _server = HttpListenerNanoServer.Start(config, validatedUrls); _server.HttpListenerConfiguration.ApplicationPath = "YourOptionalVirtualAppPathName"; if (Debugger.IsAttached) { Process.Start(_server.HttpListenerConfiguration.GetFirstUrlBeingListenedOn().TrimEnd('/') + "/ApiExplorer"); } Console.WriteLine("Nano Server is running on: " + _server.HttpListenerConfiguration.GetFirstUrlBeingListenedOn()); }
public static void Start(string url, string applicationName) { // Get NanoConfiguration used by the demo projects. Replace this with your own code. var config = NanoConfigurationHelper.GetNanoConfiguration(); // Specify your application name. A reasonable default is automatically used if not // supplied but it's definitely recommended to supply one. config.ApplicationName = "Nano.Demo.SelfHost.WindowsService"; _server = HttpListenerNanoServer.Start(config, url); url = _server.HttpListenerConfiguration.GetFirstUrlBeingListenedOn(); if (Debugger.IsAttached) { Process.Start(url + "ApiExplorer/"); } Console.WriteLine("Nano Server is running on: " + url); }
public void DoItToIt() { var nanoConfiguration = new NanoConfiguration(); nanoConfiguration.AddMethods <Customer>(); using (HttpListenerNanoServer.Start(nanoConfiguration, "http://localhost:4545")) { var parameters = new NameValueCollection { { "customerNbr", "1" } }; using (var client = new WebClient()) { byte[] responsebytes = client.UploadValues("http://localhost:4545/api/Customer/GetCustomer", "POST", parameters); string responsebody = Encoding.UTF8.GetString(responsebytes); Trace.WriteLine(responsebody); } } }
public void SimpleSpeedTest(int requestCount) { ServicePointManager.UseNagleAlgorithm = false; ServicePointManager.DefaultConnectionLimit = int.MaxValue; var nanoConfiguration = new NanoConfiguration(); nanoConfiguration.AddFunc("/Customer/CreateCustomer", context => new { CustomerId = 1, FirstName = context.Request.GetRequestParameterValue("firstName"), LastName = context.Request.GetRequestParameterValue("lastName") }); using (HttpListenerNanoServer.Start(nanoConfiguration, "http://localhost:4545")) { var parameters = new NameValueCollection { { "firstName", "Clark" }, { "lastName", "Kent" } }; var stopwatch = Stopwatch.StartNew(); Parallel.For(0, requestCount, i => { using (var client = new WebClient()) { byte[] responsebytes = client.UploadValues("http://localhost:4545/Customer/CreateCustomer", "POST", parameters); string responsebody = Encoding.UTF8.GetString(responsebytes); if (requestCount == 1) { Trace.WriteLine(responsebody); } } }); var elapsedTime = stopwatch.Elapsed; Trace.WriteLine(string.Format("{0} requests completed in {1}", requestCount, elapsedTime.GetFormattedTime())); var averageRequestTimeInMilliseconds = elapsedTime.TotalMilliseconds / requestCount; var averageRequestTimeSpan = TimeSpan.FromTicks((long)(TimeSpan.TicksPerMillisecond * averageRequestTimeInMilliseconds)); Trace.WriteLine(string.Format("Average request time: {0}", averageRequestTimeSpan.GetFormattedTime())); } }
private static void Main(string[] args) { const string url = "http://localhost:4545"; var exitEvent = new ManualResetEvent(false); Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; exitEvent.Set(); }; var config = new NanoConfiguration(); // When the Debugger is attached, map two folders up so that you can live edit files in Visual Studio without having to restart // your application to get the files copied to your bin directory. config.AddDirectory("/", Debugger.IsAttached ? "../../www" : "www", returnHttp404WhenFileWasNotFound: true); config.AddMethods <Customer>("/api/customer/"); config.AddFunc("/hi", context => "Hello World!"); config.AddBackgroundTask("Test", 30000, () => { string result = "Hi, the time is now: " + DateTime.Now; Console.WriteLine(result); return(result); }); using (HttpListenerNanoServer.Start(config, url)) { if (Debugger.IsAttached) { Process.Start(url + "/ApiExplorer/"); } Console.WriteLine("Nano Server is running on: " + url); Console.WriteLine("Press Ctrl+C to exit."); Console.WriteLine(config.ToString()); exitEvent.WaitOne(); } }
public void Start(string url) { #region NanoSetup var exitEvent = new ManualResetEvent(false); Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; exitEvent.Set(); }; var validatedUrls = ValidateUrls(url); var config = new NanoConfiguration(); config.EnableCorrelationId(); config.AddDirectory("/", Debugger.IsAttached ? "../../www" : "www", returnHttp404WhenFileWasNotFound: true); #endregion //you will need to provide a connection string in your app.config for this to produce any events. //constructor will set default hardcoded values, you can then override any of those values here ( below values listed are the default values ) var spokeConfig = new Spoke.SpokeConfiguration { DefaultAbortAfterMinutes = 60, LiveRetryAbortAfterMinutes = 60, EventProcessingMutexTimeToLiveMinutes = 2, EventSubscriptionMutexTimeToLiveMinutes = 2, FailedEventsLookbackMinutes = 1440, FailedEventsThresholdMinutes = 60, FailedNotificationsThresholdMinutes = 60, MutexAcquisitionWaitTime = 1, ClockBackfillTotalMinutes = 10, ClockBackfillOffsetMinutes = 2, ClockBackfillMutexTimeToLiveMinutes = 2, ClockSleepMilliseconds = 10000, SendClockMessages = true, AppName = $"{Environment.MachineName}-{AppDomain.CurrentDomain.FriendlyName}", UserName = WindowsIdentity.GetCurrent()?.Name, GetApiUri = x => x.Uri, WasApiCallSuccessfulHandlers = new Dictionary <string, Func <Spoke.Models.WasApiCallSuccessfulInput, bool> > { { "DEFAULT", response => response.HttpResponse.Exception == null && response.HttpResponse.Response != null && response.HttpResponse.Response.StatusCode == HttpStatusCode.OK } }, JsonSerializer = new Spoke.Utils.JsonSerializer(), Database = () => new Spoke.DatabaseIO.SpokeSqlDatabase(), DatabaseConnectionString = () => ConfigurationManager.ConnectionStrings["spoke"].ConnectionString, SchemaName = "Spoke" }; //you can add support for "service types" by adding WasApiCallSuccessfulHandlers instead of the default. spokeConfig.WasApiCallSuccessfulHandlers.Add( "OTHER_SERVICE_TYPE", //the key for the new service type response => response.HttpResponse.Exception == null //func to evaluate if the service call was successful or not. ); var spoke = new Spoke(spokeConfig); spoke.Start(); config.AddMethods <Spoke.ExternalApi>("/Api/Events"); config.AddMethods <Spoke.InternalApi>("/Api/Events/Internal"); #region NanoSetup config.AddBackgroundTask("GCCollect", 30000, () => { GC.Collect(); return(null); }); config.GlobalEventHandler.UnhandledExceptionHandlers.Add((exception, context) => { try { if (!EventLog.SourceExists(_applicationName)) { EventLog.CreateEventSource(_applicationName, "Application"); } var msg = new StringBuilder() .AppendLine("Nano Error:") .AppendLine("-----------").AppendLine() .AppendLine("URL: " + context.Request.Url).AppendLine() .AppendLine("Message: " + exception.Message).AppendLine() .AppendLine("StackTrace:") .AppendLine(exception.StackTrace) .ToString(); EventLog.WriteEntry(_applicationName, msg, EventLogEntryType.Error); } catch (Exception) { // Gulp: Never throw an exception in the unhandled exception handler } }); _server = HttpListenerNanoServer.Start(config, validatedUrls); if (Debugger.IsAttached) { Process.Start(_server.HttpListenerConfiguration.GetFirstUrlBeingListenedOn().TrimEnd('/') + "/ApiExplorer/"); } Console.WriteLine("Nano Server is running on: " + _server.HttpListenerConfiguration.GetFirstUrlBeingListenedOn()); #endregion }