【问题标题】:Using a method from a derived WCF proxy class (VS 2010 generated) yields error使用派生 WCF 代理类中的方法(生成 VS 2010)会产生错误
【发布时间】:2013-05-07 17:50:45
【问题描述】:

我正在尝试连接 Magento(os 商务解决方案)提供的 API WebService。

由于这个来自 magento 的 web 服务需要为每个方法(loginendSession 除外)进行会话处理,我想将生成的 WCF 代理类包装在我自己的类中,做所有的会话处理工作。

但是,我终其一生都无法弄清楚为什么我的派生类与我派生的原始 WCF 包装器的工作方式不同。特别是长时间运行的方法会失败(使用此堆栈跟踪:

System.ServiceModel.FaultException: Internal Error. Please see log for details.
Server stack trace:
    bei System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
    bei System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
    bei System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
    bei System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

这是我的包装类的(初稿)(MagentoService 是生成到 Magento Webservice 的代理类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace MagentoConnector
{
  public class MagentoController : MagentoService.Mage_Api_Model_Server_V2_HandlerPortTypeClient
  {
    private string username = "apiusername";
    private string password = "apiuserpassword";
    private string sessionId = null;
    public MagentoController(string url) : base()
    {
      if(!string.IsNullOrEmpty(url)) {
        if(!url.ToLower().StartsWith("http://")) url = "http://" + url;
        if(!url.EndsWith("/")) url += "/";
        url += "magento/index.php/api/v2_soap/index/";
        this.Endpoint.Address = new EndpointAddress(new Uri(url), this.Endpoint.Address.Identity, this.Endpoint.Address.Headers);
      }
      this.Open();
    }
    public MagentoController(string url, string username, string password) : this(url)
    {
      if(!string.IsNullOrEmpty(username)) {
        this.username = username;
      }
      if(!string.IsNullOrEmpty(password)) {
        this.password = password;
      }
      sessionId = this.login(this.username, this.password);
    }
    public new string login(string username, string password) 
    {
      if(!string.IsNullOrEmpty(sessionId)) {
        this.endSession(sessionId);
      }
      sessionId = base.login(username, password);
      this.username = username;
      this.password = password;
      return sessionId;
    }
    public string login() 
    {
      return login(this.username, this.password);
    }
    public void logoff()
    {
      if(!string.IsNullOrEmpty(sessionId)) {
        this.endSession(sessionId);
      }
    }
    ~MagentoController()
    {
      try {
        logoff();
      } catch(Exception) {
        ;
      }
    }

    public MagentoService.catalogCategoryTree catalogCategoryTree(string parentId, string storeView)
    {
      return base.catalogCategoryTree(sessionId, parentId, storeView);
    }

    public static MagentoService.catalogCategoryTree getCatalogTree(string parentId, string storeView)
    {
      string sessionId = null;
      MagentoService.catalogCategoryTree myTree = null;
      MagentoService.Mage_Api_Model_Server_V2_HandlerPortTypeClient myService = new MagentoService.Mage_Api_Model_Server_V2_HandlerPortTypeClient();
      try {
        myService.Open();
        sessionId = myService.login("applus-dev", "FL1LBveiNW8nGOg9QRa4z");
        myTree = myService.catalogCategoryTree(sessionId, null, null);
      } finally {
        if(myService != null && !string.IsNullOrEmpty(sessionId)) {
          myService.endSession(sessionId);
        }
      }
      return myTree;
    }
  }
}

请注意静态方法getCatalogTree,它直接与MagentoService 接口,并且可以正常工作(返回magento 的所有类别节点的树)。 方法catalogCategoryTree调用基方法失败,错误如上。

这里是调用代码:

MagentoController myService = new MagentoController(null, null, null);
MagentoService.catalogCategoryTree myTree = myService.catalogCategoryTree(parentId, storeView);

我无法弄清楚为什么会这样。使用静态方法和调用基类的方法有什么区别?

除此之外,使用 .NET 的 a#*(使用 v2 soap 服务变得更好)使用 magento 网络服务是一件痛苦的事情......

亲切的问候,

阿恩特

【问题讨论】:

  • 深入挖掘我发现这只发生在需要一些时间来处理/发送大量数据的方法上。但是我仍然不明白为什么在我调用基类的方法时会在派生类中发生这种情况,但如果我直接实例化基类并调用相同的方法则不会。

标签: c# .net magento


【解决方案1】:

我找到了答案,它的名字叫愚蠢……

Magento 允许在 catalogCategoryTree 方法中使用空参数 parentId

为了测试我的包装类,我构建了一个简单的表单,它将parentIdstoreView 参数传递给`catalogCategoryTree 方法。但是,当没有输入任何内容时,我会传递空字符串文字(文本字段的值)。然后出现错误...

我在上面发布的工作静态测试方法丢弃了parentIdstoreView 参数并将null 传递给magento API,magento 显然可以处理。因此,Magento 在空参数(至少对于字符串)和空字符串之间有所不同。

如果我重写自己的 catalogCategoryTree 方法来检查空字符串,一切都会按计划进行......

public MagentoService.catalogCategoryTree catalogCategoryTree(string parentId, string storeView)
{
  parentId = (string.IsNullOrEmpty(parentId)) ? null : parentId;
  storeView = (string.IsNullOrEmpty(storeView)) ? null : storeView;
  return base.catalogCategoryTree(sessionId, parentId, storeView);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 2010-12-20
    相关资源
    最近更新 更多