Return null instead of empty stream from Post/Delete/Put requests (#306)

This commit is contained in:
Andrey Chistyakov 2020-01-29 19:03:19 +00:00 коммит произвёл GitHub
Родитель fc217fac80
Коммит f9ce07c667
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 33 добавлений и 18 удалений

Просмотреть файл

@ -1,4 +1,4 @@
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
// <copyright file="PerceptionSimulationPlayback.cs" company="Microsoft Corporation">
// Licensed under the MIT License. See LICENSE.TXT in the project root license information.
// </copyright>
@ -192,26 +192,29 @@ namespace Microsoft.Tools.WindowsDevicePortal
using (Stream dataStream = await this.GetAsync(uri))
{
using (MemoryStream outStream = new MemoryStream())
if (dataStream != null)
{
dataStream.CopyTo(outStream);
if (outStream.Length != 0)
using (MemoryStream outStream = new MemoryStream())
{
outStream.Seek(0, SeekOrigin.Begin);
// Try to get the session state.
try
dataStream.CopyTo(outStream);
if (outStream.Length != 0)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HolographicSimulationPlaybackSessionState));
HolographicSimulationPlaybackSessionState sessionState = (HolographicSimulationPlaybackSessionState)serializer.ReadObject(dataStream);
playbackState = sessionState.State;
}
catch
{
// We did not receive the session state, check to see if we received a simulation error.
dataStream.Position = 0;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HolographicSimulationError));
HolographicSimulationError error = (HolographicSimulationError)serializer.ReadObject(dataStream);
throw new InvalidOperationException(error.Reason);
outStream.Seek(0, SeekOrigin.Begin);
// Try to get the session state.
try
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HolographicSimulationPlaybackSessionState));
HolographicSimulationPlaybackSessionState sessionState = (HolographicSimulationPlaybackSessionState)serializer.ReadObject(dataStream);
playbackState = sessionState.State;
}
catch
{
// We did not receive the session state, check to see if we received a simulation error.
dataStream.Position = 0;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HolographicSimulationError));
HolographicSimulationError error = (HolographicSimulationError)serializer.ReadObject(dataStream);
throw new InvalidOperationException(error.Reason);
}
}
}
}

Просмотреть файл

@ -54,6 +54,10 @@ namespace Microsoft.Tools.WindowsDevicePortal
// Ensure we return with the stream pointed at the origin.
dataStream.Position = 0;
if (dataStream.Length == 0)
{
dataStream = null;
}
}
}
}

Просмотреть файл

@ -83,6 +83,10 @@ namespace Microsoft.Tools.WindowsDevicePortal
// Ensure we return with the stream pointed at the origin.
responseDataStream.Position = 0;
if (responseDataStream.Length == 0)
{
responseDataStream = null;
}
}
}
}

Просмотреть файл

@ -58,6 +58,10 @@ namespace Microsoft.Tools.WindowsDevicePortal
// Ensure we return with the stream pointed at the origin.
dataStream.Position = 0;
if(dataStream.Length == 0)
{
dataStream = null;
}
}
}
}