C# 向同一第三方API的不同版本添加多个web引用时出现问题?

C# 向同一第三方API的不同版本添加多个web引用时出现问题?,c#,magento,versioning,xml-deserialization,web-reference,C#,Magento,Versioning,Xml Deserialization,Web Reference,我的C#ASP.NET MVC web应用程序有一个问题,我们正在尝试向不同版本的Magento API添加多个引用 基本上,我的应用程序需要能够连接到不同的Magento电子商务商店。其中一些存储可能是1.4版,一些可能是1.5版,依此类推。为了处理这种情况,我添加了多个web引用,每个版本一个: <applicationSettings> <WebApp.Properties.Settings> <setting name="WebApp

我的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>
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>
您可以看到,当从1.4版商店请求产品列表时,我故意创建了1.4版web引用。但是,当此代码运行时,会出现以下错误:

Cannot assign object of type WebApp.Magento1510WebReference.salesOrderInvoiceEntity[] 
to an object of type WebApp.MagentoWebReference.salesOrderInvoiceEntity[].
由于某些原因,返回值的类型为
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 ---
我可以看到,当响应从MagentoAPI返回时,XmlSerializer.Deserialize会感到困惑,认为XML来自Magento 1.5引用,而不是Magento 1.4引用。问题是它为什么会这样做,如何修复

编辑:

正如我所怀疑的,来自Magento API的响应的soap信封对于这两个版本实际上是相同的:

  <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>
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>
1.4.1 soap=6325f7753ea72ba70e0a04254d58abe5
1.5.1肥皂=90ebce956623bb7e1637165306de40d0

这意味着当接收到响应时,Xml反序列化器无法确定要使用哪一个web引用,因为它们都与响应签名匹配。它最终使用Magento1510引用进行反序列化,而它本应该是Magento1411引用。我能做些什么吗?(我已经开始尝试建议将引用拆分为单独项目的答案)

如果WCF服务的版本控制良好,那么即使使用1.5程序集,您也应该能够与1.4版本对话。你试过这个吗


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

@V4我的问题是web引用而不是程序集版本控制。我需要能够支持这两个版本,我不需要做从旧版本到新版本的绑定重定向。两者都必须是活动的、可用的和可用的。@Mikael对不起,我没有听清楚你说的话。“以良好的方式进行版本控制”是什么意思?无论如何,Magento是PHP,所以我调用的不是WCF服务。拆分成不同的项目可能值得一试。对不起,我原以为是WCF,但现在可以看出它不是。我认为把它分成不同的项目可能比其他项目更好。您可以创建一个公共接口,并使用工厂或IoC/MEF创建最适合您需要的实现。从长远来看,也应该让事情更干净。@Mikael这不是WCF,而是关于拆分web引用以便每个引用都有各自独立的项目工作的建议,因此,感谢您的接受。