为什么我的ServiceStack服务抛出异常



我使用ServiceStack构建了一个简单的Rest服务(非常出色),它返回键值对的列表。

我的服务是这样的:

 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;
            }
        }
    }
}

我的回答是这样的:

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

我已经将其余的URL映射如下:

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

当调用服务时

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

我收到这个异常(服务第一行的断点没有命中):

NullReferenceException对象引用未设置为对象的实例。ServiceStack.Common.Web.HttpResponseFilter的ServiceStack.Text.XmlSerialize.SerializeToStream(对象对象,流流)。<GetStreamSerializer>b_3(IRequestContext r,Object o,Stream s),位于ServiceStack.Common.Web.HttpResponseFilter。<gt;c_DisplayClass1.<GetResponseSerializer>ServiceStack.WebHost.Endpoints.Extensions.HttpResponseExtensions.WriteToResponse处的b__0(IRequestContext httpReq,Object dto,IHttpResponse httpRes)

无论我是否在调用中包含任何参数,都会抛出异常。我还在同一个项目中创建了许多其他类似的服务,这些服务运行良好。有人能给我指出正确的方向吗?这意味着什么?

您的web服务设计有点落后,您的请求DTO应该在RestServiceBase<TRequest>上运行,而不是您的响应。如果你正在创建一个RESTful服务,我建议你的服务的名称(即RequestDTO)是一个名词,例如在这种情况下可能是Codes。

此外,我建议为您的服务使用相同的强类型响应,其名称遵循约定"{RequestDto}Response',例如CodesResponse。

最后返回一个空响应而不是null,因此客户端只需要处理一个空结果集,而不需要处理null响应。

以下是我如何改写您的服务:

 [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; 
     }
 }

您可以使用[RestService]属性或以下路由(做同样的事情):

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

这将允许您这样呼叫服务:

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

最新更新