Reputation: 75
I am attempting to build a Restful WCF Service. In my WCF Service I am referencing 1 dll which contains the Data Access Layer.
I have the following method in my WCF class
public class Search : ISearch
{
public List<Product> GetProductList()
{
ProductDA dataAccess = new ProductDA();
List<Product> obj = new List<Product>();
obj = dataAccess.GetProducts();
return obj;
}
}
My interface holds the OPerationContract as follows:
[ServiceContract]
public interface ISearch
{
[OperationContract]
[WebGet(UriTemplate = "getProductList", RequestFormat = WebMessageFormat.Xml, ResponseFormat WebMessageFormat.Xml)]
List<Product> GetProductList();
}
When I run the url http://localhost:36741/RestService/Search.svc/getFoodTruckNameList in the browser I receive the following error: The server encountered an error processing the request
Does anyone know the correct code to call the [OperationContract] for List<Product> GetProductList();
?
Thanks in advance. Been pulling my hair out on this one for a while..
Upvotes: 2
Views: 1563
Reputation: 18965
Looking at my RESTful web services that do something like this, I typically do something closer to:
[CollectionDataContract]
public class Products : List<Product>
{
public Products(IEnumerable<Product> products) : base(products) { }
}
Then your interface would be like:
[ServiceContract]
public interface ISearch
{
[OperationContract]
[WebGet(UriTemplate = "getProductList", RequestFormat = WebMessageFormat.Xml, ResponseFormat WebMessageFormat.Xml)]
Products GetProductList();
}
And then your service contract would look like:
public class Search : ISearch
{
public Products GetProductList()
{
return new Products(new ProductDA().GetProducts());
}
}
Upvotes: 3
Reputation: 65371
We have often had problems returning a Generic list as the response to a WCF service.
The simple way to solve it is to create a class which has a single property which is the Generic list, and then return this class.
[DataContract]
public class Class1
{
[DataMember]
public List<Foo> Foos { get; set; }
}
Upvotes: 0