I have been doing Salesforce.com (SFDC) application development for some time and the one thing I do know is that there are
some things that you just have to do over and over again. One of them is connecting to SFDC and ensuring a connection based
on a session identifier. To save some time, I thought about writing this as a blog post to save me some time in the future. I hope
you find it useful.
public static class SFDCManager
{
#region "Member Fields"
/// <summary>
/// SFDC Proxy
/// </summary>
private static SforceService _SfBinding;
#endregion
#region "Private Member Methods"
/// <summary>
/// Login Member Method
/// </summary>
/// <returns></returns>
private static bool Login()
{
if ( string.IsNullOrEmpty( SfUserName ) )
SfUserName = Properties.Settings.Default.SFUserName;
if ( string.IsNullOrEmpty( SfPassword ) )
SfPassword = Properties.Settings.Default.SFPassword;
bool bReturn = false;
int timeout = 9000000;
if ( _SfBinding == null )
{
_SfBinding = null;
_SfBinding = new SforceService();
_SfBinding.Timeout = timeout;
}
try
{
_SfBinding.AssignmentRuleHeaderValue = new AssignmentRuleHeader();
_SfBinding.AssignmentRuleHeaderValue.useDefaultRule = true;
LoginResult loginResult = _SfBinding.login( SfUserName , SfPassword );
_SfBinding.SessionHeaderValue = null;
_SfBinding.SessionHeaderValue = new SessionHeader();
_SfBinding.SessionHeaderValue.sessionId = loginResult.sessionId;
_SfBinding.Url = loginResult.serverUrl;
bReturn = true;
}
catch
{
//TODO: You should handle this exception
throw;
}
return bReturn;
}
#endregion
#region "Member Property"
/// <summary>
/// SFDC Connection UserName Property
/// </summary>
public static string SfUserName
{
get;
set;
}
/// <summary>
/// SFDC Connection Password Property
/// </summary>
public static string SfPassword
{
get;
set;
}
#endregion
}
Posted by: |