Magento – How to access Magento 2 soap api from C#

magento2soapsoap-api-v2

I would like to know (a sample code if it is not too much to ask for) how to talk to Magento 2's SOAP API from C# using Webservice (not service reference). With Magento 1.x versions I used something like

static MagentoWebService.MagentoService MyService = new MagentoWebService.MagentoService();

static string Login = MyService.login(Config.Login, Config.Password);

where as login being,

    public string login(string username, string apiKey) 
    {
        object[] results = this.Invoke("login", new object[] {
                    username,
                    apiKey});
        return ((string)(results[0]));
    }

How do I do the same/similar one with Magento 2. I have seen few example given in PHP. However, I am struggling to convert that in to C# or understand properly. Could someone please help with this.

So far in my new code I tried this:

catalogProductRepositoryV1Service productservice = new catalogProductRepositoryV1Service();
        CatalogProductRepositoryV1GetResponse response = new CatalogProductRepositoryV1GetResponse();
        CatalogProductRepositoryV1GetRequest request = new CatalogProductRepositoryV1GetRequest();
        request.sku = "WJ01";
        response = productservice.catalogProductRepositoryV1Get(request);
        Console.WriteLine(response.result.id.ToString());

But it wont work, since I haven't logged in to the SOAP service yet.

Best Answer

I was able to make requests to Magento 2 web API using the following example in 2014 (before 2.0 release), but it should work now as well.

To run it, follow the steps:

  1. Open Visual Studio C# 2010 Express
  2. Create new console application project
  3. In the project tree click on the project root to view context menu and select "Add Service Reference"
  4. Set address to http://<magento.host>/soap/default?wsdl&services=customerCustomerAccountServiceV1 namespace to MagentoService
  5. Add example below to your project and update Access token field with actual value
  6. Run the program, console will be opened and simple attribute values are displayed

C# console application example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.MagentoService;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace ConsoleApplication1
{
    class Program
    {
        const string accessToken = "9b8cpqlvrberc51o02nccmg82yyechxe";

        static void Main(string[] args)
        {
            customerCustomerAccountServiceV1PortTypeClient customerApi = new customerCustomerAccountServiceV1PortTypeClient();
            CustomerCustomerAccountServiceV1GetCustomerRequest request = new CustomerCustomerAccountServiceV1GetCustomerRequest();
            request.customerId = 1;
            CustomerCustomerAccountServiceV1GetCustomerResponse response;
            try
            {
                /** Set authorization headers and sent a request */
                using (OperationContextScope scope = new OperationContextScope(customerApi.InnerChannel))
                {
                    var httpRequestProperty = new HttpRequestMessageProperty();
                    httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Bearer " + accessToken;
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

                    response = customerApi.customerCustomerAccountServiceV1GetCustomer(request);
                }

                CustomerV1DataCustomer customerData = response.result;
                Console.Out.WriteLine("Customer email is: {0}.", customerData.email);
                Console.Out.WriteLine("Custom attributes:", customerData.email);
                foreach (FrameworkDataEavAttributeValue attribute in customerData.customAttributes)
                {
                    /** Cast object of generic type to its real type */
                    System.Xml.XmlNode[] value = (System.Xml.XmlNode[])attribute.value;
                    Console.WriteLine("{0}: {1}", attribute.attributeCode, value[0].Value);
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.ToString());
            }
            Console.In.Read();

        }
    }
}

This example uses Service reference (not Webservice), but still might be helpful to understand how to pass authentication headers.

Related Topic