尝试连接网络服务时,配置系统初始化失败。

4

我有一个使用Visual Studio 2017制作的Xamarin Android应用和一个ASMX Web服务。

这个Web服务已经发布并在Web服务器上运行。当在调试模式下时,该应用成功连接且没有任何错误。

但是当我生成APK文件并将其安装到其他设备上时,尝试登录(第一次应用程序调用Web服务)时会收到以下错误:

Configuration system failed to initialize

System.TypeInitializationException: 
The type initializer for 'System.Uri' threw an exception. ---> 
System.TypeInitializationException: 
The type initializer for 'System.UriParser' threw an exception. ---> 
System.MissingMethodException:
Method not found: bool System.Runtime.Versioning.BinaryCompatibility.get_TargetsAtLeast_Desktop_V4_5()
--- End of inner exception stack trace --- 
 at (wrapper managed-to-native)
System.Object.__icall_wrapper_mono_generic_class_init(intptr)
 at System.Uri..cctor () [0x00000] in <988fa07610c94365ae6d295a6aa379fe>: 0
--- End of inner exception stack trace ---
 at System.Configuration.ConfigurationManager.EnsureConfigurationSystem() [0x00022] in <673a29f4914b4711bafe0a24a4318f71>: 0

我已经尝试了最常见的解决此错误的方法,但没有成功。
<configSections>
    <sectionGroup name="applicationSettings"
                  type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

      <section name="DCSWhseWS.Properties.Settings"
               type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
               requirePermission="false" />

    </sectionGroup>

这是我使用的Web.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings"
                  type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

      <section name="DCSWhseWS.Properties.Settings"
               type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
               requirePermission="false" />

    </sectionGroup>

  </configSections>
  <system.web>
    <customErrors mode="Off"/>
    <compilation debug="true" targetFramework="4.6.1"/>
    <httpRuntime maxRequestLength="10240" executionTimeout="1200"/>
    <pages controlRenderingCompatibilityVersion="4.0"/>
  </system.web>  
  <connectionStrings>
    <add name="ConnString" connectionString="Connection String"/>
    <add name="ConnStringTest" connectionString="Connection String for Testing"/>
  </connectionStrings>
</configuration>

以下是我如何从Android应用程序调用Web服务:

按钮:

        private async void btnLogin_Click(object sender, EventArgs e)
        {
            await Task.Run(() =>
            {
                clsUser u = new clsUser();
                u = ServiceCalls.Login(txtUser.Text);

                if (txtPass.Text.Trim().ToUpper() == u.Password.ToUpper())
                {
                    Intent i = new Intent(this, typeof(MainActivity));
                    i.PutExtra("UserAccount", JsonConvert.SerializeObject(u));
                    StartActivity(i);
                    Finish();
                }
                else
                    RunOnUiThread(() =>
                    {
                        Alert.ShowAlert("Invalid Password. please verify.", this).Show();
                    });
            });
        }

ServiceCalls.cs

internal static clsUser Login(string UserCode)
{
    WhseWS.Whse obj = new WhseWS.Whse();
    return JsonConvert.DeserializeObject<clsUser>(obj.Login(UserCode));
}

再次说明,当我在调试应用程序时,此错误不会发生,只有在将APK安装到任何设备上时才会出现。

编辑:

这是Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="DCS.Whse" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:allowBackup="true" android:label="Warehouse" android:icon="@drawable/logo"></application>
</manifest>

编辑2:

我仍然遇到问题,开始觉得这可能与我的网络服务器有关,因为它没有SSL证书。我会尝试获取一个SSL证书来解决这个问题。

编辑3:

SSL证书并没有解决这个问题。

编辑4:

现在,如果我忽略第一个错误并尝试重新登录,我会遇到以下错误:

The method or operation is not implemented.

调试模式下Web服务已经实现。

在尝试创建Web服务实例时,两种错误都会发生:

WhseWS.Whse obj = new WhseWS.Whse();

是的,我已经启用它了。 - Renato Wong
你在设备上测试调试模式了吗?你确定你的设备和服务器之间没有连接问题吗? - Jason
1
你可以尝试在发布模式下禁用链接器 - 可能会有一些需要的内容被剥离了。 - Jason
是的,它被设置为无。如果链接器未设置为无,VS将不允许我创建APK文件。 - Renato Wong
这听起来不太对。我认为这与您的主要问题无关,但链接器选项是在发布模式下控制 APK 大小的主要方式之一。 - Jason
显示剩余6条评论
1个回答

0

让我们来检查一些东西,你在 项目 -> 属性 -> Android清单中拥有互联网权限。

输入图片说明

Android选项 中将链接设置为只有SDK程序集 输入图片说明



验证您的解决方案引用,并在生成APK之前尝试清理和重建所有解决方案。
查看Microsoft的这个示例,其中解释了如何在Xamarin中使用ASMX Web服务,也许会有帮助。

这是我找到的一些代码,用于消费 Xamarin 中的 SOAP Web 服务

try
{
    WhseWS.Whse obj = new WhseWS.Whse();
    obj.Url = "http://yourIPAddress:Port#/xyz.asmx";
    return JsonConvert.DeserializeObject<clsUser>(obj.Login(UserCode));
}
catch (Exception ex){
    Console.WriteLine("Exception : " + ex);
}

在项目中这一切都很好,然而我仍然有同样的问题。 - Renato Wong
我认为问题更多地出现在Web服务上,您需要进行验证,可以使用System.Net中的HttpClient方法。 - Ivan-San

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接