8
        [OperationContract]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    Message GetSearchResults(string searchTerm, string searchType);

    [OperationContract]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    Message GetSearchResults(string searchTerm);

Is this possible - if not, can someone suggest an alternative?

Pones
  • 281
  • 1
  • 3
  • 12

3 Answers3

10

I've found that this was the best solution for me:

    [OperationContract(Name = "SearchresultsWithSearchType")]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType=null}", 
    ResponseFormat = WebMessageFormat.Xml)]
    Message GetSearchResults(string searchTerm, string searchType);


    [OperationContract(Name = "SearchresultsWithoutSearchType")]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}", 
    ResponseFormat = WebMessageFormat.Xml)]
    Message GetSearchResults(string searchTerm);

this matches:

"http://myservice/searchresults/mysearchterm"

"http://myservice/searchresults/mysearchterm/"

"http://myservice/searchresults/mysearchterm/mysearchtype"

northben
  • 4,682
  • 3
  • 31
  • 45
Pones
  • 281
  • 1
  • 3
  • 12
  • Does this really work for you? WCF doesn't usually allow two operations with the same name. – Doron Yaacoby Apr 21 '13 at 12:44
  • it did work for me - the `Name` property of the `OperationContract` attribute differentiates the two. However, the underlying methods still need different signatures. – northben May 24 '13 at 16:19
1

No, not really - because the string parameter searchType can be NULL - so you don't really have any way to distinguish the two URL templates. It would be different if you were using a non-nullable type, like an INT or something - then you (and the .NET runtime) could keep the two URL templates apart (based on the fact whether or not the INT is present).

What you need to do is just check whether searchType is empty or NULL in your GetSearchResults method, and act accordingly.

[OperationContract]
[WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
Message GetSearchResults(string searchTerm, string searchType);

and in your implementation:

public Message GetSearchResults(string searchTerm, string searchType)
{
   if(!string.IsNullOrEmpty(searchType))
   {
      // search with searchType
   }
   else
   {
      // search without searchType
   }
   ......
}
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
  • Thanks - my problem is: "http://myservice/searchresults/searchterm" or "http://myservice/searchresults/searchterm/" i.e. without the searchType part of the URL will not match the template above and returns a 404. Do i need to default the searchType parameter? – Pones Sep 09 '10 at 12:06
  • 1
    @pones: ok - hmm.... I was under the impression it would match that template. But seems you do need the two URI templates after all. Thanks for sharing your insights! – marc_s Sep 09 '10 at 13:05
0

I achieved this by using STREAM to pass data from client. You can even have 2 operations with same name but different method name. from front end make sure to set contentType as "text/javascript" OR "application/octet-stream", and try sending data as POST from HTML or in data variabke if using AJAX or jQuery

For Example

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "user/id/{id}/", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string UpdateUser(string id, System.IO.Stream stream);

[OperationContract]
[WebInvoke(Method = "DELETE", UriTemplate = "user/id/{id}/", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string DeleteUser(string id);

OR substitute PUT and DELETE for GET and POST

Guillaume Raymond
  • 1,328
  • 15
  • 28
AbhishekS
  • 71
  • 7