Norcino
Norcino

Reputation: 6587

OData Client GetReadStreamAsync not working

I use OData Connected Service to generate a client context to consume an OData service currently server with .net core 2.1 (OData 7.2). The metadata of the entity affected is the below:

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

The entity returned by the API looks as follow:

{
    "@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
}

Running the code:

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

I get this error:

System.ArgumentException: 'This operation requires that the specified entity be a Media Link Entry and that the ReadStreamUri be available. However, the specified entity either is not a Media Link Entry or does not have a valid ReadStreamUri value. If the entity is a Media Link Entry, re-query the data service for this entity to obtain a valid ReadStreamUri value. (Parameter 'entity')'

This exception was originally thrown at this call stack: Microsoft.OData.Client.DataServiceContext.CreateGetReadStreamResult(object, Microsoft.OData.Client.DataServiceRequestArgs, System.AsyncCallback, object, string) Microsoft.OData.Client.DataServiceContext.BeginGetReadStream(object, Microsoft.OData.Client.DataServiceRequestArgs, System.AsyncCallback, object) System.Threading.Tasks.TaskFactory.FromAsyncImpl(System.Func, System.Func, System.Action, TArg1, TArg2, object, System.Threading.Tasks.TaskCreationOptions) System.Threading.Tasks.TaskFactory.FromAsync(System.Func, System.Func, TArg1, TArg2, object) Microsoft.OData.Client.DataServiceContext.GetReadStreamAsync(object, Microsoft.OData.Client.DataServiceRequestArgs) ConsoleApp1.Program.Main(string[])

The documentation For the getreadstreamasync method and OData Client Async Operations did not help me figure out what I am missing or doing wrong.

Upvotes: 0

Views: 584

Answers (1)

Norcino
Norcino

Reputation: 6587

I opened an issue on GitHub, you cannot access streams when the container has been configured with no tracking. At least using GetReadStreamAsync, so far this was the only way I found.

container.MergeOptions = NoTracking;

Note that if you try to attach the entity before getting the stream, the medialink will not be set and therefore the get stream will fail.

Upvotes: 0

Related Questions