System.ServiceModel.CommunicationException on WCF Web Service

26 views Asked by At

I'm trying use Magento API, getting a list of products, I have already added the service reference, the call of method catalogProductListAsync() pass by much:

other methods

and I think the one is catching the exception is this:

ThrowForNonSucess()

but when I run the console application I get

error

Here is my code:

using System;
using Magento_API.Services;
using Magento;

namespace Magento_API
{
    class Program
    {
        static void Main()
        {
            // Configurações do serviço Magento SOAP
            string magentoUrl = "https://#######/api/v2_soap?wsdl=1&services=catalogProductList";
            string apiUser = "####";
            string apiKey = "#######";

            // Criar uma instância do serviço Magento
            ProductListService magentoService = new ProductListService(magentoUrl, apiUser, apiKey);

            // Obter a lista de produtos
            catalogProductEntity[] products = magentoService.GetProductListAsync().Result;

            // Processar os resultados
            if (products != null)
            {
                foreach (catalogProductEntity product in products)
                {
                    Console.WriteLine($"Product ID: {product.product_id}");
                    Console.WriteLine($"SKU: {product.sku}");
                    Console.WriteLine($"Name: {product.name}");
                    Console.WriteLine($"Set: {product.set}");
                    Console.WriteLine($"Type: {product.type}");
                    Console.WriteLine($"Category IDs: {string.Join(", ", product.category_ids)}");
                    Console.WriteLine();
                }
            }

            Console.ReadLine(); // Para manter a janela do console aberta
        }
    }
}
    using Magento;
    using System;
    using System.ServiceModel;
    using System.Threading.Tasks;

    namespace Magento_API.Services
    {
        public class ProductListService
        {
            private PortTypeClient _client;

            public ProductListService(string magentoUrl, string apiUser, string apiKey)
            {
                _client = new PortTypeClient();
                _client.Endpoint.Address = new EndpointAddress(magentoUrl);
                _client.ClientCredentials.UserName.UserName = apiUser;
                _client.ClientCredentials.UserName.Password = apiKey;
            }

            public async Task<catalogProductEntity[]> GetProductListAsync()
            {
                try
                {
                    string sessionId = await _client.loginAsync(
                        _client.ClientCredentials.UserName.UserName,
                        _client.ClientCredentials.UserName.Password);

                    if (!string.IsNullOrEmpty(sessionId))
                    {
                        var request = new Magento.catalogProductListRequest();
                        var response = await _client.catalogProductListAsync(request);

                        return response.storeView;
                    }
                    else
                    {
                        throw new Exception("Failed to create session.");
                    }
                }
                catch (System.ServiceModel.Security.MessageSecurityException ex)
                {
                    throw new Exception($"Authentication Error: {ex.Message}");
                }
                catch (Exception ex)
                {
                    throw new Exception($"Error: {ex.Message}");
                }
            }
        }
    }

I'm a beginner in here so I don't know what you need to help me, but if something is missing I'm here to share with you.

The URL is not wrong, I have already search on Google and it returned a XML.

0

There are 0 answers