I was able to get this to work - here's what I did. First off, my environment is VS2010 and I was using the VS web server (Cassini).
I didn't change your method signature or your WebInvoke
attribute. The body of the GetAll()
method is this:
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "product/getall")]
Product[] GetAll()
{
Product[] prods = new Product[3] {
new Product() { Name="Foo", Desc="Bar"},
new Product() {Name="Ha", Desc="Ho"},
new Product() {Name="Who", Desc="What"}
};
return prods;
}
The web.config looks like this:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WebApplication2.ProdServiceAspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="WebApplication2.ProdService">
<endpoint address="" behaviorConfiguration="WebApplication2.ProdServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="WebApplication2.ProdService" />
</service>
</services>
</system.serviceModel>
Honestly, most of this is default out-of-the-box functionality. The only thing I changed was changing the endpointBehavior
to use webHttp
instead of the default enableWebScript
. Other than that, it worked fine.
EDIT (forgot the Product class): Here's the Product class - I decorated it with DataContract
and DataMember
attributes:
[DataContract]
public class Product
{
[DataMember()]
public String Name { get; set; }
[DataMember()]
public String Desc { get; set; }
}
This was my output:
[{"Desc":"Bar","Name":"Foo"},{"Desc":"Ho","Name":"Ha"},{"Desc":"What","Name":"Who"}]
My guess is that you are using enableWebScript
in your web.config. Change it to webHttp
and see what happens. The enableWebScript
setting is used for more ASMX type of compatibility (I believe). If you're returning JSON (which a lot of my GET service methods do), use webHttp
(which is what I'm used to using).
Let me know what you get and I'll update my answer accordingly.