Commit 5d46685
authored
[Mono.Android] Tweak AndroidMessageHandler behavior for WCF support (#7785)
Context: #7230
Context: dotnet/runtime#80935
When a WCF application invokes an endpoint which returns compressed
content, and `AndroidMessageHandler` is doing the network requests
([the default when `$(UseNativeHttpHandler)`=True][0]):
var soapClient = new WebServiceSoapClient(WebServiceSoapClient.EndpointConfiguration.WebServiceSoap);
//Async test
var helloResponse = await soapClient.HelloWorldAsync();
then the method will throw:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:HelloWorldResponse.
---> 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.
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
The reason for this is that when `AndroidMessageHandler` creates a
wrapping decompression stream, it does not update `Content-Length` to
match the length of the decoded content, because it doesn't have a
way to know what the length is without first reading the stream to the
end, and that might prevent the end user to read the content.
(Additionally, I think the `Content-Length` header should reflect the
*original* content length, for the end user to be able to
interpret the response as it was sent.)
WCF, on the other hand, looks at the `Content-Length` header and, if
found, takes the value and reads only that many bytes from the content
stream and no more, which will almost always result in short reads and
failure to correctly interpret the response.
Workaround this issue by making `AndroidMessageHandler` behave the
same way as other handlers implemented in the BCL. What they do in
this situation is remove the `Content-Length` header, making WCF
read the stream to the end. Additionally, the clients remove the
compressed content encoding identifier from the `Content-Encoding`
header.
var handler = new AndroidMessageHandler {
AutomaticDecompression = DecompressionMethods.All
};
var client = new HttpClient (handler);
var response = await client.GetAsync ("https://httpbin.org/gzip");
// response.Content.Headers won't contain Content-Length,
// and response.Content.Headers.ContentEncoding won't contain `gzip`.
As a bonus, also adds support for decompression of responses
compressed with the `Brotli` compression which use the `br` encoding
ID in the `Content-Encoding` header.
[0]: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options?pivots=dotnet-7-01 parent f007593 commit 5d46685
3 files changed
Lines changed: 203 additions & 42 deletions
File tree
- src
- Mono.Android/Xamarin.Android.Net
- Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base
- tests/Mono.Android-Tests/Xamarin.Android.Net
Lines changed: 122 additions & 32 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
72 | 104 | | |
73 | 105 | | |
74 | 106 | | |
75 | 107 | | |
| 108 | + | |
76 | 109 | | |
| 110 | + | |
| 111 | + | |
77 | 112 | | |
78 | 113 | | |
79 | 114 | | |
| |||
82 | 117 | | |
83 | 118 | | |
84 | 119 | | |
85 | | - | |
| 120 | + | |
86 | 121 | | |
87 | | - | |
| 122 | + | |
88 | 123 | | |
89 | 124 | | |
90 | 125 | | |
| |||
571 | 606 | | |
572 | 607 | | |
573 | 608 | | |
| 609 | + | |
574 | 610 | | |
575 | 611 | | |
576 | 612 | | |
| |||
608 | 644 | | |
609 | 645 | | |
610 | 646 | | |
611 | | - | |
| 647 | + | |
612 | 648 | | |
613 | 649 | | |
614 | 650 | | |
615 | 651 | | |
616 | 652 | | |
617 | | - | |
| 653 | + | |
618 | 654 | | |
619 | 655 | | |
620 | 656 | | |
| |||
633 | 669 | | |
634 | 670 | | |
635 | 671 | | |
636 | | - | |
| 672 | + | |
637 | 673 | | |
638 | 674 | | |
639 | 675 | | |
| |||
661 | 697 | | |
662 | 698 | | |
663 | 699 | | |
664 | | - | |
665 | | - | |
| 700 | + | |
| 701 | + | |
666 | 702 | | |
667 | 703 | | |
668 | 704 | | |
| |||
676 | 712 | | |
677 | 713 | | |
678 | 714 | | |
679 | | - | |
| 715 | + | |
680 | 716 | | |
681 | 717 | | |
682 | 718 | | |
683 | 719 | | |
684 | 720 | | |
685 | 721 | | |
686 | 722 | | |
687 | | - | |
| 723 | + | |
688 | 724 | | |
689 | 725 | | |
690 | 726 | | |
691 | 727 | | |
692 | | - | |
| 728 | + | |
693 | 729 | | |
694 | 730 | | |
695 | 731 | | |
696 | 732 | | |
697 | 733 | | |
698 | | - | |
| 734 | + | |
699 | 735 | | |
700 | | - | |
701 | | - | |
702 | | - | |
703 | | - | |
704 | | - | |
705 | | - | |
706 | | - | |
707 | | - | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
708 | 765 | | |
709 | 766 | | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
710 | 774 | | |
711 | 775 | | |
712 | 776 | | |
| |||
881 | 945 | | |
882 | 946 | | |
883 | 947 | | |
884 | | - | |
| 948 | + | |
885 | 949 | | |
886 | 950 | | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
887 | 955 | | |
888 | 956 | | |
889 | 957 | | |
| |||
895 | 963 | | |
896 | 964 | | |
897 | 965 | | |
898 | | - | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
899 | 983 | | |
| 984 | + | |
900 | 985 | | |
901 | 986 | | |
902 | 987 | | |
| |||
1006 | 1091 | | |
1007 | 1092 | | |
1008 | 1093 | | |
1009 | | - | |
1010 | | - | |
1011 | | - | |
1012 | | - | |
1013 | | - | |
1014 | | - | |
1015 | | - | |
1016 | | - | |
1017 | | - | |
1018 | | - | |
1019 | 1094 | | |
1020 | | - | |
1021 | 1095 | | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
1022 | 1112 | | |
1023 | 1113 | | |
1024 | 1114 | | |
| |||
Lines changed: 13 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
| |||
89 | 92 | | |
90 | 93 | | |
91 | 94 | | |
92 | | - | |
| 95 | + | |
93 | 96 | | |
94 | 97 | | |
95 | 98 | | |
| |||
131 | 134 | | |
132 | 135 | | |
133 | 136 | | |
134 | | - | |
| 137 | + | |
135 | 138 | | |
136 | 139 | | |
137 | 140 | | |
| |||
206 | 209 | | |
207 | 210 | | |
208 | 211 | | |
209 | | - | |
| 212 | + | |
210 | 213 | | |
211 | 214 | | |
212 | 215 | | |
| |||
221 | 224 | | |
222 | 225 | | |
223 | 226 | | |
224 | | - | |
| 227 | + | |
225 | 228 | | |
226 | 229 | | |
227 | 230 | | |
| |||
335 | 338 | | |
336 | 339 | | |
337 | 340 | | |
338 | | - | |
| 341 | + | |
339 | 342 | | |
340 | 343 | | |
341 | 344 | | |
342 | 345 | | |
343 | 346 | | |
344 | | - | |
| 347 | + | |
345 | 348 | | |
346 | 349 | | |
347 | 350 | | |
| |||
1976 | 1979 | | |
1977 | 1980 | | |
1978 | 1981 | | |
1979 | | - | |
| 1982 | + | |
1980 | 1983 | | |
0 commit comments