2014-03-01 54 views
0

這裏:Recommended ServiceStack API Structure這裏:https://github.com/ServiceStack/ServiceStack/wiki/Physical-project-structure是如何構建項目C#的客戶重用DTO的建議。ServiceStack C#強類型的客戶端DTO

顯然,這是通過包括DTO組件的DLL來完成。我在網上搜索了一個例子,就是Hello World,它爲ServiceStack中的C#客戶端使用單獨的程序集DTO。也許我應該能夠自己打破這一點,但迄今爲止它並沒有證明那麼容易。

幾乎所有客戶的描述是通用的,非類型化JSON或其他基於非DTO客戶。沒有人像我一樣對類型化的C#客戶端感興趣(甚至是我找到的ServiceStack文檔)。所以我認爲即使最終我自己弄清楚這也是一個很好的問題。

要清楚,我已經構建並運行了Hello World示例服務器。我還使用瀏覽器連接到服務器並與之交互。我還創建了一個客戶端空項目,可以調用

JsonServiceClient client = new JsonServiceClient(myURL);

然後我試圖在我的DTO定義複製未經大會DLL,因爲我沒有一個。我得到ResponseStatus是未定義的。

很明顯,有一些缺失(它似乎在ServiceStack.Interfaces.dll中定義),如果我可以創建一個dll的dll,我認爲它會解決所有的引用。

任何人都可以深入瞭解如何創建DTO組裝的簡單的Hello World?

編輯以添加代碼:

using ServiceStack.ServiceClient.Web; 
namespace TestServiceStack 
{ 
    class HelloClient 
    {  public class HelloResponse 
    { 
     public string Result { get; set; } 
     public ResponseStatus ResponseStatus { get; set; } //Where Exceptions get auto-serialized 
    } 

    //Request DTO 
    public class Hello 
    { 
     public string Name { get; set; } 
    } 
    HelloResponse response = client.Get(new Hello { Name = "World!" }); 
    } 
} 

凡ResponceStatus是不確定的。

+0

如果你發佈一些代碼,它會更清晰。目前看來你的問題在於其他地方,因爲包括一個Dtos庫的工作方式與包括任何其他的dll完全相同。 –

+0

我沒有發佈代碼,因爲代碼全部在的公共源代碼域中。如果我遵循該教程,我可以構建我爲Host.Asp.Net所做的Hello應用程序。那是服務器。客戶端我試圖複製DTO代碼: – tradetree

+0

@JoannaTurban我希望我添加的代碼是您所指的。 – tradetree

回答

0

我能夠加入到尋找失蹤的符號ResponseStatus:

using ServiceStack.ServiceInterface.ServiceModel; 

下面是建立完整的代碼。請記住,我在過程中發現了其他事情。一旦構建它,然後失敗,因爲我在.NET 3.5環境中使用.NET 4.0環境中的DTO。但這是一個無關的問題。還要注意,這個測試代碼對響應沒有任何影響,它只是一個讓構建工作的例子。

using ServiceStack.ServiceClient; 
using ServiceStack.ServiceInterface; 
using ServiceStack.Text; 
using ServiceStack.Service; 
using ServiceStack.ServiceHost; 
using ServiceStack.WebHost; 
using ServiceStack; 
using ServiceStack.ServiceClient.Web; 
using RestTestRoot; // This is the name of my DTO assembly. You will need to insert your own here. 
using ServiceStack.ServiceInterface.ServiceModel; 

namespace WebApplicationRoot 
{ 

    class HelloClient 
    { 
     JsonServiceClient hello_client; 

     //Request DTO 
     public class Hello 
     { 
      public string Name { get; set; } 
     } 

     //Response DTO 
     public class HelloResponse 
     { 
      public string Result { get; set; } 
      public ResponseStatus ResponseStatus { get; set; } //Where Exceptions get auto-serialized 
     } 
     //Can be called via any endpoint or format, see: http://mono.servicestack.net/ServiceStack.Hello/ 
     public class HelloService : Service 
     { 
      public object Any(Hello request) 
      { 
       return new HelloResponse { Result = "Hello, " + request.Name }; 
      } 
     } 

     //REST Resource DTO 
     [Route("/todos")] 
     [Route("/todos/{Ids}")] 
     public class Todos : IReturn<List<Todo>> 
     { 
      public long[] Ids { get; set; } 
      public Todos(params long[] ids) 
      { 
       this.Ids = ids; 
      } 
     } 

     [Route("/todos", "POST")] 
     [Route("/todos/{Id}", "PUT")] 
     public class Todo : IReturn<Todo> 
     { 
      public long Id { get; set; } 
      public string Content { get; set; } 
      public int Order { get; set; } 
      public bool Done { get; set; } 
     } 


     public HelloClient(){ 
     //  ServiceStack gateway = new ServiceStack.ClientGateway(
     //    location.protocol + "//" + location.host + '/ServiceStack.Examples.Host.Web/ServiceStack/'); 
      hello_client = new JsonServiceClient("http://tradetree2.dnsapi.info:8080/"); 
      hello_client.Get<HelloResponse>("/hello/MyTestWorld!"); 
     } 
    } 
} 
相關問題