【问题标题】:ExecuteAsync() causing hard crash with no exceptionExecuteAsync() 导致硬崩溃,无一例外
【发布时间】:2021-07-09 00:59:39
【问题描述】:

问题来了:什么会阻止这段代码执行这个 http 请求?

设置如下:我正在做一个具有多个 API 的项目,这是在第一个到第二个中调用的一种方法,用于验证信息。该方法理论上应该发出请求,将模型发送到第二个服务,并在此方法结束时返回一些返回的内容。

当使用断点在本地运行两个 API 以查看它们是否被命中时,第二个永远不会被命中,因为在命中 ExecuteAsync() 行的那一刻,它会严重崩溃,然后重新启动第一个 API。我认为我在这里没有做错任何事情,但是即使我将其放入 try/catch 中,它也不会向我抛出任何类型的堆栈跟踪或异常。这是一个已知问题还是我在这里做错了什么?

        public async Task<SharkTankValidationResponseModel> CheckIfValidRequest(SharkTankValidationModel requestModel)
        {
            // Setup rest client
            RestClient userRestClient = new RestClient(endpoint + "SharkTank/Validate");
            RestRequest userRequest = new RestRequest(Method.POST);
            // New request object for body

            userRequest.AddJsonBody(requestModel);
            // HTTP Request
            IRestResponse response = await userRestClient.ExecuteAsync(userRequest);
            return JsonConvert.DeserializeObject<SharkTankValidationResponseModel>(response.Content);
        }

在顶层,这就是它的名称。模型制作正确,没有问题,我可以在代码硬崩溃之前逐步执行代码,直到执行函数

                SharkTankValidationModel model = await makeSharkTankValidationModel(Method.GET, SharkTankConstants.GET_ALL_CLIENT_CATEGORY, null);
                SharkTankValidationResponseModel validationModel = await sharkTank.CheckIfValidRequest(model);

Gif 供参考通过调试器运行的步骤和突然崩溃 https://imgur.com/bWSR78h

【问题讨论】:

  • 你忘了等待CheckIfValidRequest吗?
  • 双重检查,我确实在顶层等待方法,所以这不是问题
  • 请分享minimal reproducible example。我的猜测是在调用堆栈中的某个地方你错过了等待。
  • 添加了顶级调用。据我所知,它正在被正确等待

标签: c# asp.net .net restsharp


【解决方案1】:

好的,在最终以一种让我在响应中看到异常的方式尝试后想通了。错误如下:

ActualMessage: "A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles."
BytePositionInLine: null
ClassName: "System.Text.Json.JsonException"
Data: null
ExceptionMethod: null
HResult: -2146233088
HelpURL: null
InnerException: null
LineNumber: null
Message: "A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles."
Path: "$.requestorRoles.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.Claims.Subject.AuthenticationType"
RemoteStackIndex: 0
RemoteStackTraceString: null
Source: "System.Text.Json"
StackTraceString: "   at System.Text.Json.ThrowHelper.ThrowJsonExcep
WatsonBuckets: null

现在我用于模型的对象正在使用微软的默认声明对象类型来传递声明。这显然让 json 序列化器吓坏了,一遍又一遍地循环自己,当它找到循环时就会硬崩溃。

修复只是制作了一个自定义对象类型来映射这些声明,实际上使用 3 种不同的 HTTP 方法调用类型中的任何一种都没有问题。长话短说,不要使用默认的声明类型。只是制造问题。

【讨论】:

    【解决方案2】:

    根据 RestClient 的文档,ExecuteAsync 不会抛出异常,而是填充 response.ErrorExceptionresponse.ErrorMessage 如果 response.IsSuccessfulfalse。如果您不需要访问响应的 StatusCode 并且只关心正文,请改用PostAsync&lt;T&gt;

    public async Task<SharkTankValidationResponseModel> CheckIfValidRequest(SharkTankValidationModel requestModel)
    {
        // Setup rest client
        RestClient userRestClient = new RestClient(endpoint);
        RestRequest userRequest = new RestRequest("SharkTank/Validate")
            .AddJsonBody(requestModel);
        // HTTP Request
        IRestResponse response = await userRestClient.PostAsync<SharkTankValidationResponseModel>(userRequest);
        return response;
    }
    

    【讨论】:

    • 试一试,但是,同样的问题仍然存在。我复制了调试器中的最后一条消息以帮助解决问题? pastebin.com/GeUT4Qtz 就像我明白的那样,理论上它至少应该抛出一个成功的布尔值等,但实际上它永远不会达到这一点。该程序只是在执行步骤中失败,这在我过去使用 RestSharp 的经验中不会发生
    • @kmanrulze,自从 dotnet core 出来后,我就很少使用 RestClient 了。您是否确认您的 RestClient 版本支持您所针对的 dotnet 版本?您是否尝试过独立序列化您的requestModel?您可能想查看标准的 HttpClient、HttpClientFactory,并将客户端注入到您的类中。 docs.microsoft.com/en-us/dotnet/architecture/microservices/…
    • 嗯,我也试了一下。似乎是一个不错的选择,但是当我运行断点时,发生了相同类型的硬崩溃,没有堆栈跟踪,并且通常与上面的 pastebin 相同。让我想也许更深层次的问题是问题?中间件或其他可能导致问题?
    • 只是一个更新,我尝试了第三种不同的方法来尝试捕获一些问题(如果有的话),并发现抛出如下异常:{"A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles."} 也许这指向了什么?再一次,老实说,我的想法与中间件排序或其他事情有关。可能有助于找出问题所在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多