Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this WCF service not recognizing the UriTemplate parameter?

I've created the following RESTful WCF service, which works just fine when running it in VS.

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, 
    UriTemplate = "/sales/start={start}&end={end}")]
List<Sales> GetSalesByDate(string start, string end);

However, when deploying this to my test server (running Win2K3 and IIS6) I received the following server error:

Operation 'GetSalesByDate' in contract 'ISalesService' uses GET, but also has body parameter 'start'. GET operations cannot have a body. Either make the parameter 'start' a UriTemplate parameter, or switch from WebGetAttribute to WebInvokeAttribute.

Obviously I have already made 'start' a UriParameter. So can anyone tell me why an exception is being thrown?

EDIT: For reference, here is my configuration file:

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <services>
            <service name="Services.SalesService">
                <endpoint behaviorConfiguration="webBehavior" 
                          binding="webHttpBinding" 
                          contract="Services.ISalesService"/>
            </service>
        </services>
        <behaviors>
            <endpointBehaviors>
                <behavior name="webBehavior">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>
like image 897
Kevin Babcock Avatar asked Feb 12 '09 23:02

Kevin Babcock


People also ask

What is UriTemplate in WCF?

UriTemplate and UriTemplateTable form the basis of the URI-based dispatch engine in WCF. These classes can also be used on their own, allowing developers to take advantage of templates and the URI mapping mechanism without implementing a WCF service.

What is WebInvoke method in WCF?

WebInvoke Method Type = POST as we are implementing POST. URI Template defines the URL format by which this method is identified/linked. Note: MethodName, URI Template name & Operation Contract names may not be same means, they can be different. PostSampleMethod will accept XML string as input in POST method.


2 Answers

It turns out /sales/start={start}&end={end} is not a valid Uri (duh!). After a little trial and error I finally figured this out. Tweaking the UriTemplate with a '?' solved the problem.

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, 
    UriTemplate = "/sales/?start={start}&end={end}")]
List<Sales> GetSalesByDate(string start, string end);

Thanks for your help!

like image 103
Kevin Babcock Avatar answered Oct 13 '22 20:10

Kevin Babcock


I know it is really late but why didn't you use the following format.

UriTemplate = "/sales/{start}/{end}"
like image 4
Onur Topal Avatar answered Oct 13 '22 20:10

Onur Topal