我正在尝试使用WCF REST
中的C#
服务。当我使用时,如果方法返回数组,如果我键入转换代码工作正常。但是当我试图以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&lt;&gt;列出&lt;&gt;并且Configure Service Reference
从数组到列表的服务类型仍然没有希望。
我该如何处理?哪里我错了?
答案 0 :(得分:0)
让代码工作了。应该在类型的对象和WCF中的返回类型中进行更改。像这样,
//代码:
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();