[OperationContract] 方法中使用多个参数时,WCF 服务代理抛出异常 [英] WCF Service Proxy throws exception when more than one parameter is used in [OperationContract] method

查看:14
本文介绍了[OperationContract] 方法中使用多个参数时,WCF 服务代理抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WebServiceHost,用于在控制台应用程序中托管一些 Web 服务.我在我的客户端应用程序中添加了一个对它的服务引用,并像这样创建代理:

I have a WebServiceHost that is being used to host some web services in a console app. I added a service reference to it in my client app and create the proxy like so:

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();

它有效,但是一旦我添加了一个需要多个参数的新方法,我在创建代理时开始收到此异常(这是在任何通信发生之前):

It works, but once I added a new method that requires more than one parameter, I started getting this exception when the proxy was created (this was before any communication even took place):

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设置为wrapped没有效果:

Adding a WebInvokeAttribute and setting the BodyStyle to wrapped has no effect:

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

需要注意的是,我还有其他方法可以使用,但它们只有一个参数,因此不会出现上述问题.

It should be noted that I have other methods that work, but they only have a single parameter so they don't have the above problem.

仅供参考,以下是我设置主机的方法:

Just FYI, here's how I setup the host:

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)] 属性吗?

It seems you have created Proxy code using Add Service Reference dialog in VS. VS ASR dialog doesn't support WCF REST fully, so, the proxy code is missing [WebInvoke] attribute. Can you try adding [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)] attribute on operations in client proxy?

这篇关于[OperationContract] 方法中使用多个参数时,WCF 服务代理抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆