gregpakes
gregpakes

Reputation: 4535

Using ServiceStack.Razor I am getting IndexOutOfRangeException

I am trying to get ServiceStack.Razor to work but I am getting an IndexOutOfRangeException. The stacktrace is enclosed below:

at ServiceStack.Text.Jsv.JsvTypeSerializer.EatMapKey(String value, Int32& i) 
at ServiceStack.Text.Common.DeserializeDictionary`1.ParseStringDictionary(String value)
at ServiceStack.Html.ViewDataDictionary.PopulateModelState() 
at ServiceStack.Razor.ViewPage`1.Init(IRazorViewEngine viewEngine, ViewDataDictionary viewData, IHttpRequest httpReq, IHttpResponse httpRes) 
at ServiceStack.Razor.Templating.TemplateService.InitTemplate[T](T model, ITemplate instance, IHttpRequest httpReq, IHttpResponse httpRes) 
at ServiceStack.Razor.Templating.TemplateService.ExecuteTemplate[T](T model, String name, String defaultTemplatePath, IHttpRequest httpReq, IHttpResponse httpRes) 
at ServiceStack.Razor.RazorFormat.ProcessRazorPage(IHttpRequest httpReq, ViewPageRef razorPage, Object dto, IHttpResponse httpRes) 
at ServiceStack.Razor.RazorFormat.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, Object dto) 
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate) 
at ServiceStack.WebHost.Endpoints.Formats.HtmlFormat.SerializeToStream(IRequestContext requestContext, Object response, IHttpResponse httpRes) 
at ServiceStack.WebHost.Endpoints.Extensions.HttpResponseExtensions.WriteToResponse(IHttpResponse response, Object result, ResponseSerializerDelegate defaultAction, IRequestContext serializerCtx, Byte[] bodyPrefix, Byte[] bodySuffix)

Setup

Error.cs

public class Error
{
    public int TrackingNumber { get; set; }

    public string ClientName { get; set; }
}

[Route("/Errors", "GET")]
[Authenticate]
public class Errors : List<Error>
{

}

service.cs

public class ErrorService : Service
{
    public object Get(Errors request)
    {
        var fakeError = new Error(){ TrackingNumber=1, ClientName="Test"};
        var fakeErrorList = new Errors();
        fakeErrorList.Add(fakeError);
        return fakeErrorList;
    }
}

errors.cshtml

@inherits ViewPage<Errors>

<div>
    @Model.Count
</div>

Upvotes: 2

Views: 190

Answers (1)

paaschpa
paaschpa

Reputation: 4816

So, the only explanation I have for the error is the process that deserializes the Errors class to the View doesn't like that you're inheriting from List. You should be able to inherit from IList but then you would need to implement the entire interface. Inheriting from List doesn't seem to be a good idea according to this and this. The simplest solution I can think of if you want to inherit from List is to convert it to an Array in your Service and have your view inherit from Error[].

public class ErrorService : Service
{
    public object Get(Errors request)
    {
        var fakeError = new Error(){ TrackingNumber=1, ClientName="Test"};
        var fakeErrorList = new Errors();
        fakeErrorList.Add(fakeError);
        return fakeErrorList.ToArray();
    }
}

errors.cshtml

@inherits ViewPage<Error[]>

<div>
    @Model.Count()
</div>

Upvotes: 1

Related Questions