bgmulinari / B1SLayer

A lightweight SAP Business One Service Layer client for .NET
MIT License
136 stars 47 forks source link

Connection using different SAP users, not defined in Program.cs #51

Open riccardopasqualetti opened 1 year ago

riccardopasqualetti commented 1 year ago

Hi, I need to connect to SAP with several users.
Is there a way to provide user and password when the service is running and not in Program.cs? Thank you in advance.

bgmulinari commented 1 year ago

Hi, @riccardopasqualetti.

In case you need to connect to multiple databases or use multiple users, you will have to create and manage multiple SLConnection objects in your application and use the appropriate one when performing your requests.

A simple way to achieve this would be by creating a Dictionary containing your connection objects and a key to uniquely identify each one.

var connections = new Dictionary<string, SLConnection>()
{
    { "myKey1", new SLConnection("https://localhost:50000/b1s/v1/", "SBO_COMPANY", "user1", "password") },
    { "myKey2", new SLConnection("https://localhost:50000/b1s/v1/", "SBO_COMPANY", "user2", "password") },
    ...
}

Then, you could perform requests directly from the Dictionary by specifying the key:

var businessPartners = await connections["myKey1"].Request("BusinessPartners").GetAsync();

Anyway, this is just a suggestion and there are multiple ways you could implement this to best suit your scenario.

Let me know if this helps.