【发布时间】:2014-07-22 07:40:19
【问题描述】:
我正在尝试使用C# 中的WCF REST 服务。当我使用时,如果方法返回数组并且如果我输入转换代码工作正常。但是当我尝试以List<> 的形式返回时,当我尝试输入转换时,它会抛出错误。
//客户端代码(使用数组):
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接口代码:
[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);
}
当我尝试使用具有相同 WCF 服务的 List 时,
//客户端代码:
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());
}
上面的代码在 (typeof(DataServiceProxy.List<Product>)) 部分抛出错误。
错误:
"The type or namespace 'List' does not exist in the namespace 'Web.DataServiceProxy'(are you missing an assembly reference?)"
我已尝试将 IList 更改为 List 并将服务的类型从数组返回到 Configure Service Reference 的列表,但仍然没有希望。
我该如何处理?我哪里错了?
【问题讨论】:
-
列表类型不在 DataServiceProxy 中,删除该限定符。