简体   繁体   English

ServiceStack JsonServiceClient使用路由属性发送会引发未找到异常

[英]ServiceStack JsonServiceClient Send using route attributes throws exception not found

I am trying to make a simple api post call using servicestack and it keeps throwing an exception "not found". 我正在尝试使用servicestack进行简单的api post调用,并且不断抛出“未找到”异常。 When the same post call is made directly to the api using a web browser rest api eg postman, the api call works. 当使用Web浏览器rest api(例如邮递员)直接对api进行相同的post调用时,该api调用就起作用了。

I have decorated my request object with the route attributes 我已经用route属性装饰了我的请求对象

[Route("/register", "POST")]
public class Register : IReturn<RegistrationResponse>
{
    public DateTime? BirthDate { get; set; }

    public string Continue { get; set; }

    public string Email { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Gender { get; set; }

    public string Password { get; set; }
}

The JsonServiceClient is initialised with the base uri but the following call fails JsonServiceClient已使用基本uri初始化,但以下调用失败

_client = new JsonServiceClient(_apiUri);
_client.HttpMethod = HttpMethods.Post;
var response = _client.Send(body);

The exception that I catch is: 我捕获的异常是:

$exception {"Not Found"} System.Exception {ServiceStack.ServiceClient.Web.WebServiceException} at ServiceStack.ServiceClient.Web.ServiceClientBase.ThrowWebServiceException[TResponse](Exception ex, String requestUri) at ServiceStack.ServiceClient.Web.ServiceClientBase.ThrowResponseTypeException[TResponse](Object request, Exception ex, String requestUri) at ServiceStack.ServiceClient.Web.ServiceClientBase.HandleResponseException[TResponse](Exception ex, Object request, String requestUri, Func 1 createWebRequest, Func 2 getResponse, TResponse& response) at ServiceStack.ServiceClient.Web.ServiceClientBase.Send[TResponse](Object request) at ApiService`2.Post(String path, TParams body) in ApiService.cs:line 81 $ Exception {“ Not Found”} System.Exception {ServiceStack.ServiceClient.Web.WebServiceException}在ServiceStack.ServiceClient.Web.ServiceClientBase.ThrowWebServiceException [TResponse](Exception ex,String requestUri)在ServiceStack.ServiceClient.Web.ServiceClientBase.ThrowResponseTypeException ServiceStack上的[TResponse](对象请求,异常,字符串requestUri)。ServiceStack上的[TResponse](对象请求,异常,字符串requestUri,函数1 createWebRequest, Func函数2的getResponse,TResponse&response)。 ApiService.cs:第81行中ApiService`2.Post(字符串路径,TParams主体)的ServiceClient.Web.ServiceClientBase.Send [TResponse](对象请求)

The documentation on the new API at servicestack mentions the use of the Route attributes decorating the request DTO and the use of the IReturn but from looking at the code behind the Send method, it is working out the rest api url from the name of the request, which implies that your request dto cannot be named anything different. servicestack上有关新API的文档提到了装饰请求DTO的Route属性的使用以及IReturn的使用,但是通过查看Send方法背后的代码,它正在从请求名称中得出剩余的api url。 ,这意味着您的请求dto不能命名为其他任何东西。

public virtual TResponse Send<TResponse>(object request)
{
    var requestUri = this.SyncReplyBaseUri.WithTrailingSlash() + request.GetType().Name;
    var client = SendRequest(requestUri, request);

    try
    {
        var webResponse = client.GetResponse();
        return HandleResponse<TResponse>(webResponse);
    }
    catch (Exception ex)
    {
        TResponse response;

        if (!HandleResponseException(ex,
            request,
            requestUri,
            () => SendRequest(HttpMethods.Post, requestUri, request),
            c => c.GetResponse(),
            out response))
        {
            throw;
        }

        return response;
    }
}

What is causing the Not Found exception? 是什么导致“未找到”异常?

Everthing in your Register class looks correct. 您的Register类中的所有内容看起来都是正确的。

For your client call I would change it to 对于您的客户电话,我将其更改为

_client = new JsonServiceClient(_apiUri);
_client.Post(new Register()); //assuming you can map your 'body' variable to a Register class

Just to lose the extra line of code. 只是为了丢失多余的代码行。

it is working out the rest api url from the name of the request, which implies that your request dto cannot be named anything different. 它正在根据请求的名称计算出剩余的api网址,这意味着您的请求dto不能命名为其他任何名称。

It is working out the endpoint that the rest api will hit. 正在计算其余api将命中的端点。 Once it hits the endpoint, the internals of ServiceStack should handle the routing based on the Operation (in this case Register) and Http method. 一旦到达端点,ServiceStack的内部组件应基于Operation(在本例中为Register)和Http方法处理路由。 Basically it will try to find a Service class (any class inheriting the Service marker interface) that has the request object (Register) as a parameter and it will use the Http method as the 'function' to call. 基本上,它将尝试查找以请求对象(寄存器)为参数的Service类(继承Service标记接口的任何类),并将Http方法用作“函数”进行调用。

What is causing the Not Found exception? 是什么导致“未找到”异常?

Not exactly sure about this. 对此不确定。 If you could provide your 'Service' class it may help. 如果您可以提供“服务”类,则可能会有所帮助。

If you have a Service class like 如果您有类似的服务类

public class RegisterService : Service
{
    public RegistrationResponse Post(Register request) 
    {
        //service code 
        return new RegistrationResponse();
    }
}

the routing should work. 路由应该工作。

The fix for this was to ensure that the servicestack feature for predefined routes was enabled on the api. 解决此问题的方法是确保在api上启用了用于预定义路由的servicestack功能。 Once this is done, you don't need to bother with the Route attribute on the request objects. 完成此操作后,您无需费心请求对象上的Route属性。

The end point host config now looks like this: 现在,端点主机配置如下所示:

new EndpointHostConfig
{
    DefaultContentType = ContentType.Json,
    EnableFeatures = Feature.None
                            .Add(Feature.Json)
                            .Add(Feature.PredefinedRoutes),
    GlobalResponseHeaders = new Dictionary<string, string>(),
    DefaultRedirectPath = "/documentation"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM