Salesforce API .NET exception – a strange one

Came across this issue for the 2nd time, so thought it was worth writing about since google doesn’t really turn up much.

I’d written a .NET4 c# console app that connects to Salesforce’s API and queries for specific record entities that have been modified. The console app was set to compile for Any CPU and was always running x64 on my laptop and the server. I extended the application to query the Salesforce Opportunity entity, which worked fine on the laptop but not on the server (Microsoft Windows Server 2008 R2 Standard).

The server returned the following error;
System.InvalidOperationException: There is an error in XML document (1, 3739). ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

So it was obviously a machine specific issue, since it worked on my laptop (and another server). But there really wasn’t much else to help, so troubleshooting then begins.
The end result is that it transpires running the console app as complied x86 only (forcing 32 bit running) fixes the issues.

Hope this helps someone.

Login failed. The login is from an untrusted domain and cannot be used with Windows authentication

Potentially a Loopback problem (well it was for me).

http://support.microsoft.com/kb/896861

System.Data.SqlClient.SqlException: Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.WaitForPendingOpen()

HttpWebRequest throws “The specified registry key does not exist.”

Encountered a strange issue today, upon executing the following line of code, a “The specified registry key does not exist.” error was thrown.

A little googling reveals that the cause was a windows security update that was rolled out at work: MS12-074

Here’s a quote from the Microsoft KB article;

To enable the legacy functionality of WPAD, create the LegacyWPADSupport value in the following registry subkey:

Registry location: HKEY_LOCAL_MACHINESOFTWAREMicrosoft.NETFramework
DWORD (32-bit) Value name: LegacyWPADSupport
Value data: 1
The fix was to create the registry key with a value of 0.

WCF Configuration Pt1 : SSL and Windows Authentication

WCF isn’t the easiest beast to wrangle, and when looking to secure a WCF web service I usually do it in stages.
In this post and further posts in the next week, I’ll be securing a WCF web service with various endpoints with various different security requirements.  To start with I’m just going to secure it with Windows authentication and SSL.

It always seems to take a little fiddling to get to the first stage, published in IIS using SSL and Windows Authentication whilst still functioning.

Once IIS has been configured

  • Website created
  • Single HTTPS binding
  • SSL Settings (Require) (Ignore client certificates)

The next step is to get the webconfig to work over SSL and to use windows authentication.

And the endpoint config will look something like this;

So there you go, simple configuration to use enforce SSL and Windows Authentication in WCF.