Servicestack:从路线到运营

时间:2015-01-26 20:19:53

标签: servicestack

我可以从服务堆栈服务中的url路由检索操作DTO吗?

示例:

public class HelloService : IService
{

    public object Any(HelloRequest request)
    {
//Here I want to retrieve operation Dto. 
//In this case if request.AnotherApiRoute is "/another?Age=33" 
//then result could be operation AnotherRequest 

       return new HelloResponse { Result = "Hello, " + name };
    }
}

public class AnotherApiService : IService
{
      public object Another(AnotherRequest request)
      {
            return new AnotherResponse { Result = "Your Age : " + Age };
      }
}

// OPERATIONS

[Route("/hello/{Name}")]
public class Hello : IReturn<HelloResponse>
{
    public string Name { get; set; }
    public string AnotherApiRoute {get; set;}
}

public class HelloResponse
{
    public string Result { get; set; }
}

[Route("/another/{Age}")]
public class AnotherRequest : IReturn<AnotherResponse>
{
    public string Age { get; set; }
}

public class AnotherResponse
{
    public string Result { get; set; }
}

感谢您的建议

1 个答案:

答案 0 :(得分:0)

如果要访问HTTP请求上下文,则执行服务,您应该从便捷Service基类继承(或者让您的服务也实现IRequiresRequestContext,以便注入请求),例如:

public class HelloService : Service
{
    public object Any(Hello request)
    {
       var pathInfo = base.Request.PathInfo;
       return new HelloResponse { Result = "Hello, " + name };
    }
}

但是你所追求的是不清楚的,因为请求DTO 是针对该请求的操作DTO。如果您想从服务中调用另一个服务,您可以通过以下方式解决来自IOC(也会注入当前HTTP请求)的服务:

public class HelloService : Service
{
    public object Any(Hello request)
    {
        using (var service = base.ResolveService<AnotherService>())
        {
            var anotherDto = request.ConvertTo<Another>();
            return service.Any(anotherDto);
        }
    }
}

或者,您可以通过传入Request DTO来执行服务,并让ServiceStack调用相应的服务,例如:

public class HelloService : Service
{
    public object Any(Hello request)
    {
        var anotherDto = request.ConvertTo<Another>();
        return base.ExecuteRequest(anotherDto);
    }
}