Skip to content

Instantly share code, notes, and snippets.

@JoeStead
Created November 2, 2017 10:12
Show Gist options
  • Save JoeStead/520795a879cd60132a76daf2e854c607 to your computer and use it in GitHub Desktop.
Save JoeStead/520795a879cd60132a76daf2e854c607 to your computer and use it in GitHub Desktop.
Nancy Not Found Response
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Logging;
using Nancy;
using Nancy.Bootstrapper;
using Nancy.Owin;
using Nancy.TinyIoc;
namespace TestNancyNotFound
{
class Program
{
static void Main(string[] args)
{
var location = typeof(Program).GetTypeInfo().Assembly.Location;
var certificate = new X509Certificate2(Path.Combine(Path.GetDirectoryName(location), "Cert/cert.pfx"), "password1");
var url = $"https://+:5001";
var builder = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseUrls(url)
.UseStartup<Startup>();
builder.UseKestrel(options =>
{
options.ThreadCount = Environment.ProcessorCount;
options.UseHttps(certificate);
});
var host = builder.Build();
host.Run();
}
}
public class Startup
{
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
app.Use(async (context, next) =>
{
var sw = new Stopwatch();
sw.Start();
var requestId = context.Request.Headers["TracingId"];
requestId = requestId.FirstOrDefault();
Console.WriteLine($"Starting {context.Request.Method} request {context.Request.GetDisplayUrl()} tracingid {requestId}");
await next();
sw.Stop();
Console.WriteLine($"Finished {context.Request.Method} request {context.Request.GetDisplayUrl()} in {sw.ElapsedMilliseconds} ms tracingid {requestId}");
});
app.UseOwin(x => x.UseNancy(options => { options.Bootstrapper = new MyDefaultBootStrapper(); }));
}
}
public class MyDefaultBootStrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
pipelines.BeforeRequest += (ctx) =>
{
var requestId = ctx.Request.Headers["TracingId"].FirstOrDefault();
Console.WriteLine($"Receiving {ctx.Request.Method} for {ctx.Request.Url} with TracingId {requestId}");
return null;
};
pipelines.AfterRequest += (ctx) =>
{
var requestId = ctx.Request.Headers["TracingId"].FirstOrDefault();
if (ctx.Response.ContentType.Contains("xml"))
{
ctx.Response.ContentType = "text/xml";
}
Console.WriteLine($"Finished receiving {ctx.Request.Method} for {ctx.Request.Url} with TracingId {requestId}");
};
}
}
public class AModule : NancyModule
{
public AModule() : base("/test")
{
this.Get("/thing/{id:guid}", x => { return HttpStatusCode.NotFound; });
}
}
}
curl -k -H "Accept: application/xml" -H "Content-Type: application/xml" -i https://localhost:5001/test/thing/9e069857-2b29-4a64-ac1c-492f81212a80
> HTTP/1.1 404 Not Found
> Date: Thu, 02 Nov 2017 10:08:44 GMT
> Content-Type: application/xml
> Server: Kestrel
> Transfer-Encoding: chunked
> Vary: Accept
Nancy.ErrorHandling.DefaultStatusCodeHandler.DefaultStatusCodeHandlerResult cannot be serialized because it does not have a parameterless constructor.%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment