Bug 24679 - ServiceStack issue: PclExport.Instance needs to be initialized
Summary: ServiceStack issue: PclExport.Instance needs to be initialized
Status: CONFIRMED
Alias: None
Product: Android
Classification: Xamarin
Component: Tools and Addins ()
Version: 5.0
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-11-19 16:35 UTC by Fernando
Modified: 2015-03-05 11:29 UTC (History)
4 users (show)

Tags: PCL Android ServiceStack JsonServiceClient
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 24679 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Fernando 2014-11-19 16:35:04 UTC
I have a Xamarin.Forms portable solution, in a class in the PCL project I have this:

protected ServiceStack.JsonServiceClient sc;

in the constructor I have:

sc = new ServiceStack.JsonServiceClient(url);

I run the Android projet and when the execution reaches that code I am getting the error "PclExport.Instance needs to be initialized", what am I missing?

Thanks,

Complete error:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.TypeInitializationException: An exception was thrown by the type initializer for ServiceStack.ServiceClientBase ---> System.TypeInitializationException: An exception was thrown by the type initializer for ServiceStack.Text.Env ---> System.ArgumentException: PclExport.Instance needs to be initialized
[MonoDroid]   at ServiceStack.Text.Env..cctor () [0x00000] in <filename unknown>:0 
[MonoDroid]   --- End of inner exception stack trace ---
[MonoDroid]   at ServiceStack.ServiceClientBase..cctor () [0x00000] in <filename unknown>:0 
[MonoDroid]   --- End of inner exception stack trace ---
[MonoDroid]   at ServiceStack.JsonServiceClient..ctor (System.String baseUri) [0x00000] in <filename unknown>:0 
[MonoDroid]   at Scanpack.net.SAL.WebConnector..ctor (System.String factoryToken) [0x00000] in <filename unknown>:0 
[MonoDroid]   at Scanpack.net.BL.ServiceManager..ctor () [0x00000] in <filename unknown>:0 
[MonoDroid]   at Scanpack.net.PrintLabels..ctor () [0x00000] in <filename unknown>:0 
[MonoDroid]   at Scanpack.net.MainScreen+ctor>b__9>d__13.MoveNext () [0x00000] in <filename unknown>:0 
[mono] 
[mono] Unhandled Exception:
[mono] System.TypeInitializationException: An exception was thrown by the type initializer for ServiceStack.ServiceClientBase ---> System.TypeInitializationException: An exception was thrown by the type initializer for ServiceStack.Text.Env ---> System.ArgumentException: PclExport.Instance needs to be initialized
[mono]   at ServiceStack.Text.Env..cctor () [0x00000] in <filename unknown>:0 
[mono]   --- End of inner exception stack trace ---
[mono]   at ServiceStack.ServiceClientBase..cctor () [0x00000] in <filename unknown>:0 
[mono]   --- End of inner exception stack trace ---
[mono]   at ServiceStack.JsonServiceClient..ctor (System.String baseUri) [0x00000] in <filename unknown>:0 
[mono]   at Scanpack.net.SAL.WebConnector..ctor (System.String factoryToken) [0x00000] in <filename unknown>:0 
[mono]   at Scanpack.net.BL.ServiceManager..ctor () [0x00000] in <filename unknown>:0 
[mono]   at Scanpack.net.PrintLabels..ctor () [0x00000] in <filename unknown>:0 
[mono]   at Scanpack.net.MainScreen+ctor>b__9>d__13.MoveNext () [0x00000] in <filename unknown>:0 
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for ServiceStack.ServiceClientBase ---> System.TypeInitializationException: An exception was thrown by the type initializer for ServiceStack.Text.Env ---> System.ArgumentException: PclExport.Instance needs to be initialized
[mono-rt]   at ServiceStack.Text.Env..cctor () [0x00000] in <filename unknown>:0 
[mono-rt]   --- End of inner exception stack trace ---
[mono-rt]   at ServiceStack.ServiceClientBase..cctor () [0x00000] in <filename unknown>:0 
[mono-rt]   --- End of inner exception stack trace ---
[mono-rt]   at ServiceStack.JsonServiceClient..ctor (System.String baseUri) [0x00000] in <filename unknown>:0 
[mono-rt]   at Scanpack.net.SAL.WebConnector..ctor (System.String factoryToken) [0x00000] in <filename unknown>:0 
[mono-rt]   at Scanpack.net.BL.ServiceManager..ctor () [0x00000] in <filename unknown>:0 
[mono-rt]   at Scanpack.net.PrintLabels..ctor () [0x00000] in <filename unknown>:0 
[mono-rt]   at Scanpack.net.MainScreen+ctor>b__9>d__13.MoveNext () [0x00000] in <filename unknown>:0
Comment 1 Udham Singh 2015-01-14 09:42:23 UTC
I have checked this issue with the help of code snippet provided in bug description and observed the same behavior. To reproduce this issue I have followed the steps mentioned below :

1. Create a Xamarin.Forms portable solution.
2. Add nuget package 'ServiceStack.Client' in PCL project.
3. Implement the code provided in bug description in PCL project.
4. Set android project as start-up project.
5. Run the app and got the error mentioned in bug description.

Screencast : http://www.screencast.com/t/QuFun73Svco1

Application Output : https://gist.github.com/Udham1/4926002704b50aa2817f

Environment Info :

=== Xamarin Studio ===

Version 5.7 (build 661)
Installation UUID: 3235da96-1cb2-43d5-a836-234fd96911c2
Runtime:
	Microsoft .NET 4.0.30319.18449
	GTK+ 2.24.22 (MS-Windows theme)
	GTK# 2.12.26

=== Xamarin.Android ===

Version: 4.20.0 (Enterprise Edition)
Android SDK: D:\Backup_OldMachine\D Drive\SDK\android-sdk
	Supported Android versions:
		1.6    (API level 4)
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
Java SDK: C:\Program Files\Java\jdk1.7.0_67
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Build Information ===

Release ID: 507000661
Git revision: b70bab61da996da29045ea8ee8aed1a6faedbe78
Build date: 2015-01-05 11:27:37-05
Xamarin addins: 82f6c71490562d6cd125a09287f441902fdac3d7

=== Operating System ===

Windows 6.2.9200.0 (64-bit)
Comment 2 Fernando 2015-01-17 16:10:03 UTC
Adding an interface IWebConnector in PCL project and adding this class in Android and iOS projects is a solution for this:

using System;
using Xamarin.Forms;
using System.Collections.Generic;
using System.Threading.Tasks;
using Scanpack.net.Android;

#if __ANDROID__ 
[assembly: Dependency ( typeof ( WebConnector ))]

namespace Scanpack.net.Android
#endif

#if __IOS__
[assembly: Dependency ( typeof ( WebConnector ))]

namespace Scanpack.net.iOS
#endif
{
	public class WebConnector : IWebConnector
	{
        protected string url = "http://yourservice";
        protected ServiceStack.JsonServiceClient sc;

		public WebConnector () : base()
		{
			#if __ANDROID__ 
			ServiceStack.AndroidPclExportClient.Configure ();
			#endif
			#if __IOS__
			ServiceStack.IosPclExportClient.Configure(); 
			#endif

			sc = new ServiceStack.JsonServiceClient(url);

		}
Comment 3 Arefin 2015-03-04 22:51:35 UTC
Hi Fernando,

Where I will get the IWebConnector interface? Please let me know details. I am stuck in this issue from yesterday.

Thanks,
Arefin
Comment 4 Fernando 2015-03-05 04:46:26 UTC
Hi, you need to create one in the PCL project, for example:

   public interface IWebConnector
    {
        /// <summary>
        /// Connects to the web service to a testing service.
        /// </summary>
        Task<ServiceDebugEchoResponse> TestEchoAsync(string strData);
 }

then you add the WebConnector class (in my previous post) in Android and iOS project

in PCL project you use DependencyService to get an instance:

IWebConnector _wc = DependencyService.Get<IWebConnector>();


let me know if you need more help.

regards.
Comment 5 Arefin 2015-03-05 07:32:18 UTC
Thank you! But can you please share a sample application as I am very new in Xamarin? It will be really great for me..

I am not finding ServiceDebugEchoResponse class/namespace. Then where I will initialize IWebConnector _wc = DependencyService.Get<IWebConnector>(); 

Thanks,
Arefin
Comment 6 Fernando 2015-03-05 07:37:25 UTC
ServiceDebugEchoResponse is a class defined by me, you need to use a class that maps the json (in my case) the service sends as response. You can use any type you need in Task<T>, like string:

   public interface IWebConnector
    {
        /// <summary>
        /// Connects to the web service to a testing service.
        /// </summary>
        Task<string> TestEchoAsync(string strData);
 }

about:
Then where I will
initialize IWebConnector _wc = DependencyService.Get<IWebConnector>(); 

>>>>> in any place you need to use the WebConnector class to call a web service. You create the instance of WebConnector and then you use it like:

IWebConnector _wc = DependencyService.Get<IWebConnector>(); 
_wc.TestEchoAsync("test");

let me know if you need a sample I can try to create it.

regards,
Comment 7 Arefin 2015-03-05 07:42:17 UTC
Thanks again... I am getting your point now. But it will be really great if you can share a very simple app that is ServiceStack.Client using Xamarin.Forms PCL project.

-Arefin
Comment 8 Arefin 2015-03-05 07:51:27 UTC
Here is my email address - arefin.sust@gmail.com

Thanks!
Comment 9 Arefin 2015-03-05 11:29:37 UTC
Hi Fernando,

I am waiting for your sample code as you already solved the issue. I did exact same thing with Xamarin.iOS and Xamarin.Droid but not Xamarin.Forms.

Here is my email arefin.sust@gmail.com, so you can email me the sample code.

Thanks,
Arefin