--- myst: substitutions: method: |- ```{image} /img/code_reference/net_library/method.ico ``` prop: |- ```{image} /img/code_reference/net_library/prop.ico ``` --- # ErpClient Class Provides a class to connect with ERP services. **Namespace:** powerGate.Erp.Client\ **Assembly:** powerGate.Erp.Client.dll ## Inheritance Hierarchy [System.Object]()\  **powerGate.Erp.Client.ErpClient** ## Syntax ```{code-block} CSharp :linenos: public class ErpClient : IErpClient ``` The ErpClient type exposes the following members. ## Constructors | Name | Description | |------------------------|----------------------------------------------------| | {{method}} ErpClient() | Initializes a new instance of the ErpClient class. | ## Properties | Type | Name | Description | |---------------------------|----------|--------------------------------------| | {{prop}} [IErpServices][] | Services | Gets the list of connected services. | ## Methods | Type | Name | Description | |----------------------------|-------------------------------------------------------|---------------------------------------------------------------------------------| | {{method}} [IErpService][] | ConnectErp([ConnectionSettings][] connectionSettings) | Creates a connection to a service using the specified [ConnectionSettings][]. | | {{method}} void | Dispose() | Disconnects from all services and releases all resources used by the ErpClient. | ## Extension Methods | Type | Name | Description | |----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------| | {{method}} [IErpService][] | ConnectErp(Uri service, ICredentials credentials = null, bool ignoreCertificates = false, Action\<[ErpClientSettings][]\> onConnect = null) | Overloaded. Creates a connection to a service using the specified parameter values. | [IErpServices]: ierpservices [IErpService]: ierpservice [ConnectionSettings]: connectionsettings [ErpClientSettings]: erpclientsettings ## Remarks The **ConnectErp(..)** function is able to either connect to a CatalogService or directly to a Service.\ When calling the function passing settings having an url to a CatalogService, the client automatically connects to all the registered services with the same authentication as used for the login to the CatalogService.\ The function can be recalled for a single service with different authentication if that service is not available by using the authentication of the CatalogService. ## Examples In the following examples we are using public OData Services () for demonstration purposes:\ **Connect to the NorthwindService** ```{code-block} CSharp :linenos: using System; using powerGate.Erp.Client; namespace HelloWorldServices { class Program { static void Main(string[] args) { //Create Instance of client using (var erpclient = new ErpClient()) { //Connect to the service using (var service = erpclient.ConnectErp(new Uri("http://services.odata.org/V4/Northwind/Northwind.svc"))) Console.Write("Connected to service: {0}", service.Name); } } } } ``` **Connect to a service via secured SSL connection and trust all certificates** ```{code-block} CSharp :emphasize-lines: 11 :linenos: using System; using powerGate.Erp.Client; namespace HelloWorldServices { class Program { static void Main(string[] args) { var erpclient = new ErpClient(); var service = erpclient.ConnectErp(new Uri("https://services.odata.org/V4/Northwind/Northwind.svc"), ignoreCertificates: true); //Dispose Service and Client service.Dispose(); erpclient.Dispose(); } } } ``` [Connect to a SAP service]() **that requires authentication** ```{code-block} CSharp :emphasize-lines: 17, 18 :linenos: using System; using System.Net; using powerGate.Erp.Client; namespace HelloWorldServices { class Program { static void Main(string[] args) { using(var erpclient = new ErpClient()) { //Create special ConnectionSettings for connecting to the SAP service var connectionSettings = new ConnectionSettings { Service = new Uri("http://sap.coolorange.com"), Credentials = new NetworkCredential("EX_DEMO", "secret"), OnConnect = new SapConnect() }; using(var service = erpclient.ConnectErp(connectionSettings)) Console.Write("Connected to service: {0}", service.Name); } } } } ``` **Accessing the last server response** ```{code-block} CSharp :emphasize-lines: 13 :linenos: using System; using System.Net.Http; //assembly has to be referenced using powerGate.Erp.Client; namespace HelloWorldServices { class Program { static void Main(string[] args) { var onConnect = new Action(settings => { settings.AfterResponse = (response => { if (response.IsSuccessStatusCode) return; Console.WriteLine("Request Url: " + response.RequestMessage.RequestUri); Console.WriteLine("Status Code: " + response.StatusCode); Console.WriteLine("Body: " + response.Content.ReadAsStringAsync().Result); }) + settings.AfterResponse; }); using (var erpclient = new ErpClient()) try { erpclient.ConnectErp(new Uri("https://www.coolorange.com"), onConnect: onConnect); } catch (Exception e) { } } } } /* Console Output Request Url: https://www.coolorange.com/$metadata Status Code: NotFound Body: 404 Not Found

Not Found

The requested URL /$metadata was not found on this server.


Apache Server at www.coolorange.com Port 443
*/ ``` **Error handling, analyze why entity could not be updated** ```{code-block} CSharp :linenos: using System; using System.Collections.Generic; using powerGate.Erp.Client; namespace HelloWorldServices { class Program { static void Main(string[] args) { using (var erpclient = new ErpClient()) { try { using (var service = erpclient.ConnectErp(new Uri("http://services.odata.org/V4/Northwind/Northwind.svc"))) { var employees = service.EntitySets["Employees"]; employees.UpdatErpObject(new Dictionary{{"EmployeeID", 66}}, new Dictionary { { "FirstName", "Franz" }, { "LastName", "VomBerg" } }); } } catch (WebRequestException e) { Console.WriteLine("Message: " + e.Message); Console.WriteLine("Status Code: " + e.StatusCode); Console.WriteLine("Body: " + e.RawResponse); } } } } } /* Console Message: Not Implemented Status Code: 501 Body: {"error":{"code":"","message":"Not Implemented"}} */ ``` ## See also **Reference** - [powerGate.Erp.Client namespace]() - [Connect-ERP cmdlet]() - [Disonnect-ERP cmdlet]()