I am building my ServiceStack 4.0.8 service using Visual Studio. On Windows everything works perfectly, but when I try to run on Mono 2.10.8.1 / Ubuntu 13.10 with NGINX 1.4.1 an
A commit to ServiceStack has been made, and version 4.0.16+ should no longer suffer from this exception.
ToOptimizedResult()
works, this is not a shortcoming of ServiceStack but rather with Mono and fastcgi-server-4. There is a suitable workaround.
System.Web.HttpContextWrapper.GetService
:This exception is thrown because the Mono project have not yet implemented this method. As Mono is an OpenSource project they have to recreate the .NET specification themselves, and this method simply isn't done.
See here for the relevant Mono source code:
public class HttpContextWrapper : HttpContextBase
{
...
[MonoTODO]
public override object GetService (Type serviceType)
{
throw new NotImplementedException ();
}
}
I run ServiceStack on Mono (albeit v3.2.6) and don't have any problem using ToOptimizedResult
but I don't use fastcgi-server4
, which will relies on System.Web.HttpContextWrapper
.
Instead I use a self hosted ServiceStack application, which is based on a pool of underlying System.Net.HttpListener
, which doesn't seem to be affected by the same issue. And thus works well.
Below is code for a working self-hosted ServiceStack application, using your test code:
using System;
using ServiceStack;
using System.Collections.Generic;
namespace Testv4
{
class MainClass
{
public static void Main()
{
// Very basic console host
var appHost = new AppHost(500);
appHost.Init();
appHost.Start("http://*:8082/");
Console.ReadKey();
}
}
public class AppHost : AppHostHttpListenerPoolBase
{
public AppHost(int poolSize) : base("Test Service", poolSize, typeof(TestApp).Assembly) {}
public override void Configure(Funq.Container container)
{
}
}
public static class TestApp
{
public class DataItem
{
public string Data { get; set; }
}
[Route("/Test", "GET")]
public class TestRequest {}
public class TestController : Service
{
public object Get(TestRequest request)
{
var list = new List {
new DataItem { Data = "Fake Data" }
};
return base.Request.ToOptimizedResult(list);
}
}
}
}
You can test the application by going to localhost:8082/Test
. It shouldn't throw an exception when compressing the result.
My suggestion would be to either forward requests from NGINX using it's transparent proxying abilities onto the ServiceStack application, or simply cut out NGINX and have request go directly to the application.
The AppHostHttpListenerPoolBase
works well and I have had no issue using it in production environments.