尝试连接到Web服务时,配置系统无法初始化

时间:2019-04-08 18:59:53

标签: c# android web xamarin service

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

Web服务已发布并在Web服务器中运行,在调试模式下,该应用程序可以连接并正常运行,没有错误。

当我生成APK文件并将其安装在其他设备上时,尝试登录时(首次应用调用网络服务),我收到以下错误消息。

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: 我仍然遇到问题,我开始认为它与我的Web服务器有关,因为它没有SSL证书。我会尝试弄一个,看看是否能解决问题。

1 个答案:

答案 0 :(得分:0)

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

enter image description here

Android选项中将链接设置为仅SDK程序集 enter image description here



验证解决方案参考,并在制作APK之前尝试清洁和重建所有解决方案。
请查看Microsoft的此示例,其中说明了如何使用ASMX Web Service in Xamarin,也许会有所帮助。

这是我发现消耗SOAP Web Service in Xamarin的一些代码。

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);
}