成功修补程序后,Simple.Odata.Client获得“未找到所需的实例注释odata.context”异常

时间:2019-03-27 14:23:38

标签: asp.net-core asp.net-core-mvc odata asp.net-core-2.1 simple.odata.client

我正在ASP.Net Core 2.1 MVC网站上工作,并且正在使用Simple.OData.V4.Client v 5.6.2调用Azure中托管的ASP.NET Core 2.2 API。该API实现了Microsoft.AspNetCore.OData v7.1.0

当我从客户端进行PATCH调用时,API会正确修补记录,但是当客户端收到响应时,会出现以下错误;

An unhandled exception occurred while processing the request.
ODataException: The required instance annotation 'odata.context' was not found at the beginning of a response payload.
Microsoft.OData.JsonLight.ODataJsonLightDeserializer.ReadContextUriAnnotation(ODataPayloadKind payloadKind, PropertyAndAnnotationCollector propertyAndAnnotationCollector, bool failOnMissingContextUriAnnotation)

API旨在返回“ 204 No Content”(按照https://www.odata.org准则),因此,响应有效载荷中当然没有'odata.context'。

我已经使用错误消息搜索了Google,并且似乎应该在ODataClientSettings中找到一种解决方法。但是,到目前为止,我还没有运气。

我尝试过...

oDataClientSettings.IncludeAnnotationsInResults = false;

但这对错误没有影响。

我也尝试过...

oDataClientSettings.PayloadFormat = ODataPayloadFormat.Json;

同样,错误没有区别。

这是我的Edit方法,该方法调用PATCH API端点。

    [HttpPost]
    public async Task<IActionResult> Edit(EditAddressComplianceCodeViewModel editVm, string cancel)
    {
        if (!string.IsNullOrEmpty(cancel))
        {
            return RedirectToAction("Index", "AddressComplianceCodes");
        }

        if (!ModelState.IsValid) return View(editVm);

        try
        {
            var tokenSource = new CancellationTokenSource();
            var token = tokenSource.Token;

            var accessToken = await _psUtils.GetApiAccessToken(token);

            var oDataClientSettings = SetODataToken(_psUtils.GetBaseUspsReferenceApiUrl(), accessToken);
            oDataClientSettings.IncludeAnnotationsInResults = false;
            oDataClientSettings.PayloadFormat = ODataPayloadFormat.Json;

            var client = new ODataClient(oDataClientSettings);

            var response = await client.For<AddressComplianceCode>()
                .Key(editVm.Code)
                .Set(new
                {
                    editVm.Description,
                })
                .UpdateEntryAsync(token); //<-- ODataException occurs when this block of code is executed.


            _logger.LogInformation($"User ID [{User.Identity.Name}] edited the Address Compliance Code reocrd for code [{response.Code}].");

            return RedirectToAction("Index", "AddressComplianceCodes");

        }
        catch (Exception ex)
        {
            var message = $"An exception has occurred trying to edit the Address Compliance Code record for [{editVm.Code}].";
            _logger.LogError(ex, message);
            throw;
        }

    }

预期结果是将编辑应用于记录,并且页面将重定向到记录列表。通过API将确实应用于数据库中的记录,但是,在上面的代码指示的位置,我得到了ODataException。它没有达到_logger行。

当我在Postman中测试API时,会获得成功的204 No Content状态和空的响应正文(正如我期望的204响应一样)

有什么想法吗?

0 个答案:

没有答案