tags:

views:

769

answers:

2

I m trying to rest-ify our existing WCF service and one of the hurdle is mapping complex type using Uritemplate. For example take a look at the below code

[DataContract]
public class Stock
{
    [DataMember]
    public string Symbol { get;set; }
    [DataMember]
    public double FaceValue { get; set; }
}

[ServiceContract]
public interface IRestService
{       
    [OperationContract]
    [WebGet(UriTemplate = "?Symbol={stk.Symbol}")]
    void Test1(Stock stk); 
}

The above Uritemplate declaration will definitely not work, but this is what is my intention of mapping the input query variable to one of the property of that object.. Is this possible ?

Tks in advance for your help.

A: 

I blogged about this back in the early days, unfortunately the UriTemplate architecture does not support mapping data contracts, just the basic .NET data types. :(

Drew Marsh
A: 

The example you give doesn't illustrate the problem. In that case, all that's needed is to pass a stock symbol, a simple string, and there's no need for a complex type.

In cases where you want to pass a more complex set of data, a JSON-encoded object, use WebInvoke with PUT or POST, a non-parameterized UriTemplate, and pass form data. See this answer for details.

In cases where you want GET, and can pass a small set of discrete parameters, you can use WebGet, an appropriate UriTemplate, and do the manual mapping. like so:

public enum OptionFlavor { Put, Call }
public class OptionInqury { public String Symbol; public String Month; public OptionFlavor Flavor;}

[OperationContract] 
[WebGet(UriTemplate = "/optionquote/{stockSymbol}/{month}/{flavor}")] 
void GetOptionPrice(string stockSymbol, string month, string flavor)
{
   var x = new OptionInquiry { 
       Symbol = stockSymbol, 
       Month = month,
       Flavor = (flavor.Equals("put")) ? OptionFlavor.Put : OptionFlavor.Call
   };
   // off you go...
}
Cheeso