Search code examples
asp.netprofilehttpcontextapplication-start

Can't get profile data in Application_Start


I have the following code at some point in Application_Start

MembershipUserCollection users = Membership.GetAllUsers();

foreach (MembershipUser u in users)
{
    ProfileBase profile = ProfileBase.Create(u.UserName, false);

    if (profile["Index"] != null)
    {
        // Some code
    }
}

The problem is that when i try to access the profile properties I get "Request is not available in this context" httpException. I have tried using profile.GetPropertyValue but no luck also.

Why does profile need the request object and how can I get this to work?

EDIT:

I have looked into the .NET code and found that at some point SqlProfileProvider.GetPropertyValuesFromDatabase(string userName, SettingsPropertyValueCollection svc) is called, inside of which rests this string of code:

if (!current.Request.IsAuthenticated)

Apparently this will never work if not in request context. The workaround descripbed in Onur's answer will work fine.


Solution

  • Apparently you are trying to access request object which is not possible in your case.

    Please take a look following link and apply one of workarounds if possible. http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart

    and if you give more information what are you trying to achieve we can find another way.