Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feedback]: ServiceModel.Channels.TextEncoderDefaults.ValidateEncoding throw ArgumentException with 'shift_jis' Maui.net (android) #5675

Open
JiakaiDou opened this issue Nov 6, 2024 · 1 comment
Assignees
Labels
Milestone

Comments

@JiakaiDou
Copy link

JiakaiDou commented Nov 6, 2024

Community Feedback: ServiceModel.Channels.TextEncoderDefaults.ValidateEncoding throw ArgumentException with 'shift_jis' Maui.net (android) - Developer Community

Description: ServiceModel.Channels.TextEncoderDefaults.ValidateEncoding throw ArgumentException with 'shift_jis' Maui.net (android),reported encountering an exception while trying to use a WCF client in MAUI .NET: System.ArgumentException: テキスト メッセージ形式に使用されているテキスト エンコード ‘shift_jis’ はサポートされていません。 (Parameter ‘encoding’)

Double reviewing the relevant code in ServiceModel.Channels.TextEncoderDefaults.ValidateEncoding, which indicates that only UTF8, Unicode, and BigEndianUnicode encodings are supported . The encoding the customer is using is shift_jis(a character encoding for the Japanese language).

Ref: Shift JIS - Wikipedia

@antoniohlopes
Copy link

antoniohlopes commented Nov 6, 2024

Hello, JiakaiDou
Sorry, but I think the problem is not that simple.
I have commented the 'shift_jis' source code that register the japanese encoding

public static class MauiProgram
{
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .UseSkiaSharp()
                .UseMauiCommunityToolkit()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });
            builder.Services.AddSingleton<MainPage>();
            // commented here to avoid
            // System.ArgumentException: テキスト メッセージ形式に使用されている
            // テキスト エンコード ‘shift_jis’ はサポートされていません。 (Parameter ‘encoding’)
            //ShiftJisEncoding.RegisterShiftJis(); (Line 34)
            return builder.Build();
        }
 }

The BasicHttpBinding() worked without problems in this way.

My old wcf service not have reference to 'shift_jis' encoding.
But, what is the relationship between EncodingProvider' class and the "BasicHttpBinding" class ? (I have commented the line 34 of MauiProgram.cs)

Even so, the wcf client (android) failed when connecting to the server ("Connection failure")
The Windows version does not fail, it connects and returns the data.

My conclusion is that Maui.net (Android) is not WCF compatible. Is this right? (NO)
Correction :
After append in "AndroidManifest.xml" in the <application android:allowBackup="true" ... /> element an 'android:usesCleartextTraffic = "true"'

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
	<application android:allowBackup="true" android:icon="@mipmap/appicon" android:supportsRtl="true" 
				 android:usesCleartextTraffic="true"></application>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />
</manifest>

WCF client (basicHttpBinding) have worked for me.

PS : ShiftJisEncoding class

    public class ShiftJisEncoding : EncodingProvider
    {
        public static void RegisterShiftJis()
        {
            try
            {
                ShiftJisEncoding jap = new ShiftJisEncoding();
                Encoding.RegisterProvider(jap);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }

        public ShiftJisEncoding()
        {
        }

        static private Encoding jp;

        public override Encoding? GetEncoding(string name, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
        {
            return base.GetEncoding(name, encoderFallback, decoderFallback);
        }

        public override Encoding GetEncoding(int codepage)
        {
            if (jp is null)
            {
                jp = CodePagesEncodingProvider.Instance.GetEncoding(codepage);
            }
            return jp;
        }

        public override Encoding GetEncoding(string name)
        {
            if (jp is null)
            {
                jp = CodePagesEncodingProvider.Instance.GetEncoding(name);
            }
            return jp;
        }
    }

PPS: "Connection failure" can be a better error message, even when debugging; for example, adding this
Try append in "AndroidManifest.xml" in the <application android:allowBackup="true" ... /> element an 'android:usesCleartextTraffic = "true"' in your AndroidManifest.xml

@HongGit HongGit added this to the 10.0 milestone Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants