Created
October 1, 2011 14:57
-
-
Save johncoder/1256144 to your computer and use it in GitHub Desktop.
Nancy MaxJsonLength example, behaves strangely if exceeded.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ServiceModel; | |
using System.ServiceModel.Web; | |
using Nancy; | |
using Nancy.Hosting.Wcf; | |
using Nancy.Responses; | |
namespace NancyMaxJsonLengthSandbox | |
{ | |
class Program | |
{ | |
private static WebServiceHost _nancyHost; | |
public static Uri _baseUri = new Uri("http://localhost:8585/"); | |
// NuGet Packages: | |
// - Nancy | |
// - Nancy.Hosting.Wcf | |
static void Main(string[] args) | |
{ | |
var service = new NancyWcfGenericService(); | |
_nancyHost = new WebServiceHost(service, _baseUri); | |
_nancyHost.AddServiceEndpoint(typeof(NancyWcfGenericService), new WebHttpBinding(), ""); | |
// this can fail if there isn't a urlacl for the desired port | |
// if it fails, open a cmd with "run as administrator" and run: | |
// netsh http add urlacl url=http://+:8585/ user=\Everyone | |
_nancyHost.Open(); | |
Console.WriteLine("Nancy: Listening to requests on {0}", _baseUri); | |
Console.ReadKey(); | |
_nancyHost.Close(); | |
} | |
} | |
public class MainModule : NancyModule | |
{ | |
private static int _defaultJsonMaxLength; | |
public MainModule() | |
{ | |
if (_defaultJsonMaxLength == 0) | |
_defaultJsonMaxLength = JsonSettings.MaxJsonLength; | |
Get["/"] = parameters => | |
{ | |
// A small json response, works perfectly. | |
return Response.AsJson(new { value = "Hello, world!" }); | |
}; | |
Get["/fails"] = parameters => | |
{ | |
// using the default MaxJsonLength, this response will return a 504 (indicated by fiddler) | |
// to the client and there's no real debugging hooks or ways to figure out | |
// what's going on in Nancy since this method returns without a problem. | |
JsonSettings.MaxJsonLength = _defaultJsonMaxLength; | |
return Response.AsJson(new { value = new string[111000] }); | |
}; | |
Get["/succeeds"] = parameters => | |
{ | |
// increasing the MaxJsonLength allows this same size object to be returned to the client, | |
// indicating that something else is happening in the Nancy pipeline. | |
// exceeding the MaxJsonLength is an understandable problem, but it would be nice | |
// to have a nicer indication of this problem. | |
JsonSettings.MaxJsonLength = 2000000; | |
return Response.AsJson(new { value = new string[111000] }); | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment