There are many posts and forums questions about the reasons of why User Profile Synchronization Service may fail to start: it may stuck in Starting status or return to Stopped state after some time. Recently we faced with similar issue and the reason was quite interesting: may be you won’t face with it in your environment, but technique used for troubleshooting may be useful for you.
Ok, everything has started with unsuccessful attempt to start User Profile Synchronization Service from Sharepoint Central administration. It froze some time in Starting state and then returned to Stopped state. In Sharepoint ULS logs the following message was added:
The Execute method of job definition Microsoft.Office.Server.UserProfiles.UserProfileImportJob (ID …) threw an exception. More information is included below. Operation is not valid due to the current state of the object.
at Microsoft.Office.Server.Administration.UserProfileApplicationJob.Execute(SPJobState jobState)
at Microsoft.SharePoint.Administration.SPTimerJobInvokeInternal.Invoke(SPJobDefinition jd, Guid targetInstanceId, Boolean isTimerService, Int32& result)
If you will google for it you may find the following frequent answer: add Read and Execute permissions to all users group or to Network Service on C:\Program Files\Microsoft Office Servers\15.0 folder. It didn’t help in our case. In the ULS there was also another error right after the message which is shown above:
SettingsProvider: System.InvalidOperationException: Adding Microsoft.SharePoint.PowerShell...
Data Source=dbserver;Initial Catalog=SharePoint_Service_User_Profile_Sync;Integrated Security=True;Enlist=False;Pooling=True;Min Pool Size=0;Max Pool Size=100;Connect Timeout=15
at Microsoft.IdentityManagement.Samples.SettingsProvider.Microsoft.IdentityManagement.Settings.ISettingsProvider.TryGetSetting(String name, String& value)
at Microsoft.IdentityManagement.Settings.ExternalSettingsManager.TryGetSetting(String settingName, String supportedParameters, String& value, String& unsupportedValue)
at Microsoft.ResourceManagement.Utilities.ExternalSettingsManager.TryGetSetting(String settingName, String supportedParameters, String& value, String& unsupportedValue)
at Microsoft.ResourceManagement.Data.DatabaseConnection.Open(DataStore store)
at Microsoft.ResourceManagement.Data.TransactionAndConnectionScope..ctor(Boolean createTransaction, IsolationLevel isolationLevel, DataStore dataStore)
at Microsoft.ResourceManagement.Data.TransactionAndConnectionScope..ctor(Boolean createTransaction)
at Microsoft.ResourceManagement.Data.DataAccess.RegisterService(String hostName)
at Microsoft.ResourceManagement.Workflow.Hosting.HostActivator.RegisterService(String hostName)
at Microsoft.ResourceManagement.WebServices.ResourceManagementServiceHostFactory.CreateServiceHost(String constructorString, Uri baseAddresses)
at Microsoft.ResourceManagement.WindowsHostService.OnStart(String args)
In order to check what may be the reason of this exception I needed to find assembly with Microsoft.IdentityManagement.Samples.SettingsProvider class. In the GAC there are several Microsoft.IdentityManagement.* assemblies, but they don’t contain mentioned class. Then I went to the folder which contains executable file for Forefront Identity Manager Service (e.g. "C:\Program Files\Microsoft Office Servers\15.0\Service\Microsoft.ResourceManagement.Service.exe", but it may be different in your case. In order to check it go to Services MMC and find executable file for Forefront Identity Manager Service service on General tab of service details window). There was SettingsProvider.dll which contained interested class.
Here is the code of Microsoft.IdentityManagement.Samples.SettingsProvider.GetDbConnection() method which threw exception:
Exception is thrown on line 32 in SqlConnectionStringBuilder(connectionString) constructor call. It occurs if connection string passed to SqlConnectionStringBuilder constructor is invalid. If we will see few records up we will find that connection string is retrieved using quite interesting way: Windows PowerShell process is launched and inside it the following PowerShell script is executed:
I.e. it gets reference on User Profile Sync database which id is stored in HKEY_LOCAL_MACHINE > System > CurrentControlSet > Services > FIMSynchronizationService > Parameters > SyncDatabaseId and then just calls its DatabaseConnectionString property in last script command which makes it return value of the PowerShell process which is returned to Microsoft.IdentityManagement.Samples.SettingsProvider.GetDbConnection() method.
This code doesn’t contain dependencies on other sub systems so it was quite easy to simulate it by copying to standalone console application. When I ran it I found that instead of single connection string it returns 2 lines:
Data Source=dbserver;Initial Catalog=SharePoint_Service_User_Profile_Sync;Integrated Security=True;Enlist=False;Pooling=True;Min Pool Size=0;Max Pool Size=100;Connect
And the problem was in the first line “Adding Microsoft.SharePoint.PowerShell...”. Because of it SqlConnectionStringBuilder() constructor threw exception because this is not correct connection string. After that I contacted guys who configured environment and they said that added Microsoft.SharePoint.PowerShell module to default Windows PowerShell by default because it is needed almost everytime (the code which adds it was added via custom profile.ps1 file in powershell.exe folder C:\Windows\System32\WindowsPowerShell\v1.0). As it turned out it was not very good idea because it caused mentioned problem with starting User Profile Synchronization Service: if you need PowerShell console with Sharepoint support it is better to use Sharepoint Management Shell which loads Microsoft.SharePoint.PowerShell automatically, while Windows shell should be left untouched. After I deleted profile.ps1 with custom code and restarted the PC, User Profile Synchronization Service had started successfully. Hope that this story will help you to troubleshoot similar problems.