【问题标题】:How to call a servicestack service directly from c# code, with validation and optional request context如何直接从 C# 代码调用 servicestack 服务,带有验证和可选的请求上下文
【发布时间】:2015-01-16 23:15:03
【问题描述】:

我希望我的代码中的服务能够通过请求验证直接调用其他 servicestack 服务。

在我看来,最好不要使用 JsonServiceClient,因为服务在同一个进程中运行,所有的序列化和 http 请求等。

我知道我可以打电话:

using (var service = base.ResolveService<MyService>()) { 
    service.Post(new MyRequest()); 
}

但是,这种方法不会强制执行我使用标准 servicestack fluent 验证类构建的请求验证代码。

理想情况下,我也可以选择更改请求上下文,以启用以不同权限运行等。

有人可以建议如何实现这一目标。

谢谢。

【问题讨论】:

    标签: c# servicestack fluentvalidation


    【解决方案1】:

    您可以使用HostContext.ServiceController.ExecuteMessage(new Message&lt;T&gt;(requestDto), httpReq);。这将通过完整的管道传递请求。

    this question 中提供了更多详细信息,以及我更详细的答案。

    【讨论】:

    • 是的,这也可以在服务中使用base.ExecuteRequest(requestDto)
    • 嗨,斯科特,感谢您的及时回答。提供的方法不应用验证过滤器,引用问题中使用的方法效果很好:HostContext.ServiceController.ExecuteMessage(new Message(requestDto), httpReq);
    • @richardwhatever 你说得对,我没有正确阅读自己之前的答案。我已经更新了这个。
    • 嗨@mythz。我认为您建议的方法实际上并不适用验证。它需要调用 HostContext.ServiceController.ExecuteMessage(new Message(requestDto), httpReq);调用验证过滤器。
    猜你喜欢
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多