微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 当在[OperationContract]方法中使用多个参数时,WCF服务代理引发异常

我有一个WebServiceHost用于在控制台应用程序中托管一些Web服务.我在客户端应用程序中添加一个服务引用,并创建了如下代理:
var binding = new WebHttpBinding();
var endPoint = new EndpointAddress(string.Format(Settings.serviceBase,Settings.wcfPort));

ChannelFactory<IzWaveSVC> factory = new ChannelFactory<IzWaveSVC>(new WebHttpBinding(),endPoint);

factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
// **Exception occurs here**
var proxy = (IzWaveSVC)factory.CreateChannel();

它可以工作,但一旦我添加一个需要多个参数的新方法,当代理创建时,我开始收到这个异常(甚至发生任何通信之前):

Operation 'setDeviceState' of contract 'IzWaveSVC' specifies multiple request 
body parameters to be serialized without any wrapper elements. At most one 
body parameter can be serialized without wrapper elements. Either remove the 
extra body parameters or set the BodyStyle property on the WebGetAttribute / 
WebInvokeAttribute to Wrapped.

添加WebInvokeAttribute并将BodyStyle设置为wrap不起作用:

[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]        
bool setDeviceState(byte nodeId,bool powered,byte level);

应该注意的是,我有其他的方法可以工作,但它们只有一个参数,所以他们没有上述问题.

只是在FYI,我是如何设置主机:

endPoint = new EndpointAddress(string.Format(Settings.serviceBase,port));
binding = new WebHttpBinding();

host = new WebServiceHost(singletonObject,new Uri(string.Format(Settings.serviceBase,port)));

host.AddServiceEndpoint(typeof(IzWaveSVC),binding,""); 
ServiceMetadataBehavior mexBehavior = new ServiceMetadataBehavior();                
mexBehavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(mexBehavior);                
host.AddServiceEndpoint(typeof(IMetadataExchange),MetadataExchangeBindings.CreateMexHttpBinding(),endPoint.Uri.AbsoluteUri + "mex");    
host.open();

任何帮助是赞赏.

谢谢!

解决方法

似乎您已经在VS中使用“添加服务引用”对话框创建了代理代码. VS ASR对话框不完全支持WCF REST,所以代理代码缺少[WebInvoke]属性.您可以尝试在客户端代理中的操作中添加[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]属性吗?

原文地址:https://www.jb51.cc/csharp/92956.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐