Carlos Mendes - 383 - Nov 6, 2014

I have a UsersService that uses the ToOptimizedResultUsingCache. 

When I call it using the ContentType ‘application/json’ it works fine.

But when I resolve it inside a page:

@{
    var svc = ResolveService<UsersService>();
    var response =  (GetUserResponse) svc.Get(new GetUser {Id = 1});
}

I always get an error when returning the result.

I think the problem is related to the ContentType which is automatically set to 'text/html’

I tried decorating my service with the following attribute: 

[AddHeader(ContentType = “application/json”)]

Even so the compressed result is ‘text/html’ and I don’t have a razor view for the UsersService.

Right if you use ToOptimizedResultUsingCache() it returns the most optimized result for that request which can be deflated html. I recommend providing an API that gets the result without cacning, e.g:

 var response =  (GetUserResponse) svc.GetUser(1);

Carlos Mendes:

So I can’t use the ToOptimizedResultUsingCache in this case? 

Should I handle the caching manually?

Because you want access to the intermediate response DTO and not the compressed serialized bytes response, you should refactor it out into another method (i.e. which doesn’t use ToOptimizedResultUsingCache()) and call that instead.