powergate:code_reference:net_library:connectionsettings:sapconnect

SapConnect Class

Class which provides a default implementation for connecting to SAP services and handling CSRF protection.

Namespace: powerGate.Erp.Client
Assembly: powerGate.Erp.Client.dll

System.Object
powerGate.Erp.Client.SapConnect

public class SapConnect

The SapConnect type exposes the following members.

NameDescription
SapConnect(long reconnectInterval = 1500 * 1000)Initializes a new instance of the SapConnect class.
The default reconnectInterval parameter, expressed in milliseconds, is set to 25 minutes.
NameDescription
Implicit(SapConnect to Action<ErpClientSettings>)Converts the SapConnect object to an action of type Action<ErpClientSettings>.

Since most of the SAP systems require PUT requests as the preferred method to update entities in SAP, this class adapts the according PreferredUpdateMethod to UpdateMethod.PUT.

Additionally many SAP systems require by default CSRF protection.
The Netweaver Gateway extension requires for every update/write operation the X-CSRF-Token header to be set and a cookie.
Because the X-CSRF-Token expires after a certain amount of time, the Token will be automatically refreshed, on the next request after the expiration!
Depending on the SAP Release or the security session management configuration, the Token is valid for 24 hours or 30 minutes by default.
The default behaviour to retrieve a new token every 25min can be changed the reconnectInterval parameter in the constructor.

Connect to a SAP service using SapConnect

using (var erpclient = new ErpClient())
{
	var connectionSettings = new ConnectionSettings
	{
		Service = new Uri("http://sap.coolorange.com/CATALOGSERVICE"),
		Credentials = new System.Net.NetworkCredential("Administrator", "<secret password>"),
		OnConnect = new SapConnect() //SAP Connection
	};
	using (var service = erpclient.ConnectErp(connectionSettings))
		Console.Write("Connected to SAP service: {0}", service.Name);
}

Connecting multiple SAP systems with different CSRF-Token expiration intervals:

using (var erpclient = new ErpClient())
{
	var connectionSettings1 = new ConnectionSettings
	{
		Service = new Uri("http://sap.coolorange.com/CATALOGSERVICE"),
		OnConnect = new SapConnect()
	};
	var connectionSettings2 = new ConnectionSettings
	{
		Service = new Uri("http://sap.some_other_company.com/CATALOGSERVICE"),
		OnConnect = new SapConnect(86400 ‬* 1000) //tokens expire every 24h on this system
	};
	using (var service1 = erpclient.ConnectErp(connectionSettings1))
	using (var service2 = erpclient.ConnectErp(connectionSettings2))
	{
		Console.Write("Connected to SAP-1: {0}", service1.Name);
		Console.Write("Connected to SAP-2: {0}", service2.Name);
	}
}