Repository navigation
Deserialization issue when consuming ASMX Web Service from Xamarin Android .NET6 App #7230
Description
Android application type
Android for .NET (net6.0-android)
Affected platform version
VS 2022 Version 17.3.0 Preview 5.0 (64 bit)
Description
I'm upgrading an existing Xamarin Traditional (Android) application into .NET 6 but I'm running into deserialization problems. Our Android app integrates with a .NET Framework 4.8 ASMX Web Service on the backend. This has been working fine for years but doesn't work after we move to .NET 6.
I was able to create a repro project on my GitHub: https://github.com/adolfo1981/Net6AndroidWebServiceTest
Here are the details from deserialization error.
Exception:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:HelloWorldResponse. The InnerException message was 'There was an error deserializing the object of type ServiceReference1.HelloWorldResponseBody. Unexpected end of file. Following elements are not closed: HelloWorldResult, HelloWorldResponse, Body, Envelope. Line 1, position 298.'. Please see InnerException for more details.
StackTrace:
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName)
at System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName)
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.PartInfo.ReadObject(XmlDictionaryReader reader, XmlObjectSerializer serializer) in /_/src/System.Private.ServiceModel/src/System/ServiceModel/Dispatcher/DataContractSerializerOperationFormatter.cs:line 657
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.PartInfo.ReadObject(XmlDictionaryReader reader) in /_/src/System.Private.ServiceModel/src/System/ServiceModel/Dispatcher/DataContractSerializerOperationFormatter.cs:line 652
at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part, Boolean isRequest) in /_/src/System.Private.ServiceModel/src/System/ServiceModel/Dispatcher/DataContractSerializerOperationFormatter.cs:line 521
My GitHub repro repo consists of the following projects:
- HelloAndroid (NET6 - .net6.0-android Target Framework Moniker)
- WebServiceServer (.NET Framework 4.8)
- WebServiceClient (.NET 6) --> referenced by HelloAndroid project to consume ASMX Web Services
Steps to Reproduce
- Create a Xamarin Android (NET 6) project similar to @jamesmontemagno https://github.com/jamesmontemagno/net6-mobile-samples/tree/main/HelloAndroid
- Create a .NET Framework 4.8 WebServiceServer application following instructions like this: https://www.c-sharpcorner.com/article/how-to-create-a-web-service-project-in-net-using-visual-studio/
- Create a .NET 6 (Class Library) WebServiceClient and add a Connected Service -> Web Service pointing to a wsdl or deployed asmx file
- From Main Activity create an instance of the generated soap client and call HelloWorldAsync method:
var soapClient = new WebServiceSoapClient(WebServiceSoapClient.EndpointConfiguration.WebServiceSoap);
//Async test
var helloResponse = await soapClient.HelloWorldAsync();
Expected Result: Get string response from HelloWorldAsync() method
Actual Result: I get the deserialization issue mentioned above
Repro Project: https://github.com/adolfo1981/Net6AndroidWebServiceTest
IMPORTANT:
The repro project points to my local IP (Bottom of Reference.cs file) and needs to be changed to IP of the user running the code. I could not use localhost because I was getting connectivity issues and only worked when setting my local IP.
Things that I've tried:
- Generating the Web Service Client with 'Reuse referenced assemblies' enabled and disabled and that had no effect. Kept getting deserialization issue
- Tried changing the HelloAndroid.csproj TargetFramework from .net6,0-android to .net6.0-android31.0 and nothing changed even after deleting /bin and /obj folders in this project
Did you find any workaround?
No workaround has been found
Relevant log output
No response
Activity
adolfo1981 commentedon Aug 6, 2022
Any update? This is holding a big mobile app migration. We need to move to net6 to support some required Google maps SDK upgrades. Hope someone can provide some help.
adolfo1981 commentedon Aug 6, 2022
I added a
ClientTestConsole.NET6 project that references myWebServiceClientand successfully calls myHelloWorldsAsyncmethod receiving a valid response to prove that Web Service client consumption works outside of the Android environment.adolfo1981 commentedon Aug 6, 2022
msbuild.zip
I uploaded the msbuild.binlog generated through this command in case it helps:
dotnet build HelloAndroid.csproj /bl:msbuild.binlogadolfo1981 commentedon Aug 6, 2022
I was able to capture request and responses when running Android Emulator (failed call with deserialization error) and when running Console app (Successful call). I did not find significant differences on soap requests and responses. (See attached):
Request and Response Fiddler Captures.zip
The biggest difference I saw was on the Emulator Request which sent a Connection: Keep-Alive header and User-Agent as you can see below:

This makes me think that the problem is with some possible Assembly mismatch or conflict trying to deserialize the message when running from Xamarin Android app.
waelwederni4 commentedon Sep 19, 2022
Any update Here ?
demogog commentedon Oct 19, 2022
We have actually the same error.
The formatter threw an exception while trying to deserialize the message and _Unexpected end of file. Following elements are not closed: _
We have also tried creating a custom WCF proxy to see if we can somehow workaround the issue, but no luck.
RasmusTangaaLittleBeacon commentedon Oct 19, 2022
I have got the same problem when using .net Maui .net 6.0 with an WCF service of same type. It works fine on both IOS and Windows application, and i have also tested it in a console application where it works too.
Is it possible that the service has to be migrated to something like gRPC, CoreWCF or ASP.NET CORE API, or is the problem related to something in android that can be fixed?
LalaBond commentedon Oct 19, 2022
Hello, I was working with @adolfo1981 on this issue. After many different tries and building our WCF to core, net 6 and net standard we couldnt consume WCF services within our projects. I believe we tried also a standalone android net 6 and tried all builds for the WCF and we were able to make the calls with WCF on standard 2.1 with no issues, so got to think there were some issues with the WCF updated libs for parsing and our net 6 project references. iOS Net 6 didnt present any issues on emulators, but we started to get same serialization issues on a physical device. Decided in the end to move away from WCF and migrated our services to REST.
KhaledGaber commentedon Oct 24, 2022
We are also receiving deserialization exceptions with the Responses like OP. It is a show-stopper for us.
yuvii28 commentedon Dec 8, 2022
Are there any updates regarding this issue?
We have the same problem, and are unable to work around it.
We are unable to change move away from WCF, so have no alternative at this time.
old-man-burns commentedon Dec 22, 2022
Curious if this is actively being investigated...
I'm also experiencing the same issue within a .NET 7 Maui app connecting to a WCF service. The service returns the expected results within the Windows and iOS platforms, but the deserialization issue is preventing it from working on Android.
steveisok commentedon Jan 20, 2023
Closing in favor of dotnet/runtime#80935
Tweak AndroidMessageHandler behavior for WCF support