C#问题将多个Web引用添加到同一第三方API的不同版本?

时间:2011-07-11 05:02:28

标签: c# magento versioning xml-deserialization web-reference

我在C#ASP.NET MVC网络应用程序中遇到问题,我们正在尝试添加对不同版本的Magento API的多个引用。

基本上我的应用程序需要能够连接到不同的Magento电子商务商店。其中一些商店可能是1.4版本,有些可能是1.5等等。为了处理这种情况,我添加了多个Web引用,每个版本对应一个:

  <applicationSettings>
    <WebApp.Properties.Settings>
      <setting name="WebApp_MagentoWebReference_MagentoService"
        serializeAs="String">
        <value>http://example.com/magento1411/index.php/api/v2_soap/index/</value>
      </setting>
      <setting name="WebApp_Magento1510WebReference_MagentoService"
        serializeAs="String">
        <value>http://example.com/magento1510/index.php/api/v2_soap/index/</value>
      </setting>
    </WebApp.Properties.Settings>
  </applicationSettings>

然后在代码中我确保使用与我定位的版本匹配的正确的Web引用创建一个实例:

string url = "http://example.com/magento1411/index.php/api/v2_soap/";
_magentoService = new MagentoWebReference.MagentoService();
_magentoService.Url = url;
string apiUser = "user";
string apiKey = "key";
sessionId = _magentoService.login(apiUser, apiKey); 

var magentoProductList = _magentoService.catalogProductList(sessionId, null, null);

从1.4版本商店请求产品列表时,您可以看到我故意创建1.4版Web引用。但是,当此代码运行时,它会因此错误而失败:

Cannot assign object of type WebApp.Magento1510WebReference.salesOrderInvoiceEntity[] 
to an object of type WebApp.MagentoWebReference.salesOrderInvoiceEntity[].

由于某种原因,返回值的类型为Magento1510WebReference.salesOrderInvoiceEntity

添加多个这样的网络引用是否存在任何已知问题? Visual Studio或IIS是否在两个引用之间混淆了?

这是完整的堆栈跟踪:

System.Exception: There was a problem recieving a list of invoices from mageto store: http://example.com ---> System.InvalidOperationException: There is an error in XML document (2, 485). ---> System.InvalidCastException: Cannot assign object of type WebApp.Magento1510WebReference.salesOrderInvoiceEntity[] to an object of type WebApp.MagentoWebReference.salesOrderInvoiceEntity[].
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read351_salesOrderInvoiceListResponse()
   at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer469.Deserialize(XmlSerializationReader reader)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
   at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
   at WebApp.MagentoWebReference.MagentoService.salesOrderInvoiceList(String sessionId, filters filters) in C:\src\WebApp.2010\WebApp.UI\Web References\MagentoWebReference\Reference.cs:line 3073
   at WebApp.Controllers.Magento.MagentoController.GetLatestInvoices() in C:\src\WebApp.2010\WebApp.UI\Controllers\Magento\MagentoController.cs:line 217
   --- End of inner exception stack trace ---

我可以看到,当响应从Magento API返回时,XmlSerializer.Deserialize变得混乱,并认为XML来自Magento 1.5参考,而不是Magento 1.4参考。问题是为什么要这样做,以及如何解决?

编辑:

我怀疑Magento API响应的soap信封实际上对两个版本都是相同的:

1.4.1 soap = <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:loginResponse><loginReturn xsi:type="xsd:string">6325f7753ea72ba70e0a04254d58abe5</loginReturn></ns1:loginResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
1.5.1 soap = <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:loginResponse><loginReturn xsi:type="xsd:string">90ebce956623bb7e1637165306de40d0</loginReturn></ns1:loginResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

这意味着当收到响应时,Xml Deserializer无法确定要使用哪个Web引用,因为它们都与响应签名匹配。它最终使用Magento1510参考来反序列化,当它应该是Magento1411参考时。我能做些什么吗? (我已经开始尝试将建议分成不同项目的答案)

1 个答案:

答案 0 :(得分:1)

如果WCF服务的版本很好,即使使用1.5程序集,也应该可以与1.4版本对话。你试过这个吗?

如果这不起作用,我认为您需要将每个版本的代理分成新项目。这样,您可以确保代理只知道当前版本,并且可以避免命名空间冲突。