Skip to Content

DI API Connection in Production Environment

Today I wanted to install the first version of our addon in our Company, but I found out that the addon have a dependency of a DI API Config file, while it shouldn't.

On the computer I developed the addon works fine, but after I added the addon in the addon manager and I tried to run it on a few other computer (it installed fine), it threw an exception 'Couldn't connect to the Licence server..'. After a few tries and a version which prints to the console, I figured it it tires to connect with 'localhost:30000'. This address is provided to the SAP in the config file here: ''C:\Program Files (x86)\SAP\SAP Business One DI API\Conf\b1-local-machine.xml', saying:

<leaf kind="single" name="LicenseServer" type="String">
    <value>localhost:30000</value>
</leaf>

If I change it to our company's server ip, my addon works. My problem is that every new installation of the SAP B1 client will have the DI API's config set to localhost and I need to manually change it everytime. It is bothersome at least.

Other thing is we have two other addons which are not developed by me and are perfectly fine with the localhost address. So I am guessing I am doing something wrong (I followed the sample file though)

Here is the class I am using as a base class and every class that would use DI API inherit this:

public class SAPDIDao
{
    private static Company savedCompany = null;

    protected SAPbouiCOM.Application SBOApp = null;
    protected Company oCompany = null;

    public SAPDIDao(SAPbouiCOM.Application SBOApp)
    {
        this.SBOApp = SBOApp;
        this.oCompany = GetCompany(SBOApp);
    }

    public static Company GetCompany(SAPbouiCOM.Application SBOApp)
    {
        if (savedCompany == null)
        {
            Company tmpCompany = new Company();
            int contextCode = SetConnectionContext(SBOApp, tmpCompany);
            int errorCode = tmpCompany.Connect();
            Console.WriteLine("Connection to Licence server: " + tmpCompany.LicenseServer);
            if (errorCode != 0)
            {
                string lastException = "";
                tmpCompany.GetLastError(out errorCode, out lastException);
                Console.WriteLine("Company connect error: " + errorCode + ", msg: " + lastException);
                tmpCompany.GetLastError(out errorCode, out lastException);
                Console.WriteLine("Context: " + contextCode + ", msg: " + lastException);
                throw new Exception("Company connect error: " + errorCode + ", msg: " + lastException + "\nContext: " + contextCode + ", msg: " + lastException);
            }
            savedCompany = tmpCompany;
        }
        return savedCompany;
    }

    private static int SetConnectionContext(SAPbouiCOM.Application SBOApp, Company tmpCompany)
    {
        int setConnectionContextReturn = 0;
        string sCookie = tmpCompany.GetContextCookie();
        string sConnectionContext = SBOApp.Company.GetConnectionContext(sCookie);

        if (tmpCompany.Connected)
        {
            tmpCompany.Disconnect();
        }
        setConnectionContextReturn = tmpCompany.SetSboLoginContext(sConnectionContext);

        Console.WriteLine("DI API Connection, login context: " + setConnectionContextReturn);
        return setConnectionContextReturn;
    }
}

I am using SAP B1 9.2 and I tried with both the seperate ui/di dlls and the new one, which provides both as a single dll.

Also, I posted a Question about this already in the Past, but the solution there lead me to this problem:

https://archive.sap.com/discussions/thread/3964295

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    Nov 25, 2016 at 05:29 PM

    Hi Szabolcs,

    Did you try to use GetDICompany() from Company object from Application object?

    Instead of use your code above, follow this sample:

    I always use this method to get DI company object on my add-ons and I never had problems.

    Hope it helps.

    Kind Regards,

    Diego Lother

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 29, 2016 at 03:34 PM

    Hi Szabolcs,

    I've switched from that method after a source at SAP told me that it can cause issues with deadlocks (or at least increase their occurrence).

    Your mileage may vary.

    Pedro Magueija

    LinkedIn | Twitter | Blog

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 28, 2016 at 11:49 AM

    Hi,

    This solution works, but the performance of DI-API calls will be less because every call is now a RPC call instead of a COM-call.

    Kind Regerds,

    Ad

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 28, 2016 at 01:52 PM

    Iit is the other way, GetDICompany() is the slower way.

    Kind Regards

    Ad

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Ad,

      I always used GetDICompany(). I never had tested the other way, but even that GetDICompany() method is slower than other way, I never had performance problem with GetDICompany().

      Kind Regards,

      Diego Lother