2011-09-21 60 views
3

我有一些與統一時間管理器的問題,它使用像它的單例那樣的對象,但在配置中我將它設置爲「PerWebRequest」。控制器'TestController'的單個實例不能用於處理多個請求

錯誤是: 控制器'TestController'的單個實例不能用於處理多個請求。如果自定義控制器工廠正在使用中,請確保它爲每個請求創建控制器的新實例。

的PerWebRequest代碼:

public class UnityPerWebRequestLifetimeManager : LifetimeManager 
{ 
    private HttpContextBase _httpContext; 

    public UnityPerWebRequestLifetimeManager() 
     : this(new HttpContextWrapper(HttpContext.Current)) 
    { 
    } 

    public UnityPerWebRequestLifetimeManager(HttpContextBase httpContext) 
    { 
     _httpContext = httpContext; 
    } 

    private IDictionary<UnityPerWebRequestLifetimeManager, object> BackingStore 
    { 
     get 
     { 
      _httpContext = (HttpContext.Current != null) ? new HttpContextWrapper(HttpContext.Current) : _httpContext; 

      return UnityPerWebRequestLifetimeModule.GetInstances(_httpContext); 
     } 
    } 

    private object Value 
    { 
     [DebuggerStepThrough] 
     get 
     { 
      IDictionary<UnityPerWebRequestLifetimeManager, object> backingStore = BackingStore; 

      return backingStore.ContainsKey(this) ? backingStore[this] : null; 
     } 

     [DebuggerStepThrough] 
     set 
     { 
      IDictionary<UnityPerWebRequestLifetimeManager, object> backingStore = BackingStore; 

      if (backingStore.ContainsKey(this)) 
      { 
       object oldValue = backingStore[this]; 

       if (!ReferenceEquals(value, oldValue)) 
       { 
        IDisposable disposable = oldValue as IDisposable; 

        if (disposable != null) 
        { 
         disposable.Dispose(); 
        } 

        if (value == null) 
        { 
         backingStore.Remove(this); 
        } 
        else 
        { 
         backingStore[this] = value; 
        } 
       } 
      } 
      else 
      { 
       if (value != null) 
       { 
        backingStore.Add(this, value); 
       } 
      } 
     } 
    } 

    [DebuggerStepThrough] 
    public override object GetValue() 
    { 
     return Value; 
    } 

    [DebuggerStepThrough] 
    public override void SetValue(object newValue) 
    { 
     Value = newValue; 
    } 

    [DebuggerStepThrough] 
    public override void RemoveValue() 
    { 
     Value = null; 
    } 
} 

控制器:

public class TestController : Controller 
{ 
    // 
    // GET: /Test/ 

    public TestController() 
    { 

    } 

    public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult RadioButtonList() 
    { 
     return View("TestControl"); 
    } 
} 

的控制器廠:

public class ControllerFactory : DefaultControllerFactory 
{ 
    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) 
    { 
     return (controllerType == null) ? base.GetControllerInstance(requestContext, controllerType) : IoC.Resolve<IController>(controllerType); 
    } 
} 

而且在一個視圖中我試圖使用它像這樣:

...

<% Html.RenderAction<TestController>(c => c.RadioButtonList()); %> 

         <% Html.RenderAction<TestController>(c => c.RadioButtonList()); %> 

...

我不知道自己做錯了什麼嗎?

謝謝。

回答

3

這兩個統一控制器請求都在相同的HTTP請求/回覆中創建,因此您得到相同的實例。您需要切換,以便控制器具有瞬態生命週期。

因爲您運行的是MVC3,所以我會切換到DependencyResolver而不是使用ControllerFactory

+0

但是控制器被設置爲瞬態生命週期。 那麼,我該如何將它設置爲在同一個HTTP請求中響應同一個控制器的多個請求呢? 謝謝... – may215

+0

嘗試切換到DependencyResolver,可能是您的控制器工廠的東西。 – jgauffin

+0

我這樣做了,但是,我仍然得到同樣的問題,也許我需要檢查該類型是否已經註冊然後,我會跳過對同一類型的解決嗎? – may215