I'm trying to consume WCF REST
services in C#
. When i'm using, if the method returns array and if i type convert the code works fine. But when i tried to return as a List<>
and when i tried to type convert its throwing me error.
//Client Code(Using Array):
try
{
string ServiceUrl = "http://localhost:58092/Service1.svc/DataService/LoadAllDatas";
WebRequest wreq = WebRequest.Create(ServiceUrl);
WebResponse wres = wreq.GetResponse();
DataContractSerializer coll = new DataContractSerializer(typeof(DataServiceProxy.Product[]));
var arrProd = coll.ReadObject(wres.GetResponseStream());
DataServiceProxy.Product[] prd = arrProd as DataServiceProxy.Product[];
lstProd = new List<DataServiceProxy.Product>(prd);
}
catch (Exception)
{
throw;
}
//WCF Interface Code:
[ServiceContract]
public interface IDataService
{
[OperationContract]
[WebGet(BodyStyle=WebMessageBodyStyle.Wrapped,UriTemplate="LoadAllData")]
IList<Product> LoadAllData();
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "LoadAllDatas")]
Product[] LoadAllDatas();
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "LoadAllColumnData/{Id}")]
IList<GdColumns> LoadAllColumnData(string Id);
}
When i'm trying with List with the same WCF Service,
//Client Code:
try
{
string Service = "http://localhost:58092/Service1.svc/DataService/LoadAllData";
WebRequest wreq = WebRequest.Create(Service);
WebResponse wres = wreq.GetResponse();
DataContractSerializer coll = new DataContractSerializer(typeof(DataServiceProxy.IList<Product>));
var arrProd = coll.ReadObject(wres.GetResponseStream());
}
The above code throws error in the (typeof(DataServiceProxy.List<Product>)
) part.
Error:
"The type or namespace 'List' does not exist in the namespace 'Web.DataServiceProxy'(are you missing an assembly reference?)"
I have tried changing the IList<> to List<> and return type of the service from array to list by Configure Service Reference
still no hopes.
How can i handle this? Where i'm wrong?
Have made the code worked. The change should be made in the object of type and the return type in WCF. Like this,
//Code:
string Service = "http://localhost:58092/Service1.svc/DataService/LoadAllData";
WebRequest wreq = WebRequest.Create(Service);
WebResponse wres = wreq.GetResponse();
DataContractSerializer coll = new DataContractSerializer(typeof(IList<DataServiceProxy.Product>));
// MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(coll.));
var arrProd = coll.ReadObject(wres.GetResponseStream());
DataServiceProxy.Product[] prd = arrProd as DataServiceProxy.Product[];
lstProd = new List<DataServiceProxy.Product>(prd);
//WCF:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "LoadAllData")]
IList<Product> LoadAllData();