Servicestack:从路由到操作

Servicestack : From route to operation

我可以从服务堆栈服务中的 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 };
      }
}

//操作

[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; }
}

感谢您的建议

如果你想访问执行服务的 HTTP 请求上下文,你应该从方便的 Service 基础 class 继承(或者让你的服务也实现 IRequiresRequestContext 所以请求被注入),例如:

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

但是您所追求的内容尚不清楚,因为 Request DTO 该请求的 Operation 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);
        }
    }
}

或者你可以通过传入请求DTO来执行服务,让ServiceStack调用适当的服务,例如:

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