2012-03-08 14 views
3

ServiceStack(これはすばらしいです)を使用して簡単なRestサービスを構築しました。これは、キー値のペアのリストを返します。私のServiceStackサービスが例外をスローするのはなぜですか?

私のサービスは、次のようになります。

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

Iは、(サービスの最初の行にブレークポイントがヒットされていない)は、この例外を受け取る:オブジェクトのインスタンスに設定されていない

とNullReferenceExceptionオブジェクト参照。 (ServiceStack.Common.Web.HttpResponseFilterのServiceStack.Text.XmlSerializer.SerializeToStream(Object obj、ストリームストリーム))。 <GetStreamSerializer> b_ ServiceStack.Common.Web.HttpResponseFilterの3(IRequestContext r、Object o、Stream s)。 < >c _DisplayClass1。 < ServiceStack.WebHost.Endpoints.Extensions.HttpResponseExtensions.WriteToResponseでGetResponseSerializer > b__0(IRequestContext httpReq、オブジェクトDTO、IHttpResponse httpRes)(IHttpResponse応答、オブジェクト結果、ResponseSerializerDelegate DEFAULTACTION、IRequestContext serializerCtx、バイト[] bodyPrefix、バイト[] bodySuffix)

例外は関係なく、私は任意の呼び出しのパラメータが含まれているか否かのスローされます。私は同じプロジェクトで同じ行に沿っていくつかの他のサービスを作成しています。誰かがこれが意味することについて正しい方向に私を向けることができますか?

答えて

7

ウェブサービスのデザインが少し後ろです。リクエストDTOは、あなたの回答ではなく、RestServiceBase<TRequest>になるはずです。また、RESTフルサービスを作成している場合は、サービス名(例:DTOリクエスト)を名詞にすることをおすすめします。この場合は多分コード。

また、「{RequestDto}レスポンス」の慣習に従った名前を使用してサービスに同じ強固なレスポンスを使用することをおすすめします。コードレスポンス。

最後に、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 
+0

本当にありがとう、私は本当に努力を感謝します。私は実際に自分自身でサービスを書き直しました。私はあなたが提案したラインに沿って広範にしたと言ってうれしく思います。また、私の回答にResponseStatusを含めました。あなたの答えはhttp://stackoverflow.com/q/4692313/20048です – Simon

関連する問題