我有一个使用 Visual Studio 2017 制作的 Xamarin Android 应用程序和一个 ASMX Web 服务。
Web 服务在 Web 服务器上发布并运行,应用程序在 Debug模式下连接并运行良好,没有错误。
当我生成 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 list :
<?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 证书。我会尝试获取一个,看看是否能解决问题。
编辑 3:
SSL 证书没有解决问题。
编辑 4:
现在,如果我忽略第一个错误并尝试再次登录,我会收到此错误:
The method or operation is not implemented.
它在 DEBUG 模式下工作,因此实现了 Web 服务。
这两个错误都是在尝试创建 Web 服务实例时发生的:
WhseWS.Whse obj = new WhseWS.Whse();
最佳答案
让我们检查一些东西,您在 Project -> Properties -> Android Manifest 中拥有互联网权限。
在 Android 选项 中将Linking 设置为 SDK Assemblies Only
验证您的解决方案引用并尝试在制作 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);
}
关于c# - 配置系统在尝试连接到 Web 服务时初始化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55580085/