0

我在我的 C# ASP.NET MVC Web 应用程序中遇到问题,我们正在尝试向不同版本的 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 版网络参考。但是,当此代码运行时,它会因以下错误而失败:

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

由于某种原因,返回值是 type Magento1510WebReference.salesOrderInvoiceEntity

像这样添加多个 Web 引用是否存在任何已知问题?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 的响应的肥皂信封对于两个版本实际上是相同的:

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 引用。对此我能做些什么吗?(我已经开始尝试建议将引用拆分为单独项目的答案)

4

1 回答 1

1

如果 WCF 服务以良好的方式进行版本控制,即使您使用 1.5 程序集,您也应该能够与 1.4 版本通信。你试过这个吗?

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

于 2011-07-11T06:06:43.003 回答