0
votes

I am using the C# ServiceStack PCL Client in Xamarin to attempt to map a response DTO from my remote service to my client's domain data model. The service works great and I am able to successfully get the DTO. However, it seems that no matter how simple I make the DTO or Domain Model, I still get an ArgumentException when I call either ConvertTo or PopulateWith in order to covert that DTO to my local Domain Model. So, I have simplified my test down to an extremely simple test which still fails. Here are my data objects:

public class TestDto
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class TestModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

The convert call is just as simple:

var testDto = new TestDto() {FirstName = "First", LastName = "Last"};
var testModel = testDto.ConvertTo<TestModel>();

When I call ConvertTo, I get System.ArgumentException with the following message:

Method 'Void set_FirstName(System.String)' declared on type 'Test.Core.Models.TestModel' cannot be called with instance of type 'System.String'

Calling var testModel = new TestModel().PopulateWith(testDto); results in the same exception. So does changing the data types of the properties from string to int, bool, etc.

I have searched all over and it feels like I'm doing exactly what I'm supposed to according to ServiceStack, their examples, and everything I've found on Stack Overflow. See the links here:

(Note that ConvertTo was formerly named TranslateTo in old ServiceStack versions)

So the fact that I'm getting this exception makes me feel like I have missed something incredibly basic with my project setup or that I missed a step where I somehow need to configure my objects or ServiceStack to enable the conversion. I know I could use Automapper but I would rather not have to pull in another library when ServiceStack already claims to have the capability built in. However, I'm not sure what to do at this point. Any ideas what could cause this or suggestions where I should look?

1
I'd recomment to use automapper it's a more robust implpedrommuller
What platform are you getting this error on?mythz
The ServiceStack Client code itself is in a Xamarin PCL. My testing has only been on Android so far.thedigitalsean

1 Answers

0
votes

This is due to a bug in Android implementation of Compiled Expressions. In the meantime you can workaround this by disabling use of compiled expressions with:

PclExport.Instance.SupportsExpression = false;

A fallback was added in the next v4.0.32+ release of ServiceStack that's now on MyGet that will automatically fallback to using reflection when compiling the property expression fails.