How to resolve "No User Profile Application available to service the request"

When I was trying to get the user properties

serverContext = ServerContext.GetContext(site);
UserProfileManager upm = new UserProfileManager(serverContext);
UserProfile up = upm.GetUserProfile(strAccountName);

I was getting the following error. 


How to resolve:
  • Go to Central Administration => Application Management => Manage Service Applications => User Profile Service Application


  • Click on Administrators => Add the user and give Full Control.

  • Click on Permissions => Add the user and give Full Control.