【发布时间】:2011-03-17 06:47:14
【问题描述】:
我正在尝试使用 ASMX/WCF 在站点(公共/私有)之间传递对象。我可以从我的私有 ASMX 服务获取序列化对象到我的公共 WCF 服务,但我无法反序列化该对象。下面的代码后跟错误。
调用私有 ASMX 服务的 WCF 服务。
[WebGet(UriTemplate = "{part}")]
public Distributor GetDistributorInventory(string part)
{
const string url = "http://www.site.com/service/lookup.asmx/StockCheck?part=" + part;
//This is a wrapper for a HttpWebRequest that returns a string
string results = WebHelper.HttpRequest("GET", "text/xml", null, url, new CookieContainer());
byte[] byteArray = Encoding.ASCII.GetBytes(results);
MemoryStream stream = new MemoryStream(byteArray);
DataContractSerializer deserialize = new DataContractSerializer(typeof(Distributor));
return (Distributor)deserialize.ReadObject(stream);
}
用于公共/私人服务的合同
[DataContract(Namespace = "http://www.site.com/Services/", Name = "Inventory")]
public class Inventory
{
[DataMember(Order = 1)]
public string MPN{ get; set; }
[DataMember(Order = 2)]
public string DataSheetURL { get; set; }
[DataMember(Order = 3)]
public List<Distributor> Stock { get; set; }
}
[DataContract(Namespace = "http://www.site.com/Services/", Name = "Distributor")]
public class Distributor
{
[DataMember(Order = 1)]
public string Name { get; set; }
[DataMember(Order = 2)]
public string Part { get; set; }
[DataMember(Order = 3)]
public int Quantity { get; set; }
[DataMember(Order = 4)]
public string URL { get; set; }
}
错误信息:
第 1 行位置 166 中的错误。需要来自命名空间“http://www.site.com/Services/”的元素“Distributor”。遇到名称为“Inventory”、命名空间“http://www”的“元素” .site.com/Services/'。
我可能会以完全错误的方式来解决这个问题,因此对于更好的方法(带示例)的建议将不胜感激。我的最终目标是使用自定义对象和 DataContracts 在 WCF 和 WCF 或 ASMX 服务之间传递对象。
【问题讨论】:
标签: c# wcf datacontractserializer datacontract