具有复杂类型集合的C#调用odata操作失败

时间:2018-10-26 12:57:18

标签: c# odata odata.net

有人知道解决方法吗?我需要从C#代码中调用公开操作的服务器。此操作具有字符串和复杂类型的集合,或者具有字符串和复杂类型的集合作为参数的复杂类型。我可以得到任何一种,但是我需要两者之一,因为我需要用大量复杂类型来称呼它。

元数据是: 要么:

<Action Name="BulkChange" IsBound="true">
<Parameter Name="bindingParameter" Type="Collection(PropertyCore.InspectionDuty)"/>
<Parameter Name="Comment" Type="Edm.String" Unicode="false"/>
<Parameter Name="Changes" Type="Collection(PropertyCore.InspectionDutyChange)"/>
</Action>

具有以下复杂类型:

<ComplexType Name="InspectionDutyChange">
<Property Name="Operation" Type="Operations.Operations" Nullable="false"/>
<Property Name="Identity" Type="Edm.Guid" Nullable="false"/>
<Property Name="IsDisabled" Type="Edm.Boolean" Nullable="false"/>
<Property Name="SeriesStart" Type="Edm.DateTimeOffset"/>
<Property Name="Interval" Type="Common.Interval"/>
<NavigationProperty Name="Module" Type="PropertyCore.Module"/>
<NavigationProperty Name="Equipment" Type="PropertyCore.Equipment"/>
<NavigationProperty Name="OperatorTask" Type="PropertyCore.OperatorTask"/>
</ComplexType>

OR,或者:

<Action Name="BulkChange" IsBound="true">
<Parameter Name="bindingParameter" Type="Collection(PropertyCore.InspectionDuty)"/>
<Parameter Name="Updates" Type="PropertyCore.InspectionDutyChanges"/>
</Action>

复杂类型定义为

<ComplexType Name="InspectionDutyChanges">
<Property Name="Comment" Type="Edm.String"/>
<Property Name="Changes" Type="Collection(PropertyCore.InspectionDutyChange)"/>
</ComplexType>

<ComplexType Name="InspectionDutyChange">
<Property Name="Operation" Type="Operations.Operations" Nullable="false"/>
<Property Name="Identity" Type="Edm.Guid" Nullable="false"/>
<Property Name="IsDisabled" Type="Edm.Boolean" Nullable="false"/>
<Property Name="SeriesStart" Type="Edm.DateTimeOffset"/>
<Property Name="Interval" Type="Common.Interval"/>
<NavigationProperty Name="Module" Type="PropertyCore.Module"/>
<NavigationProperty Name="Equipment" Type="PropertyCore.Equipment"/>
<NavigationProperty Name="OperatorTask" Type="PropertyCore.OperatorTask"/>
</ComplexType>

我看不到解决方法,因为我们必须在一次运行中支持多个更新。

例外情况我完全注意到odata.net中的严重缺陷。

第一个变体:

Microsoft.OData.ODataException: Unsupported primitive type. A primitive type could not be determined for an instance of type 'Api.Odata.InspectionDutyChange'.

Result StackTrace:
at Microsoft.OData.ValidationUtils.ValidateIsExpectedPrimitiveType(Object value, IEdmPrimitiveTypeReference valuePrimitiveTypeReference, IEdmTypeReference expectedTypeReference)
at Microsoft.OData.JsonLight.ODataJsonLightValueSerializer.WritePrimitiveValue(Object value, IEdmTypeReference actualTypeReference, IEdmTypeReference expectedTypeReference)
at Microsoft.OData.JsonLight.ODataJsonLightCollectionWriter.WriteCollectionItem(Object item, IEdmTypeReference expectedItemType)
at Microsoft.OData.ODataCollectionWriterCore.InterceptException(Action action)
at Microsoft.OData.TaskUtils.GetTaskForSynchronousOperation(Action synchronousOperation)
--- End of stack trace from previous location where exception was thrown ---

和第二个变体:

Microsoft.OData.ODataException: The parameter 'Updates' is of Edm type kind 'Complex'. You cannot call WriteValue on a parameter that is not of Edm type kinds 'Primitive', 'Enum' or 'Complex'.

Stack trace:

Result StackTrace:
at Microsoft.OData.ODataParameterWriterCore.VerifyCanWriteValueParameter(Boolean synchronousCall, String parameterName, Object parameterValue)
at Microsoft.OData.ODataParameterWriterCore.WriteValueAsync(String parameterName, Object parameterValue)

我可以提出一个解决方法来进行一次更新-但这通常不可用。

有人知道如何解决此限制吗?我们使用Simple.Odata.Client作为客户端,但这不是问题所在-堆栈跟踪指向Odata.Net堆栈中的限制(缺少单元测试)。更新也很慢。没有替代品,我将不得不为这些调用手动创建http请求。

更新:没有解决方法。直到在odata.net库中解决该问题之前,我们都必须坚持使用标准REST api并手动构建paylad。每种compelx类型都会失败,并且由于包含导航属性,因此我无法分解最后一个级别-也无法重构引用(我必须使用自定义解串器来进行引用,因为这是odata.net中另一个不受支持的方案)。因此,除非有人采取解决方法,否则此操作与odata兼容,但与odata.net不兼容。

1 个答案:

答案 0 :(得分:1)

我假设您正在使用Simple.Odata.client。自从OData核心进行更改以考虑复杂类型与实体类型以来,该库尚未更新,因为复杂类型就像实体类型一样具有导航属性。可以在以下位置找到更改描述:enter image description here GitHub问题:ODataComplexValue中也解决了该问题。因此,我建议您使用Microsoft.OData.Client,它是最新的并且可以使用。