【问题标题】:ServiceStack C# strongly typed client DTOServiceStack C# 强类型客户端 DTO
【发布时间】:2014-04-02 20:06:36
【问题描述】:

这里:Recommended ServiceStack API Structure 和这里:https://github.com/ServiceStack/ServiceStack/wiki/Physical-project-structure 是关于如何为 C# 客户构建项目以重用 DTO 的建议。

显然,这是通过包含 DTO 程序集的 dll 来完成的。我在网上搜索了一个示例,只是 Hello World,它为 ServiceStack 中的 C# 客户端使用了单独的程序集 DTO。也许我应该能够自己解决这个问题,但到目前为止还没有证明这很容易。

几乎所有客户端描述都是针对通用和非类型化 JSON 或其他基于非 DTO 的客户端。似乎没有人对像我这样的类型化 C# 客户端感兴趣(甚至是我找到的 ServiceStack 文档)。所以我认为这将是一个很好的问题,即使我最终自己弄清楚了。

明确地说,我已经构建并运行了 Hello World 示例服务器。我还使用浏览器连接到服务器并与之交互。我还创建了一个可以调用的客户端空项目

JsonServiceClient client = new JsonServiceClient(myURL);

然后我尝试在没有程序集 DLL 的情况下复制我的 DTO 定义,因为我没有。我得到ResponseStatus 未定义。

显然缺少一些东西(它似乎在ServiceStack.Interfaces.dll 中定义),如果我可以创建 DTO 的 dll,我认为它会解析所有引用。

谁能深入了解如何为简单的 Hello World 创建 DTO 程序集?

编辑添加代码:

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。

【问题讨论】:

  • 如果你发布一些代码会更清楚。目前,您的问题似乎出在其他地方,因为包含 Dtos 库的工作方式与包含任何其他 dll 完全相同。
  • 我没有发布代码,因为代码都在mono.servicestack.net/ServiceStack.Hello>的公共源域中。如果我遵循该教程,我可以构建我为 Host.Asp.Net 所做的 Hello 应用程序。那是服务器。我试图复制 DTO 代码的客户端:
  • @JoannaTurban 我希望我添加的代码是您所指的。

标签: c# .net visual-studio rest servicestack


【解决方案1】:

我能够通过添加找到缺少的符号 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!");
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多