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

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

问题描述

我正在使用一个控制台应用程序来承载一些Web服务的WebServiceHost。我在客户端应用程序添加一个服务引用,并创建代理,像这样:

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设置为包裹没有任何影响:

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

任何帮助是AP preciated。

Any help is appreciated.

谢谢!

推荐答案

看来你已经使用添加服务引用对话框中VS.创建的代理code VS ASR对话框不完全支持WCF休息,所以,代理code缺少 [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天全站免登陆