C# 使用C连接到MagentoAPI#

C# 使用C连接到MagentoAPI#,c#,web-services,api,magento,C#,Web Services,Api,Magento,我正在尝试连接到Magento 1.4.0.1 API,但直到现在我都没有运气 我添加了一个名为MagentoAPI的服务参考,并将其指向http://mydomain.com/api/v2_soap?wsdl=1(我知道=1不是故意的,但是没有它就不能工作) 这很好,我得到了所有可用方法的列表,但是当我尝试使用其中任何一种方法时,它都不起作用 using Magento_Import.MagentoAPI; namespace Magento_Import { public part

我正在尝试连接到Magento 1.4.0.1 API,但直到现在我都没有运气

我添加了一个名为
MagentoAPI
服务参考
,并将其指向
http://mydomain.com/api/v2_soap?wsdl=1
(我知道
=1
不是故意的,但是没有它就不能工作)

这很好,我得到了所有可用方法的列表,但是当我尝试使用其中任何一种方法时,它都不起作用

using Magento_Import.MagentoAPI;

namespace Magento_Import
{
    public partial class _Default : System.Web.UI.Page
    {
        Mage_Api_Model_Server_V2_HandlerPortType handler;

        protected void Page_Load(object sender, EventArgs e)
        {
            string session = handler.login("username", "password");
        }
    }
}
这就是我初始化web服务的方式,但是当我调试代码时,
处理程序
null


我做错了什么?

好的,我是这样做的:

using Magento_Import.MagentoAPI;

namespace Magento_Import
{
    public partial class _Default : System.Web.UI.Page
    {
        Mage_Api_Model_Server_V2_HandlerPortTypeClient handler = new Mage_Api_Model_Server_V2_HandlerPortTypeClient();

        protected void Page_Load(object sender, EventArgs e)
        {
            string session = handler.login("username", "password");

            catalogProductEntity[] products;
            handler.catalogProductList(out products, session, null, null);
        }
    }
}

但我不确定这是否是最佳实践,如果有人知道更好的方法,请说:D

好的,我通过这样做来实现:

using Magento_Import.MagentoAPI;

namespace Magento_Import
{
    public partial class _Default : System.Web.UI.Page
    {
        Mage_Api_Model_Server_V2_HandlerPortTypeClient handler = new Mage_Api_Model_Server_V2_HandlerPortTypeClient();

        protected void Page_Load(object sender, EventArgs e)
        {
            string session = handler.login("username", "password");

            catalogProductEntity[] products;
            handler.catalogProductList(out products, session, null, null);
        }
    }
}

但是我不确定这是否是最佳实践,如果有人知道更好的方法,请说:D

在使用对象之前初始化对象是非常好的实践。处理程序为null的原因是因为您从未将其设置为值(即类实例的值)。在使用对象之前初始化对象是非常好的做法。处理程序为null的原因是,您从未将其设置为值(即类实例的值)。