简体   繁体   English

Xamarin.Forms:使用ServiceStack调用API会引发异常

[英]Xamarin.Forms: Call to a API using ServiceStack throws exception

I have a local service made with ServiceStack and I want to call a method from my Xamarin.Forms application: 我有一个使用ServiceStack进行的本地服务,我想从Xamarin.Forms应用程序中调用一个方法:

  ServiceStack.JsonServiceClient sc;
  sc = new ServiceStack.JsonServiceClient("http://api.sportsstars.local/");

When the debugger reaches the second line, it throws this exception: 当调试器到达第二行时,它将引发此异常:

System.TypeInitializationException: An exception was thrown by the type initializer for ServiceStack.ServiceClientBase System.TypeInitializationException:类型初始化为ServiceStack.ServiceClientBase引发了异常

Any ideas what could it be? 有什么想法吗?

Have you initialized the PCL Client ? 您是否已初始化PCL客户端 You can configure it by adding: 您可以通过添加以下内容进行配置:

ServiceStack.IosPclExportClient.Configure();

To your application on StartUp. 到您在StartUp上的应用程序。

I have found out how to avoid the exception. 我发现了如何避免异常。 I've put the code in the Droid project and used a DependencyService to call the method from my PCL project. 我已将代码放入Droid项目中,并使用DependencyService从PCL项目中调用该方法。 My problem was for Android. 我的问题是Android。 If you get the problem for IOS, the other answer may help you. 如果您遇到IOS的问题,则其他答案可能会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM