The below code was used to expose RESTful application using WCF. Here I pass the input through UriTemplate objects, which are used in the AddDetailsRequest class.
[ServiceContract]
public interface IMyRestService
{
[OperationContract(Name = "AddEmployee")]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "AddEmpDetails/{objReq}")]
AddDetailsResponse AddEmpDetails(AddDetailsRequest objReq);
}
Class AddDetailsRequest
[DataContract(Name = "AddDetailsRequest")]
public class AddDetailsRequest
{
[DataMember(IsRequired = true, Name = "Name")]
public string Name { get; set; }
[DataMember(IsRequired = true, Name = "City")]
public string City { get; set; }
}
What the problem I was faced here means, I am not able to pass the objects through UriTemplate. I want to pass the object, which has 2 parameters (Name and City), in a UriTemplate. How can I achieve this?
WCF UriTemplate does not support complex objects. In this case you have to stick with primary types from .NET. If you still want to use a complex object as parameter to your method you have to drop the UriTemplate and pass your object using JSON. If you really don’t want to drop the UriTemplate your last option is to use a string parameter:
Where
objEncodedReqis your serialized object (XML serialized fro example) and encoded using Base64. You would have to do this from client level.