简体   繁体   中英

ExecuteAsync() causing hard crash with no exception

Here's the question: What would be stopping this code from executing this http request?

Here's the setup: I am doing a project that has multiple API's, and this is one method that is called within the first to the second for validation info. The method, in theory should make the request, send the model to the second service, and return back with something that is returned at the end of this method.

When running both API's locally with breakpoints to see if they are being hit, the second never gets hit, because the moment the ExecuteAsync() line is hit, it hard crashes, then restarts the first API. I dont think I did anything wrong here, but there isn't any sort of stack trace or exception that it throws at me, even if I put it into a try/catch. Is this a known issue or am I just doing something wrong here?

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

On the top level, this is is how it is called. The model is being made correctly without issue, and I can step through the code all the way up to the execute function before it just hard crashes

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

Gif for reference of steps running through debugger and the sudden crash https://imgur.com/bWSR78h

Alright, figured it out after finally trying it in a way that let me see an exception in a response. The error was the following:

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

Now the object I was using for a model was using microsoft's default claims object types for passing forward the claims. This apparently made the json serializer freak out and loop through itself over and over, and just hard crash when it found a loop.

The fix was just making a custom object type to map those claims to, and literally there were no issues using any of the 3 different HTTP method call types. Long story short, dont use the default claims type for things. Just makes issues.

Based on RestClient's documentation, ExecuteAsync does not throw an exception, but instead populates response.ErrorException and response.ErrorMessage if response.IsSuccessful is false . If you don't need access to the response StatusCode and only care about the body, just use PostAsync<T> instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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