WCF Service Proxy throws exception when more than one parameter is used in [OperationContract] method
Asked Answered
M

5

9

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.

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

Any help is appreciated.

Thanks!

Morie answered 3/12, 2010 at 14:35 Comment(3)
Hi, maybe my eyes are not working properly. But I don't see how you are calling the method. Can you post the code on how you are calling the method?Bise
@Alex - When you say calling the method, are you saying when I call setDeviceState from the client? If that's what you mean, I don't even get the chance to call it. The failure happens while setting up a proxy to the service before a call can even take place.Morie
I created a service and single parameters consumed it. Then I added the same method as you did with the same parameter and was able to consume it. Have you tried to delete the service from the client and add it back againBise
L
15

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?

Loner answered 3/12, 2010 at 15:29 Comment(3)
I added that to my ASR-generated code, in the Reference.cs file and it did indeed work. Is there a way to accomplish the same thing during the call stage instead of having to modify the reference.cs file? I'd hate to have to remember to update that file everytime I refresh my service....Morie
what do u mean by attribute on operations in client proxy? , I added them to the WCF library, and doesn't work see my questionQuicksand
@Morie even when adding it to the reference file, it keeps spitting the same error, as if nothing had changedAnguine
T
5

Two solutions I found:

If you can remove <webHttp/>

From

<behaviors>
  <endpointBehaviors>
    <behavior>
      <!--<webHttp/>-->
    </behavior>
  </endpointBehaviors>
</behaviors>

If you cannot, I had to add the attribute

[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)]

Above the Operation Contact Method

Toulouse answered 15/7, 2011 at 11:35 Comment(1)
but it never worked see my yquestion https://mcmap.net/q/430253/-wcf-strange-behaviourQuicksand
V
1

Try changing the hosting environment of your service within Visual Studio.
Change it from Use local IIS to 'Use Visual Studio Development Server'.

Apparently there is a different behaviour depending the hosting environment.

Vireo answered 16/10, 2012 at 12:15 Comment(0)
M
1

updating tha app.config in the client and setting the defaultBodyStyle as WrappedRequest worked for me, where as removing the webHttp cause other issues.

<behaviors>
  <endpointBehaviors>
    <behavior name="webHttp">
      <webHttp defaultBodyStyle="WrappedRequest"/>
    </behavior>
  </endpointBehaviors>
Messer answered 13/2, 2022 at 19:58 Comment(0)
G
0

I have another scenario for this issue, even if I added [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)] on the Operation Contact Method, it still doesn't work, I found this post is resolve my problem. http://www.codemeit.com/wcf/wcf-restful-pox-json-and-soap-coexist.html

What we need to do is simply locate the file “reference.cs” generated after adding service reference. Search keyword: “public interface ITestService” within the client project, you will find the generated service contract, add [System.ServiceModel.Web.WebGet] on top of the OperationContract.

So I had had added [System.ServiceModel.Web.WebGet] on the top of OprationContract on the proxy class of client side, it works.

Grof answered 4/2, 2016 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.