简体   繁体   中英

WCF Client does not follow contract UriTemplates

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...

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 ie accessing the service via SOAP and REST just add [OperationContract] along with the WebInvoke attribute and expose another endpoint element with basicHttpBinding

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM