0

I have defined a function in the ServiceContract of my WCF service as followed:

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/test")]
CommandResponse Test();

And on the other end, at the client app I added the WCF service via "Add service reference" in visual studio. When calling the service test function using:

var test = m_ServiceClient.Test();

I am getting an error saying:

Operation 'GetUser' of contract... specifies multiple request body parameters to be serialized ...

The GetUser() is an other function I have in the service contract (also a GET function but with parameters in the UriTemplate). My guess is that the client is calling the function with it's parameters as it should but the request is going to the wrong UriTemplate (or with no template at all and it just jumps to some kind of default).

Any special instructions I have to follow to let the client know about the functions UriTemplates ?

I've search all over and could not find a single page that helps with this issue...

Idan
  • 2,819
  • 4
  • 33
  • 61
  • The webInvoke attribute is used when you expose your service via WebHttpBinding which is for consuming your service in REST style. In order to access the method via SOAP add "OperationContract" attribute and expose it via basicHttpBinding – Rajesh Aug 07 '12 at 08:37

2 Answers2

2

The WebInvoke/WebGet attributes are used when you expose your service via WebHttpBinding which is for consuming your WCF service in REST style. In order to access the method via SOAP add [OperationContract] attribute and expose a endpoint via basicHttpBinding.

If you would want to access the service in a REST style then you should use the HttpWebRequest class to create your request rather than adding a Add Service Reference.

For achieving both i.e accessing the service via SOAP and REST just add [OperationContract] along with the WebInvoke attribute and expose another endpoint element with basicHttpBinding

Rajesh
  • 7,766
  • 5
  • 22
  • 35
  • I do want to access the service via REST. Added the [OperationContract] to the function and still i get the same results. When using the browser to access the service all is working fine, my issue is only with visual studio auto generated client proxy. – Idan Aug 07 '12 at 12:12
  • Have you added the endpoint element in your servers config file that exposes your WCF service using SOAP – Rajesh Aug 07 '12 at 15:21
  • I am not using SOAP. I am using REST only. – Idan Aug 08 '12 at 08:00
  • If you are trying to use REST you dont need to Add a service reference to the WCF Service rather you should use HttpWebREquest class to make a request to the WCF Service – Rajesh Aug 08 '12 at 08:09
0

For the client to recognize the UriTemplates and pass each method to it's own Url in the format defined by the template I copied the ServiceContract Interface to the client and then created a channel to the service base url

WebChannelFactory<IServiceContract> cf = new WebChannelFactory<IServiceContract>(new Uri("http://...."));
var service = cf.CreateChannel();

The resulting 'service' is a usable interface that works directly with the web service.

Idan
  • 2,819
  • 4
  • 33
  • 61