Forum Cross Platform with Xamarin

Call to a rest service with json with a content type format = raw results in InvalidCastException

Hi,

we have a set of services made available using a self-hosted .net application.

to make them available to mobile, we created a new rest/json endpoints, I used the following example ([blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx](Carlos Figueira example - WCF extensibility message formatters)) as a baseline, because I wanted to use json.net and, in this example the body content is only serialized once (some examples converted the xml into json and changed body content serializing every object twice).

This example uses raw content format.

To set the content type mapper in .NET to RAW we set the content type mapper with the WebGttpBinding object, in mono I had to created a custom binding based on WebHttpBinding (the ContentTypeMapper property seems to be missing from the WebHttpBinding type in Mono).

Using the same code in mono and in .NET the results differ, in .NET everything works, in monodroid it throws an exception:

System.InvalidCastException: Cannot cast from source type to destination type.

07-24 16:47:05.686 I/MonoDroid( 2494): UNHANDLED EXCEPTION: System.InvalidCastException: Cannot cast from source type to destination type.
07-24 16:47:05.686 I/MonoDroid( 2494): at System.ServiceModel.Channels.WebMessageEncoder.WriteMessage (System.ServiceModel.Channels.Message message, System.IO.Stream stream) [0x00000] in :0
07-24 16:47:05.686 I/MonoDroid( 2494): UNHANDLED EXCEPTION: System.InvalidCastException: Cannot cast from source type to destination type.07-24 16:47:05.686 I/MonoDroid( 2494): at System.ServiceModel.Channels.HttpRequestChannel.BeginProcessRequest (System.ServiceModel.Channels.HttpChannelRequestAsyncResult result) [0x00000] in :0
07-24 16:47:05.686 I/MonoDroid( 2494): at System.ServiceModel.Channels.HttpRequestChannel.BeginRequest (System.ServiceModel.Channels.Message message, TimeSpan timeout, System.AsyncCallback callback, System.Object state) [0x00000] in :0
07-24 16:47:05.686 I/MonoDroid( 2494): at System.ServiceModel.Channels.HttpRequestChannel.Request (System.ServiceModel.Channels.Message message, TimeSpan timeout) [0x00000] in :0
07-24 16:47:05.686 I/MonoDroid( 2494): at System.ServiceModel.MonoInternal.ClientRuntimeChannel.Request (System.ServiceModel.Channels.Message msg, TimeSpan timeout) [0x00000] in :0
07-24 16:47:05.686 I/MonoDroid( 2494): at System.ServiceModel.MonoInternal.ClientRuntimeChannel.Request (System.ServiceModel.Description.OperationDescription od, System.Object[] parameters) [0x00000] in :0
07-24 16:47:05.686 I/MonoDroid( 2494): at System.ServiceModel.MonoInternal.ClientRuntimeChannel.DoProcess (System.Reflection.MethodBase method, System.String operationName, System.Object[] parameters) [0x00000] in :0
07-24 16:47:05.686 I/MonoDroid( 2494): at System.ServiceModel.MonoInternal.ClientRuntimeChannel.Process (System.Reflection.MethodBase method, System.String operationName, System.Object[] parameters) [0x00000] in :0
<

I know what an invalid cast is, but the error is being thrown inside the WriteMessage code, is this a mono problem?

I'm new at this mobile stuff, and I'm having some problems trying to understand the error, can anyone give me a hand to sort this out?
-> If you guys have some suggestions that involve a simpler way to use json.net to encode the message, I'm open to suggestions.

Thanks,

Luis Pinho

Posts

  • lpinholpinho USMember ✭✭

    Just a few details, this happens before writing the message body (I've got a break point in the OnWriteBodyContents of the Body Writer and the breakpoint isn't reached), so, the message doesn't reach the server.

  • lpinholpinho USMember ✭✭

    Any help? Should I rewrite the request?

  • GaryMedinaGaryMedina USUniversity

    Hey Luis,
    I am diving in myself and having the same issue.

    var jsonObjects = (JsonArray) JsonArray.Load(reader);

    JsonObject works fine for a single object but errors out with multiple. Did you resolve and how?

    Thanks,
    Gary

  • lpinholpinho USMember ✭✭

    I gave up and went another way :(

    I'm using WCF Rest ($h#tty solution, I know), but I couln't get any help anywhere

Sign In or Register to comment.