Question

I have constructed a simple Rest service using ServiceStack (which is brilliant), that returns a list of key value pairs.

My service looks like this:

 public class ServiceListAll : RestServiceBase<ListAllResponse>
{
    public override object OnGet(ListAllResponse request)
    {          
        APIClient c = VenueServiceHelper.CheckAndGetClient(request.APIKey, VenueServiceHelper.Methods.ListDestinations);

        if (c == null)
        {
            return null;
        }
        else
        {
            if ((RequestContext.AbsoluteUri.Contains("counties")))
            {
                return General.GetListOfCounties();
            }

            else if ((RequestContext.AbsoluteUri.Contains("destinations")))
            {
                return General.GetListOfDestinations();
            }

            else
            {
                return null;
            }
        }
    }
}

my response looks like this:

    public class ListAllResponse
{
    public string County { get; set; }
    public string Destination { get; set; }
    public string APIKey { get; set; }     
}

and I have mapped the rest URL as follows:

.Add<ListAllResponse>("/destinations")
.Add<ListAllResponse>("/counties")

when calling the service

http://localhost:5000/counties/?apikey=xxx&format=xml

I receive this exception (a breakpoint in the first line of the service is not hit):

NullReferenceException Object reference not set to an instance of an object. at ServiceStack.Text.XmlSerializer.SerializeToStream(Object obj, Stream stream) at ServiceStack.Common.Web.HttpResponseFilter.<GetStreamSerializer>b_3(IRequestContext r, Object o, Stream s) at ServiceStack.Common.Web.HttpResponseFilter.<>c_DisplayClass1.<GetResponseSerializer>b__0(IRequestContext httpReq, Object dto, IHttpResponse httpRes) at ServiceStack.WebHost.Endpoints.Extensions.HttpResponseExtensions.WriteToResponse(IHttpResponse response, Object result, ResponseSerializerDelegate defaultAction, IRequestContext serializerCtx, Byte[] bodyPrefix, Byte[] bodySuffix)

The exception is thrown regardless of whether I include any parameters in call or not. I have also created a number of other services along the same lines in the same project which work fine. Can anyone point me in the right direction as to what this means?

Était-ce utile?

La solution

Your web service design is a little backwards, Your Request DTO should go on RestServiceBase<TRequest> not your response. And if you're creating a REST-ful service I recommend the name (i.e. Request DTO) of your service to be a noun, e.g. in this case maybe Codes.

Also I recommend having and using the same strong-typed Response for your service with the name following the convention of '{RequestDto}Response', e.g. CodesResponse.

Finally return an empty response instead of null so clients need only handle an empty result set not a null response.

Here's how I would re-write your service:

 [RestService("/codes/{Type}")]
 public class Codes {
      public string APIKey { get; set; }     
      public string Type { get; set; }
 }

 public class CodesResponse {
      public CodesResponse() {
           Results = new List<string>();
      }

      public List<string> Results { get; set; }
 }

 public class CodesService : RestServiceBase<Codes>
 {
      public override object OnGet(Codes request)
      {          
           APIClient c = VenueServiceHelper.CheckAndGetClient(request.APIKey, 
              VenueServiceHelper.Methods.ListDestinations);

           var response = new CodesResponse();
           if (c == null) return response;

           if (request.Type == "counties") 
                response.Results = General.GetListOfCounties();
           else if (request.Type == "destinations") 
                response.Results = General.GetListOfDestinations();

           return response; 
     }
 }

You can either use the [RestService] attribute or the following route (which does the same thing):

Routes.Add<Codes>("/codes/{Type}");

Which will allow you to call the service like so:

http://localhost:5000/codes/counties?apikey=xxx&format=xml
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top