Wednesday, June 21, 2017

Xamarin Android - Making a Rest request with a complex parameter (object) throws exception, in .NET it works fine (Using channel factory)

Leave a Comment

I'm starting to use mono droid or Xamarin for Android, so, my idea was to reuse most of the code that I already use in .NET.

One of the things I need my android and ios application to do is to make calls to web services made available using wcf rest with json encoding.

So my code is simple:

WebHttpBinding webBinding = new WebHttpBinding(); EndpointAddress endPointAddress = new EndpointAddress("http://192.168.126.24:8025/Services/SecurityManagement"); ChannelFactory<ISecurityManagement> newFactory = new ChannelFactory<ISecurityManagement>(webBinding, endPointAddress);  newFactory.Endpoint.Behaviors.Add(new WebHttpBehavior() { DefaultOutgoingRequestFormat = System.ServiceModel.Web.WebMessageFormat.Json, DefaultOutgoingResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json });  newFactory.Endpoint.Behaviors.Add(new RestEndPointBehavior());  ISecurityManagement newProxy = newFactory.CreateChannel(); ValidateUserExistenceOutput output = newProxy.ValidateUserExistence(new ValidateUserExistenceInput() { Domain = "CRITICAL", Username = "myUserName" }); 

Simple enough to get me started (at least, that was my idea about mono, make in .net reuse in mono)

But when I run this code I get the following exception exception:

System.NotSupportedException: Loading... 07-25 10:43:40.922 E/mono    ( 1950):  07-25 10:43:40.922 E/mono    ( 1950): Unhandled Exception: 07-25 10:43:40.922 E/mono    ( 1950): System.NotSupportedException: Conversion from the argument parameterType 'BusinessOrchestration.SecurityManagement.InputObjects.ValidateUserExistenceInput' is not supported 07-25 10:43:40.922 E/mono    ( 1950):   at System.ServiceModel.Dispatcher.QueryStringConverter.ConvertValueToString (System.Object parameter, System.Type parameterType) [0x00000] in <filename unknown>:0  07-25 10:43:40.922 E/mono-rt ( 1950):   at System.ServiceModel.Dispatcher.WebMessageFormatter+WebClientMessageFormatter.SerializeRequest (System.ServiceModel.Channels.MessageVersion messageVersion, System.Object[] parameters) [0x00000] in <filename unkno07-25 10:43:40.922 E/mono    ( 1950):   at System.ServiceModel.Dispatcher.WebMessageFormatter+WebClientMessageFormatter.SerializeRequest (System.ServiceModel.Channels.MessageVersion messageVersion, System.Object[] parameters) [0x00000] in <filename unknown>:0  07-25 10:43:40.922 E/mono    ( 1950):   at System.ServiceModel.Description.WebHttpBehavior+ClientPairFormatter.SerializeRequest (System.ServiceModel.Channels.MessageVersion messageVersion, System.Object[] parameters) [0x00000] in <filename unknown>:0  07-25 10:43:40.922 E/mono    ( 1950):   at System.ServiceModel.MonoInternal.ClientRuntimeChannel.CreateRequest (System.ServiceModel.Dispatcher.ClientOperation op, System.Object[] parameters) [0x00000] in <filename unknown>:0  07-25 10:43:40.922 E/mono    ( 1950):   at System.ServiceModel.MonoIn 07-25 10:43:40.922 E/mono-rt ( 1950): [ERROR] FATAL UNHANDLED EXCEPTION: System.NotSupportedException: Conversion from the argument parameterType 'BusinessOrchestration.SecurityManagement.InputObjects.ValidateUserExistenceInput' is not supported 07-25 10:43:40.922 E/mono-rt ( 1950):   at System.ServiceModel.Dispatcher.QueryStringConverter.ConvertValueToString (System.Object parameter, System.Type parameterType) [0x00000] in <filename unknown>:0  07-25 10:43:40.922 E/mono-rt ( 1950):   at System.ServiceModel.Dispatcher.WebMessageFormatter+WebClientMessageFormatter.SerializeRequest (System.ServiceModel.Channels.MessageVersion messageVersion, System.Object[] parameters) [0x00000] in <filename unknown>:0  07-25 10:43:40.922 E/mono-rt ( 1950):   at System.ServiceModel.Description.WebHttpBehavior+ClientPairFormatter.SerializeRequest (System.ServiceModel.Channels.MessageVersion messageVersion, System.Object[] parameters) [0x00000] in <filename unknown>:0  07-25 10:43:40.922 E/mono-rt ( 1950):   at System.ServiceModel.MonoInternal.ClientRuntimeChannel.CreateRequest (System.ServiceModel.Dispatcher.ClientOperation op, System.Object[] parameters) [0x00000] in <filename unknown>:0  07-25 10:43:40.922 E/mono-rt ( 1950):   at System.Servic The program 'Mono' has exited with code 0 (0x0). 

I'm using the exact same code in a .NET 4.5 application and it works fine, it seems that its failing when converting the object to a string (a json string, I suppose).

Should it work directly in mono since it works in .NET?

Do you guys have services with complex input objects? What api do you use to make these calls?

Thanks ahead for all your help,

Luis Pinho

1 Answers

Answers 1

It doesn't work because some of the object used in your code hasn't been ported to Mono since they are Microsoft specific. There is some information here : Mono Class Status

For example, this namespace Microsoft.EnterpriseManagement used for ISecurityManagement in your code will probably never be ported.

To work on both Mono or .Net your code must follow the .NET standard. It's a work in progress to harmonize Mono, .Net and .Net Core framework so they could compile common libraries and programs with less code differences.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment