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

1
public class ErpClient : IErpClient

The ErpClient type exposes the following members.

Constructors

Name

Description

../../../_images/method.ico ErpClient()

Initializes a new instance of the ErpClient class.

Properties

Type

Name

Description

../../../_images/prop.ico IErpServices

Services

Gets the list of connected services.

Methods

Type

Name

Description

../../../_images/method.ico IErpService

ConnectErp(ConnectionSettings connectionSettings)

Creates a connection to a service using the specified ConnectionSettings.

../../../_images/method.ico void

Dispose()

Disconnects from all services and releases all resources used by the ErpClient.

Extension Methods

Type

Name

Description

../../../_images/method.ico 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.

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 (http://services.odata.org) for demonstration purposes:
Connect to the NorthwindService

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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<ErpClientSettings>(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: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
	<html><head>
	<title>404 Not Found</title>
	</head><body>
	<h1>Not Found</h1>
	<p>The requested URL /$metadata was not found on this server.</p>
	<hr>
	<address>Apache Server at www.coolorange.com Port 443</address>
	</body></html>
*/

Error handling, analyze why entity could not be updated

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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<string, object>{{"EmployeeID", 66}},
	                                                new Dictionary<string, object> { { "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