【问题标题】:Configuration System failed to initialize while trying to connect to web service尝试连接到 Web 服务时配置系统无法初始化
【发布时间】:2019-04-08 18:59:53
【问题描述】:

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

Web Service 在 Web Sever 中发布并运行,App 在 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 清单:

<?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();

【问题讨论】:

  • 您是否在清单中启用了 Internet 权限? DEBUG模式下默认启用,Release模式下不启用
  • 是的,我确实启用了它
  • 您是否也在设备上测试调试模式?您确定您的设备和服务器之间没有连接问题吗?
  • 到目前为止,我已经在两台设备上以 DEBUG 模式测试了该应用程序。在调试模式下,它在两个设备上都可以正常工作。安装 APK 后无法在任何设备上运行
  • 您可以尝试在发布模式下禁用链接器 - 可能会删除您需要的东西

标签: c# android web-services xamarin


【解决方案1】:

让我们检查一些东西,您在 Project -> Properties -> Android Manifest 中有 Internet 权限。

Android 选项 中将 Linking 设置为 仅 SDK 程序集



验证您的解决方案引用并尝试在制作 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);
}

【讨论】:

  • 在项目中这一切都很好,但是我仍然有同样的问题。
  • 我认为问题更多出在Web服务上,您需要验证它,您可以使用System.Net的HttpClient方法
猜你喜欢
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 2016-12-29
  • 2011-09-20
相关资源
最近更新 更多