My environment: Visual Studio 2010
I have 2 C# webAPI projects that return data to a VB client project.
API project #1: OrchardsAPI - returns lots of data perfectly.
API project #2: aspnetAPI - accesses the Microsoft aspnetdb database on my SQL server. Throws an exception when deserialized on the Client.
When I interrogate the API projects directly from my browser, the following is the first the first line each response. API#1 returns a list of cultivars used in my orchards. Note the xmlns values.
API #1:
<ArrayOfCultivarEntity xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
API #2:
<ArrayOfaspnet_RoleEntity xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/aspnetEntities">
When accessed directly from a browser, a valid xml file is displayed with records from each API. However, when I get request the data from my Client, I get an exception stating that:
InnerException {"<ArrayOfaspnet_RoleEntity xmlns='http://schemas.datacontract.org/2004/07/aspnetEntities'> was not expected."}
The relevant code in my client is:
Public Function GetAllRoles() As List(Of aspnetEntities.aspnet_RoleEntity)
Dim _entities As New List(Of aspnet_RoleEntity)
Try
url = "role/byAppName/OrchardAutomation"
responseMessage = client.GetAsync(url).Result
If responseMessage.IsSuccessStatusCode = True Then
_entities = New Xml.Serialization.XmlSerializer(_entities.GetType).Deserialize(responseMessage.Content.ReadAsStreamAsync.Result)
Return _entities
Else
ProcessClientError(responseMessage.Content.ReadAsStringAsync.Result)
Return Nothing
End If
Catch ex As Exception
ProcessClientException(ex)
Return Nothing
End Try
End Function
I have searched for all instances of "xmlns" in both API projects and they are all identical. There must be something I'm missing. Any hints greatly appreciated.
CodePudding user response:
Solved thanks to the following link: [https://docs.microsoft.com/de-de/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization#xml_media_type_formatter]
Turns out my API #1 had the following code in the global.asax file:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "xml", new System.Net.Http.Headers.MediaTypeHeaderValue("application/xml")));
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.JsonFormatter);
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
The aforementioned link noted that the datacontract serializer was the default and one had to add the code to switch to the xml formatter:
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;
Which I put in the initialization section of my controller. That solved the problem. I then looked at the global.asax in API#1 and found the 3 lines in the app_start method. I inserted them into the app_start of API#2, removed the "var xml" lines from the initialization of the individual controller and the project continued to work perfectly.
Hope this helps someone else!