Bruce Hunter - 323 - Feb 3, 2014

What is the best way to mimic the Error Response given by servicestack, from the Applicaiton_Error within the Global.asax when the ServiceStack.HostContext is unavailable.

Can I get there from starting off like so within the Applicaiton_Error().

Server.ClearError(); // this is the secret to not returning the CustomError HTML!!!         
var httpContextBase = HttpContext.Current.ToHttpContextBase();

You could serialize a generic ErrorResponse: https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Interfaces/ErrorResponse.cs

Bruce Hunter:

Agreed, I did that like so. 

var responseContent = new Exception(“Internal Server Error”).ToResponseStatus();

However, I find my self writting logic to determine the response format to return to the client by  doing responseContent. ToJson() or responseConent.ToXml().

Is there something exposed to easily get the return format expected ? I see in HttpRequestExtensions.cs , but need IRequest to get at it.

Bruce Hunter:

I’d like to call HttpRequestExtentions.GetResponseContentType

However, I would need to IRequest and ServiceStack.HostContext (<-- Instance available)

I could just mimic this function and remove what I don’t need to get close, just thought I would ask if there is an easier way or even this might be useful to add to ServiceStack.

You should be able to get an IRequest with the ToRequest() extension method, i.e:

var req = HttpContext.Current.ToRequest();

You can get to ServiceStack’s AppHost ContentType serializers with:

var str = HostContext.ContentTypes.SerializeToString(req, errorDto);

Bruce Hunter:

The above can’t work as in my situation the HostContext failed to Init(), but the site is still up and returning responses back to the client. 

[System.Configuration.ConfigurationErrorsException] = {“ServiceStack: AppHost does not exist or has not been initialized. Make sure you have created an AppHost and started it with ‘new AppHost().Init();’  in your Global.asax Application_Start() or alternative Application StartUp”}

If there was a generic way to get at this information from ServiceStack Api without the need for HostContext. It could benefit other developers.

you can create the instance yourself, e.g:

var contentTypes = new ContentTypes();

But it only comes with Xml, JSON, JSV and SOAP by default, you’d have to use the Register() api to register more contentTypes.

Otherwise you can create your own empty AppHost instance with:

using (var appHost = new BasicAppHost().Init()) 
{
  var a = appHost.ContentTypes…
}

Anyway I’m off to sleep now, 3am where I’m at. Will check back in tomorrow morning.

Bruce Hunter:

That’s what I was thinking of doing. Thanks for responding and gooo to bed! lol

Bruce Hunter:

Thanks! This is the solution.

            Server.ClearError(); // this is the secret to not returning the CustomError HTML!!!            
            
            HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.InternalServerError; // 500
            HttpContext.Current.Response.Clear(); 

            try
            {
                using (var tempAppHost = new BasicAppHost().Init())
                {
                    var req = HttpContext.Current.ToRequest();

                    var res = new ErrorResponse
                    {
                        ResponseStatus = new Exception(“Internal Server Error”).ToResponseStatus()
                    };

                    var i = req.ResponseContentType;

                    var result = tempAppHost.ContentTypes.SerializeToString(req, res);
                    HttpContext.Current.Response.Write(result);
                }             
            }

Bruce Hunter:

When there is a hard crash we do not want to send back the Exception information, just that there has been an error, but not what it actually was for security reasons. We log everything.

Thanks again.