Xamarin Android - 使用复杂参数(对象)发出Rest请求抛出异常,在.NET中它工作正常(使用通道工厂)

时间:2013-07-25 11:07:23

标签: android wcf rest xamarin

我开始使用mono droid或Xamarin for Android,因此,我的想法是重用我已经在.NET中使用的大部分代码。

我需要我的android和ios应用程序做的事情之一就是使用带有json编码的wcf rest调用Web服务。

所以我的代码很简单:

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" });

足够简单让我开始(至少,这是我对单声道的想法,使.net中的.net重用)

但是当我运行此代码时,我得到以下异常异常:

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).

我在.NET 4.5应用程序中使用完全相同的代码并且它工作正常,它似乎在将对象转换为字符串时失败(我想是一个json字符串)。

它是否应该直接在单声道中工作,因为它适用于.NET?

你们有复杂输入对象的服务吗? 你用什么API来打电话?

感谢您的所有帮助,

Luis Pinho

1 个答案:

答案 0 :(得分:0)

它不起作用,因为代码中使用的某些对象尚未移植到Mono,因为它们是Microsoft特定的。这里有一些信息: Mono Class Status

例如,您的代码中用于Microsoft.EnterpriseManagement的此命名空间ISecurityManagement可能永远不会被移植。

要在Mono或.Net上工作,您的代码必须遵循.NET standard。这是一项协调Mono,.Net和.Net Core框架的工作,因此他们可以编译具有较少代码差异的公共库和程序。