【问题标题】:OData Client GetReadStreamAsync not workingOData 客户端 GetReadStreamAsync 不起作用
【发布时间】:2020-03-05 09:26:20
【问题描述】:

我使用OData Connected Service 生成客户端上下文以使用当前使用 .net core 2.1 (OData 7.2) 服务的 OData 服务。 受影响实体的元数据如下:

 <EntityType Name="Question" HasStream="true">
    <Key>
        <PropertyRef Name="Language" />
        <PropertyRef Name="QuestionRevisionId" />
    </Key>
    <Property Name="QuestionRevisionId" Type="Edm.Int32" />
    <Property Name="Language" Type="Edm.String" Nullable="false" />
    <Property Name="CreatedDateTime" Type="Edm.DateTimeOffset" Nullable="false" />
    <Property Name="Author" Type="Edm.String" />
    <Property Name="ModifiedDateTime" Type="Edm.DateTimeOffset" Nullable="false" />
    <Property Name="Editor" Type="Edm.String" />
    <Property Name="TranslationStatus" Type="Edm.Int32" />
    <Property Name="IsDeleted" Type="Edm.Boolean" Nullable="false" />
    <NavigationProperty Name="QuestionRevision" Type="QM.AuthoringApi.OData.Entity.QuestionRevision">
        <ReferentialConstraint Property="QuestionRevisionId" ReferencedProperty="Id" />
    </NavigationProperty>
</EntityType>

API 返回的实体如下:

{
    "@odata.context": "https://[...]/odata/$metadata#Questions/$entity",
    "@odata.mediaReadLink": "https://[...]/odata/Questions(QuestionRevisionId=9009,Language='en-US')/$value",
    "@odata.mediaContentType": "text/plain",
    "QuestionRevisionId": 9009,
    "Language": "en-US",
    "CreatedDateTime": "2017-08-18T11:16:56.02Z",
    "Author": "Manager",
    "ModifiedDateTime": "2018-09-03T15:55:42.063Z",
    "Editor": "Editor",
    "TranslationStatus": null,
    "IsDeleted": false
}

运行代码:

var keys = new Dictionary<string, object>
{
    { "QuestionRevisionId", 9009 },
    { "Language", "en-US" }
};
var question = await apiReference.Questions.ByKey(keys).GetValueAsync();
apiReference.AttachTo("Questions", question); 
var thisIsNull = apiReference.GetReadStreamUri(question);
var stream = (await apiReference.GetReadStreamAsync(question, new DataServiceRequestArgs())).Stream;
var sr = new StreamReader(stream).ReadToEnd();

我收到此错误:

System.ArgumentException: '此操作需要指定的 entity 是一个媒体链接条目,并且 ReadStreamUri 可用。 但是,指定的实体要么不是媒体链接条目,要么不 没有有效的 ReadStreamUri 值。如果实体是媒体链接 入口,重新查询该实体的数据服务以获得有效的 读取StreamUri 值。 (参数'实体')'

这个异常最初是在 这个调用堆栈: Microsoft.OData.Client.DataServiceContext.CreateGetReadStreamResult(对象, Microsoft.OData.Client.DataServiceRequestArgs,System.AsyncCallback, 对象,字符串) Microsoft.OData.Client.DataServiceContext.BeginGetReadStream(对象, Microsoft.OData.Client.DataServiceRequestArgs,System.AsyncCallback, 目的) System.Threading.Tasks.TaskFactory.FromAsyncImpl(System.Func,System.Func, System.Action,TArg1,TArg2,对象, System.Threading.Tasks.TaskCreationOptions) System.Threading.Tasks.TaskFactory.FromAsync(System.Func,System.Func, TArg1,TArg2,对象) Microsoft.OData.Client.DataServiceContext.GetReadStreamAsync(对象, Microsoft.OData.Client.DataServiceRequestArgs) ConsoleApp1.Program.Main(string[])

文档 For the getreadstreamasync methodOData Client Async Operations 并没有帮助我弄清楚我错过了什么或做错了什么。

【问题讨论】:

    标签: c# odata


    【解决方案1】:

    我在GitHub 上打开了一个问题,当容器配置为没有跟踪时,您无法访问流。至少使用 GetReadStreamAsync,到目前为止这是我找到的唯一方法。

    container.MergeOptions = NoTracking;
    

    请注意,如果您在获取流之前尝试附加实体,则不会设置媒体链接,因此获取流将失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多