我有一个使用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();