The problem may just be that the example is slightly outdated. Also, the implementation class, NewtonsoftJsonBehavior
, is explicitly overriding and throwing an InvalidOperationException
within the Validate(ServiceEndpoint endpoint)
method.
Using Carlos' example, remove the validation:
public override void Validate(ServiceEndpoint endpoint)
{
base.Validate(endpoint);
//TODO: Stop throwing exception for default behavior.
//BindingElementCollection elements = endpoint.Binding.CreateBindingElements();
//WebMessageEncodingBindingElement webEncoder = elements.Find<WebMessageEncodingBindingElement>();
//if (webEncoder == null)
//{
// throw new InvalidOperationException("This behavior must be used in an endpoint with the WebHttpBinding (or a custom binding with the WebMessageEncodingBindingElement).");
//}
//foreach (OperationDescription operation in endpoint.Contract.Operations)
//{
// this.ValidateOperation(operation);
//}
}
Add a UriTemplate
to GetPerson
or other method:
[WebGet, OperationContract]
Person GetPerson();
[WebGet(UriTemplate="GetPersonByName?l={lastName}"), OperationContract(Name="GetPersonByName")]
Person GetPerson(string lastName);
Within the Service
class, add a simple implementation to validate that the arguments are parsed:
public Person GetPerson(string lastName)
{
return new Person
{
FirstName = "First",
LastName = lastName, // Return the argument.
BirthDate = new DateTime(1993, 4, 17, 2, 51, 37, 47, DateTimeKind.Local),
Id = 0,
Pets = new List<Pet>
{
new Pet { Name= "Generic Pet 1", Color = "Beige", Id = 0, Markings = "Some markings" },
new Pet { Name= "Generic Pet 2", Color = "Gold", Id = 0, Markings = "Other markings" },
},
};
}
In the Program.Main()
method, a call to this new URL will parse and return my query string value without any custom implementation:
[Request]
SendRequest(baseAddress + "/json/GetPersonByName?l=smith", "GET", null, null);
[Response]
{
"FirstName": "First",
"LastName": "smith",
"BirthDate": "1993-04-17T02:51:37.047-04:00",
"Pets": [
{...},
{...}
}