2011-08-30 74 views
1

我创建一个ASP.NET MVC 3.0的网站,有几个基于该网站是否是用于开发,测试或生产不同的数据库初始化的添加配置文件值。我被困在测试初始化​​,因为我试图让一个测试用户创建。我可以让用户创建好,但是当我尝试添加一些配置文件值时,我得到:System.Web.HttpException:请求在此上下文中不可用。有没有办法在请求不可用的情况下添加配置文件值?自动生成的用户

下面的代码是什么正在运行:

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 

     if (ApplicationServices.GetInitialCatalog() != "tasktracker") 
     { 
      Database.SetInitializer(new TaskTrackerDropCreateDatabaseIfModelChanges()); 
     } 
     else 
     { 
      Database.SetInitializer(new TaskTrackerCreateDatabaseIfNotExists()); 
     } 

     using (var db = new TaskTrackerContext()) 
     { 
      db.Database.Initialize(false); 
     } 
    } 

public class TaskTrackerDropCreateDatabaseIfModelChanges : DropCreateDatabaseIfModelChanges<TaskTrackerContext> 
{ 
    protected override void Seed(TaskTrackerContext context) 
    { 
     // Set up the membership, roles, and profile systems. 
     ApplicationServices.InstallServices(SqlFeatures.Membership | SqlFeatures.Profile | SqlFeatures.RoleManager); 

     // Create the default accounts and roles. 
     if (ApplicationServices.GetInitialCatalog() == "tasktracker_testing") 
     { 
      if (Membership.GetUser("testuser", false) == null) 
      { 
       Membership.CreateUser("testuser", "password", "[email protected]"); 
       MembershipUser user = Membership.GetUser("testuser", false); 
       user.IsApproved = true; 

       var profile = ProfileBase.Create("testuser"); 
       profile.SetPropertyValue("FirstName", "test"); 
       profile.SetPropertyValue("LastName", "user"); 
       profile.SetPropertyValue("TimeZone", "US Mountain Standard Time"); 
       profile.Save(); 
      } 
     } 
    } 
} 
+0

有趣的问题。你看过使用新的通用提供商吗?不知道,如果你会遇到相同的httpcontext问题,但可能值得一看:http://www.hanselman。com/blog/IntroducingSystemWebProvidersASPNETUniversalProvidersForSessionMembershipRolesAndUserProfileOnSQLCompactAndSQLAzure.aspx –

+0

绝对奏效,非常感谢! –

+0

很高兴它的工作...我会张贴答案,所以你可以关闭问题:) –

回答

0

你尝试做一个叫“初始化()”:

profile.Initialize(username, true) 

后您创建的行动,看看上下文应该被初始化。

通过使用反射我看到初始化(见下文)的ProfileBase造成这种背景下,从设置:

public void Initialize(string username, bool isAuthenticated) 
{ 
    if (username != null) 
    { 
     this._UserName = username.Trim(); 
    } 
    else 
    { 
     this._UserName = username; 
    } 
    SettingsContext context = new SettingsContext(); 
    context.Add("UserName", this._UserName); 
    context.Add("IsAuthenticated", isAuthenticated); 
    this._IsAuthenticated = isAuthenticated; 
    base.Initialize(context, s_Properties, ProfileManager.Providers); 
} 

它似乎在这里工作,在SettingsContext()似乎正在宣告我的自定义属性的帐户在web.config中。

问候,

0

我再回来,因为我加入其实“初始化()”函数的解决方案不是其他测试之后真正运行。所以实际上我找到了一种正确运行的方式。

的问题“的要求是不是在这方面可参考”中的Application_Start在你的情况可能是由于应用模式“集成”,这是新的,而不是II7的经典模式。

看到一个很好的解释你的CA去的迈克沃洛达尔斯基的博客IIS7 Integrated mode: Request is not available in this context exception in Application_Start

我复制/粘贴的提取物可能表明的主要原因:

“*这个错误是由于在IIS7集成管道,使得在Application_Start事件无法请求上下文中的设计更改。当使用经典模式(在以前版本的IIS中运行时的唯一模式),曾经是可用,即使Application_Start事件一直打算在应用程序生命周期全球和要求无关的事件请求上下文。尽管这样,因为ASP。 .NET应用程序总是由第一个请求到应用程序启动时,它使用的是可能获得通过静态HttpContext.Current场的请求上下文。*“

为了解决这个问题,你可以使用移动你的第一个请求从初始化到的Application_Start和的BeginRequest执行第一个请求的具体要求初始化一个解决方法。

的代码一个很好的例子是在自己的博客做:

void Application_BeginRequest(Object source, EventArgs e) 

{ 

    HttpApplication app = (HttpApplication)source; 

    HttpContext context = app.Context; 



    // Attempt to peform first request initialization 

    FirstRequestInitialization.Initialize(context); 

} 




class FirstRequestInitialization 

{ 

    private static bool s_InitializedAlready = false; 
    private static Object s_lock = new Object(); 

    // Initialize only on the first request 
    public static void Initialize(HttpContext context) 
    { 
     if (s_InitializedAlready) 
     { 
      return; 
     } 

     lock (s_lock) 
     { 
      if (s_InitializedAlready) 
      { 
       return; 
      } 

      // Perform first-request initialization here 
      // 
      // You can use your create profile code here.... 


      //--- 

      s_InitializedAlready = true; 
     } 
    } 
}