Skip to content

Deserialization issue when consuming ASMX Web Service from Xamarin Android .NET6 App #7230

Closed

Description

@adolfo1981

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

  1. Create a Xamarin Android (NET 6) project similar to @jamesmontemagno https://github.com/jamesmontemagno/net6-mobile-samples/tree/main/HelloAndroid
  2. 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/
  3. Create a .NET 6 (Class Library) WebServiceClient and add a Connected Service -> Web Service pointing to a wsdl or deployed asmx file
  4. 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

  1. added
    Area: Mono RuntimeMono-related issues: BCL bugs, AOT issues, etc.
    and removed
    needs-triageIssues that need to be assigned.
    on Aug 4, 2022
  2. adolfo1981 commented on Aug 6, 2022

    @adolfo1981
    Author

    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.

  3. adolfo1981 commented on Aug 6, 2022

    @adolfo1981
    Author

    I added a ClientTestConsole .NET6 project that references my WebServiceClient and successfully calls my HelloWorldsAsync method receiving a valid response to prove that Web Service client consumption works outside of the Android environment.

  4. adolfo1981 commented on Aug 6, 2022

    @adolfo1981
    Author

    msbuild.zip
    I uploaded the msbuild.binlog generated through this command in case it helps:
    dotnet build HelloAndroid.csproj /bl:msbuild.binlog

  5. adolfo1981 commented on Aug 6, 2022

    @adolfo1981
    Author

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

    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.

  6. added
    Area: HTTPIssues with sockets / HttpClient.
    and removed
    Area: Mono RuntimeMono-related issues: BCL bugs, AOT issues, etc.
    on Sep 6, 2022
  7. waelwederni4 commented on Sep 19, 2022

    @waelwederni4

    Any update Here ?

  8. demogog commented on Oct 19, 2022

    @demogog

    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.

  9. RasmusTangaaLittleBeacon commented on Oct 19, 2022

    @RasmusTangaaLittleBeacon

    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?

  10. LalaBond commented on Oct 19, 2022

    @LalaBond

    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.

  11. KhaledGaber commented on Oct 24, 2022

    @KhaledGaber

    We are also receiving deserialization exceptions with the Responses like OP. It is a show-stopper for us.

  12. yuvii28 commented on Dec 8, 2022

    @yuvii28

    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.

  13. old-man-burns commented on Dec 22, 2022

    @old-man-burns

    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.

  14. steveisok commented on Jan 20, 2023

    @steveisok
    Member

    Closing in favor of dotnet/runtime#80935

  15. ghost locked as resolved and limited conversation to collaborators on Feb 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: HTTPIssues with sockets / HttpClient.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions