ServiceStack: JsonServiceClient usage without IRet

2019-07-19 15:19发布

问题:

What I would like to do is the following:

var client = new JsonServiceClient(ServiceUrl);
var request = new FooQuery {Id = 1};
IEnumerable<Project> response = client.Get(request);

However, my FooQuery doesn't implement any IReturn, and I'd like it not to (it's in a library without ServiceStack references). Here's my service side:

Library of business objects:

public class ProjectQuery
{
    public int Id { get; set; }
}

AppHost:

Routes.Add<ProjectQuery>("/project", "GET");

Service:

public object Get(Foo request)
{
     // do stuff.
}

Is there some nice, clean way to create the JsonServiceClient without using the IReturn interface on my business object?

回答1:

Looks like there's no way not to use IReturn if you don't want to provide a URL to the JsonServiceClient Get() requests. Just decided to create another set of DTOs in my ServiceStack implementation, that are essentially mirrors of the real DTOs in another library. Then when a request comes in to my SS DTO, I create the other library's DTO, set each property, and pass it along.

Not pretty, but that's the best I could find so far.



回答2:

I had the same problem using IReturn and Routes, as I wanted to use the DTOs
in assemblies with business logic, without ServiceStack references.

It worked for me, using in the Client Model

   public class TestRequest
   {
        public int vendorId {get; set; }
        public string barcode {get; set; }     
        public string username { get; set; }   
        public string password { get; set; } 
   }    

then in the AppHost

      Routes.Add<TestRequest( "/TestAPI/Reservation/{vendorId}/{barcode}"," GET,OPTIONS")   
            .Add<TestRequest>("/TestAPI/Reservation", "POST, OPTIONS")     

and the call for JsonServiceClient with POST

        request.vendorId=12344; 
        request.barcode="AAS1223"; 
        TestResponse response = client.Post<TestResponse>(server_ip + "/TestAPI/Reservation", request);  

OR with GET

   TestResponse response = client.Get<TestResponse>(server_ip + "/TestAPI/Reservation/12344/AAS1223?username=John&password=99");     

Then in the service Get or Post functions

      public TestResponse Get(TestRequest request)
      {    
         // request members  hold  the values of the url.
         return  DoBusinessLayerWork(request);
      }


回答3:

Using the Send() method from the JsonServiceClient type is the way to go about doing this.