使用c#autorest生成的客户端(通过Visual Studio 17,添加REST API客户端),当反序列化(应用程序/邮政编码类型)响应正文时,出现异常。我正在寻找有关如何克服这一点的想法。生成的引发异常的代码:
if ((int)_statusCode == 200)
{
_responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
try
{
_result.Body = SafeJsonConvert.DeserializeObject<byte[]>(_responseContent, this.Client.DeserializationSettings);
}
catch (JsonException ex)
{
_httpRequest.Dispose();
if (_httpResponse != null)
{
_httpResponse.Dispose();
}
throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
}
}
例外情况(为了保密起见,进行了少量修改):
System.AggregateException: One or more errors occurred. ---> Microsoft.Rest.SerializationException: Unable to deserialize the response. ---> Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: P. Path '', line 1, position 1.
at Newtonsoft.Json.JsonTextReader.ReadAsBytes()
at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Microsoft.Rest.Serialization.SafeJsonConvert.DeserializeObject[T](String json, JsonSerializerSettings settings)
at API.Files.<GetZippedFileWithHttpMessagesAsync>d__8.MoveNext() in C:\...\Files.cs:line 600
--- End of inner exception stack trace ---
at API.Files.<GetZippedFileWithHttpMessagesAsync>d__8.MoveNext() in C:\Files.cs:line 609
--- End of stack trace from previous location where exception was thrown ---
据我所知,实际的响应格式正确,而且我知道其他应用程序也能够成功调用该方法并解析响应。
这里有一些用于创建客户端的json,尽管出于机密性而进行了一些编辑。
"/v{api-version}/files/{idCode}/method/zipped": {
"get": {
"tags": [
"Lists"
],
"summary": "",
"description": "This method will return a zipped file.",
"operationId": "Files_GetZipped",
"consumes": [],
"produces": [
"application/json",
"text/json",
"application/json-patch+json"
],
"parameters": [
{
"name": "idCode",
"in": "path",
"description": "identifier",
"required": true,
"type": "string"
},
{
"name": "api-version",
"in": "path",
"description": "The requested API version",
"required": true,
"type": "string",
"default": "1.0"
}
],
"responses": {
"200": {
"description": "Ok",
"schema": {
"format": "byte",
"type": "string"
}
},
"400": {
"description": "Bad request"
},
"500": {
"description": "Server Error"
}
},
"deprecated": false
}
},