来自嵌套服务的访问请求

本文关键字:访问 请求 服务 自嵌 嵌套 | 更新日期: 2023-09-27 18:06:32

是否有一种简单的方法可以从嵌套的服务调用访问Request对象?

例如

:

// Entry Point
public class ServiceA : Service
{
    public AResponse Get(ARequest request)
    {
        // Request is ok in entry point.
        // Now call another service
        var srvResp = TryResolve<ServiceB>().Get(new BRequest{ ... });
    }
}
// Called through Service A
ServiceB : Service
{   
    public BResponse Get(BRequest request)
    {
        // Request is not set here (null).
    }
}

来自嵌套服务的访问请求

您应该使用Service类提供的ResolveService<T>方法来解析服务。这将使Request对象对您正在解析的服务可用。

public class ServiceA : Service
{
    public AResponse Get(ARequest request)
    {
        // Request is ok in entry point.
        // Use ResolveService<T> here not TryResolve<T>
        var srvResp = ResolveService<ServiceB>().Get(new BRequest{ ... });
    }
}

希望有帮助