2 votes

SII Suministro Inmediato Información - Change endpoint at runtime

I am developing a project in C# for sending invoices through the SII.

The project is almost finished, but I would like to know if it is possible (and how to do it) to change the url of the endpoint at runtime, so that when I activate a checkbox, it sends to the test url or to the production url.

I have imported the WSDLs into Visual Studio and quickly explained how I have my code structured:

using SII.WSDL_SuministroFacturasRecibidas; // El nombre que he asignado a la referencia de servicio

namespace SII
{
    public class SuministroFacturasRecibidas
    {
        public SuministroFacturasRecibidas()
        {
            // Creo el servicio
            siiService servicio = new siiService(); 

            // Proceso las facturas
            SuministroLRFacturasRecibidas suministroLRFacturasRecibidas = new SuministroLRFacturasRecibidas();
            suministroLRFacturasRecibidas.Cabecera = AnadirCabecera();
            suministroLRFacturasrecibidas.RegistroLRFacturasRecibidas = AnadirFacturas();

            // Añado el certificado digital al servicio
            servicio.ClientCertificates.Add(BuscarCertificado());

            // Trato la respuesta
            RespuestaLRFRecibidasType respuesta = servicio.SuministroLRFacturasRecibidas = suministroLRFacturasRecibidas;
            TratarRespuesta(respuesta);
        }
    }
}

The problem is that from the moment I do "siiService service = new siiService()" the service is already picking up the url found in the configuration in the project properties window.

I don't know if there is a way to change that url or if I am approaching the project differently than I should be. It is the first time I use a webservice in a project and at least I see that the invoices are being uploaded to hacienda.

Thank you.

2voto

frikinside Points 760

It is possible to change the url at runtime and I don't see any problem arising from it if you do it before executing any query.

When you reference a service in C# it generates a proxy class (in your case siiService ) that encapsulates all the calls and properties you have parsed from the wdsl of the service. Among these properties is a public property Url which is given a value in the constructor. Once instantiated, you can change the value of that property without any problems:

Using the code you provide as an example:

using SII.WSDL_SuministroFacturasRecibidas; // El nombre que he asignado a la referencia de servicio

    namespace SII
    {
        public class SuministroFacturasRecibidas
        {
            public SuministroFacturasRecibidas()
            {
                // Creo el servicio
                siiService servicio = new siiService(); 
                servicio.Url = "http://urldelsercvicioactualizada.com";

                // Proceso las facturas
                SuministroLRFacturasRecibidas suministroLRFacturasRecibidas = new SuministroLRFacturasRecibidas();
                suministroLRFacturasRecibidas.Cabecera = AnadirCabecera();
                suministroLRFacturasrecibidas.RegistroLRFacturasRecibidas = AnadirFacturas();

                // Añado el certificado digital al servicio
                servicio.ClientCertificates.Add(BuscarCertificado());

                // Trato la respuesta
                RespuestaLRFRecibidasType respuesta = servicio.SuministroLRFacturasRecibidas = suministroLRFacturasRecibidas;
                TratarRespuesta(respuesta);
            }
        }
    }

In principle, from what you tell us, the url associated with the reference to the service is the correct one, so you should not change it. However, you have the possibility to do so if necessary.

This would not be the only way to do it, nor necessarily the most elegant. An option that (in case you need it) you could use, would be to modify that proxy class with the suffix "Service" that Visual Studio has generated you through the wsdl so that in the constructor of the same one, you establish it your already of base and you do not have to establish it every time that you instantiate it.

As a general rule this class is not exposed for modification by adding a reference to the service, so to modify this class with our needs we would have two options:

1. Inheritance

Instead of modifying the proxy class, we will generate our own by inheriting from it, and in our constructor, we will make the adjustments we consider necessary:

public class siiServicioWeb : siiService
{
    public siiServicioWeb ()
    {
        this.Url = "http://urldelsercvicioactualizada.com";
        //NOTA: Aquí podríamos añadir un método para cargar la url de alguna configuración, no hace falta que se encuentre hardcodeada.
    }

    public siiServicioWeb (string url)
    {
        this.Url = url;
    }
}

2. Generate the proxy class by ourselves

If we generate the class ourselves, we can make the modifications we need without problems. To avoid the tedious task of interpreting the wsdl by ourselves, there is a .NET process called . wsdl.exe that will do it for us. We can generate the class through the following command:

wsdl /language:cs  /protocol:SOAP /namespace:SII.ServicioWeb  /out:siiServicioWeb.cs /username:usuario /password:contraseña /domain:dominio http://host.com/path/al/wsdl

The parameters of /username , /password y /domain are mandatory in the event that the wsdl is protected by credentials.

I add a link to the documentation of the wsdl.exe in case it is of interest. Web Service Description Language tool

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X