Salesforce .NET Connection Pool
The following write-up will go through how to setup a connection to the Salesforce SOAP API using the Force.com Enterprise WSDL along with a custom connection pool wrapper I have written. I have used the repository pattern and object pool pattern to design a connection and connection pool for querying, saving, and deleting data from Salesforce through this SOAP API.
The connection pool code can be downloaded at the following github link.
The code from the link above enables you to query against any out of the box object or property. To hook into your custom Salesforce environment objects and properties you will need to update the Web Reference in the salesforce.connection project as outlined here. Also do not forget to modify the wsdl before updating the Web Reference because of the known bug with the wsdl generated from Salesforce.
Once you have your Web Reference set you will need to make modifications to the app.config of the calling code to include the following 4 settings.
- SalesforceUsername – the Salesforce email
- SalesforcePassword – the Salesforce password concatenated with the security token (requested through the salesforce portal).
- SalesforceConnectionPoolCapacity – the capacity of the connection pool
- SalesforceConnectionPoolGetConnectionTimeout – the timeout in milliseconds a query will wait for a connection to free up.
See the example App.config below (available in the github link above)
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <appSettings> <add key="SalesforceUsername" value="[salesforce login/email]" /> <add key="SalesforcePassword" value="[salesforce password + security token]" /> <add key="SalesforceConnectionPoolCapacity" value="4" /> <add key="SalesforceConnectionPoolGetConnectionTimeout" value="10000" /> </appSettings> </configuration>
In the console project “salesforce.connection.unity.demo” I use a Unity container (Microsoft’s DI tool) to register a singleton Connection Pool that spins up new connections as necessary. I included this to show how to do this in Unity but other DI tools such as ninject and spring will have similar features. The important lines to note for the Unity registration are as follows.
//Singleton connection pool. container.RegisterType<ISalesforceConnectionPool, SalesforceConnectionPool>(new ContainerControlledLifetimeManager()); //This registration calls the SalesforceConnectionPool Singleton to get a connection everytime a connection //is requested. The HierarchicalLifetimeManager is important here because it is makes sure the Dispose() //method is called on the SalesforceConnection that puts it back into the SalesforceConnectionPool. container.RegisterType<ISalesforceConnection>(new HierarchicalLifetimeManager(), new InjectionFactory(s => container.Resolve<ISalesforceConnectionPool>().GetConnection()));
Once you have the SalesforceConnectionPool and SalesforceConnection registered with your ioc container instances of the this SalesforceConnection will be injected into any data access objects that are also registered in your ioc container and that also have the ISalesforceConnection passed into the contructor.
The cases where you want to use this connection pooling are when you have applications repetitively hitting the Salesforce API. Spinning up new instances of the SalesforceConnection is expensive so having them pooled for reuse will make your calling application more performant. I wrote this connection pool to sit inside an ASP.NET Web API application that is accessed by an Angular2 front end. This Angular2 front end has typeaheads/autocompletes that could potentially fire off many calls to Salesforce in a very short amount of time. Using this connection pool makes these kind of features possible.
You will need to have experience with the Salesforces query language “SOQL” in order to query, save, and delete salesforce objects using the SalesforceConnection class. The README.md of the github page has simple examples of each CRUD method in the SalesforceConnection.
Hope this helps!