Вопрос

Here: Recommended ServiceStack API Structure and here: https://github.com/ServiceStack/ServiceStack/wiki/Physical-project-structure are recommendations for how to structure your projects for C# clients to reuse DTOs.

Apparently this is done by including a dll of the DTO assembly. I have searched the web for one example, just Hello World that uses a separate assembly DTO for a C# client in ServiceStack. Perhaps I should be able to break this out myself but so far it has not proven that easy.

Almost all client descriptions are for generic and non-typed JSON or other non-DTO based clients. No one appears interested in typed C# clients like I am (even the ServiceStack documentation I have found). So I thought this would be a good question even if I figure it out myself in the end.

To be clear, I have built and run the Hello World example server. I have also used a browser to attach to the server and interact with it. I have also created a client empty project that can call

JsonServiceClient client = new JsonServiceClient(myURL);

Then I tried to copy over my DTO definition without the assembly DLL as I don't have one. I get ResponseStatus is undefined.

Clearly there is something missing (it appears to be defined in ServiceStack.Interfaces.dll) and if I could create a dll of the DTO I think it would resolve all references.

Can anyone give insight into how to create the DTO assembly for the simple Hello World?

Edited to add code:

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!" });
  }
}

Where the ResponceStatus is undefined.

Нет правильного решения

Другие советы

I was able to find the missing symbol ResponseStatus by adding:

using ServiceStack.ServiceInterface.ServiceModel;

Here is the full code that built. Keep in mind that I found out something else in the process. Once this built it then failed because I was using a DTO from a .NET 4.0 environment in a .NET 3.5 environment. But that is an unrelated issue. Also note that this test code does nothing with the response, it is just an example to get the build working.

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!");
        }
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top